• Resolved kunstudios

    (@kunstudios)


    Hi,

    I think I have a syntax problem. I’m trying to show the description of a custom taxonomy on custom tab on my product pages.

    I’m using this hook:

    function woo_new_product_tab_content() {

    // The new tab content

    echo '<h2>Instructor</h2>';
    echo term_description( 23, 'instructor');

    }

    I’ve put in 23 for the $term to check if it works. Which it does, but I’d like it to print the relevant description for the instructor that’s listed. If I leave $term empty, it doesn’t work. But putting in an ID makes that instructor show up on every page. What am I missing? I’ve read https://developer.ww.wp.xz.cn/reference/functions/term_description/ but am still stuck.

    Thanks for any help!

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support EtienneP a11n

    (@etiennep)

    Hi there!

    I am not sure how you are adding the custom description for the instructors, but here is how I did it on my test site.

    I created a custom field called Instructors for WooCommerce products using https://ww.wp.xz.cn/plugins/advanced-custom-fields/

    Then I created the additional tab with this code;

    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    	
    	// Adds the new tab
    	
    	$tabs['test_tab'] = array(
    		'title' 	=> __( 'Instructor', 'woocommerce' ),
    		'priority' 	=> 50,
    		'callback' 	=> 'woo_new_product_tab_content'
    	);
    
    	return $tabs;
    
    }

    Then to display the content of the custom field I used;

    function woo_new_product_tab_content()  {
        // The new tab content
        $prod_id = get_the_ID();
        echo'<p>'.get_post_meta($prod_id,'instructors',true).'</p>';
    }

    With instructors being the custom field I created.

    Now when I add content to this custom field on the product, it will only show the content for that product and only on the product.


    Link to image: https://cld.wthms.co/4ztObb

    Thread Starter kunstudios

    (@kunstudios)

    Hi!

    Thanks for getting back to me so quick. I was hoping to cross reference the taxonomy of instructor with the products (courses in this case) so that the client just needs to put the description of the instructor once (in the description field of the custom taxonomy) and then every course with that instructor pulls in the description.

    I can also create a custom post type of instructor and then use ACF to link the post to the product, but I was hoping there was a way to use the taxonomy description as a simpler/more elegant solution.

    thanks for your help,
    Krisztina.

    Plugin Support EtienneP a11n

    (@etiennep)

    Ah, I understand now! Sorry for the misunderstanding!

    You will need to check the product for which instructor is added to it then echo the relevant description for that instructor.

    Here is my final code that worked on my test site;

    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    	
    	// Adds the new tab
    	
    	$tabs['test_tab'] = array(
    		'title' 	=> __( 'Actor', 'woocommerce' ),
    		'priority' 	=> 50,
    		'callback' 	=> 'woo_new_product_tab_content'
    	);
    
    	return $tabs;
    
    }
    
       function woo_new_product_tab_content() {
        $terms = get_the_terms( $post->ID, 'actors');                           
    if ( $terms ) {
            foreach ( $terms as $term ) {
            $termid = 'actors' . ($term->term_id);
            echo $term->description;                                
        }
    }
    }

    In my case, I used Actors, but you can just change all references of actors to the slug of your custom taxonomy

    Plugin Support EtienneP a11n

    (@etiennep)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Term Description Syntax Help’ is closed to new replies.