Support » Developing with WordPress » Output the post taxonomy values in WordPress

  • First of all I should say that I’m a beginner, I want to apologize for all the mistakes and my ignorance in coding.

    I’m trying to create a custom taxonomy and assign a value for each post, I’d like to output all the values of the taxonomy and mark the assigned one.

    This is the code that I’ve tried to use:

    if (has_term( 'A1', 'levels' )) {
    	echo '<div class="level"><p class="level-title">level</p><p><mark>'?> <?php the_terms( $post->ID, 'levels'); echo '</mark> A2 B1 B2 C1 C2</p></div>';
    }else{echo "";}
    if (has_term( 'A2', 'levels' )) {
    	echo '<div class="level"><p class="level-title">level</p><p>A1 <mark>'?> <?php the_terms( $post->ID, 'levels'); echo '</mark> B1 B2 C1 C2</p></div>';
    }else{echo "";}
    if (has_term( 'B1', 'levels' )) {
    	echo '<div class="level"><p class="level-title">level</p><p>A1 A2 <mark>'?> <?php the_terms( $post->ID, 'levels'); echo '</mark> B2 C1 C2</p></div>';
    }else{echo "";}
    if (has_term( 'B2', 'levels' )) {
    	echo '<div class="level"><p class="level-title">level</p><p>A1 A2 B1 <mark>'?> <?php the_terms( $post->ID, 'levels'); echo '</mark> C1 C2</p></div>';
    }else{echo "";}
    if (has_term( 'C1', 'levels' )) {
    	echo '<div class="level"><p class="level-title">level</p><p>A1 A2 B1 B2 <mark>'?> <?php the_terms( $post->ID, 'levels'); echo '</mark> C2</p></div>';
    }else{echo "";}
    if (has_term( 'C2', 'levels' )) {
    	echo '<div class="level"><p class="level-title">level</p><p>A1 A2 B1 B2 C1 <mark>'?> <?php the_terms( $post->ID, 'levels'); echo '</mark></p></div>';
    }else{echo "";}

    but it seems is not working appropriately, if I turn on the WP_DEBUG, it returns the following messages:

    Notice: Undefined variable: post in /home/customer/www/englishwithfrancesca.com/public_html/wp-content/themes/storefront-child/storefront-template-functions.php on line 468

    Notice: Trying to get property ‘ID’ of non-object in /home/customer/www/englishwithfrancesca.com/public_html/wp-content/themes/storefront-child/storefront-template-functions.php on line 468

    I bet there’s a smarter way to code it.

    Thanks in advance for any help!

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • You didn’t say where that code is or when it is executed, but WP has a function the_taxonomies() which can take parameters for how to output the terms.
    See https://developer.wordpress.org/reference/functions/the_taxonomies/

    What the error message is telling you is that you are using $post which is undefined. If you want to use the global variable by that name, you have to use
    global $post;

    Thread Starter Terra Adopt

    (@terra-adopt)

    Thank you for your help @joyously !

    • I made a copy of storefront-template-functions.php (from the storefront theme) in my child theme: I’ve added my own code to output some post meta info in the <aside> tag.
    • I’ve tried to follow your advise using the_taxonomies() but it shows me the following error message:

      Fatal error: Cannot redeclare the_taxonomies() (previously declared in …/public_html/wp-includes/taxonomy.php:4465) in …/public_html/wp-content/themes/storefront-child/storefront-template-functions.php on line 454

    • I think my code is too long and clunky, maybe I should use something like str_replace calling the taxonomy value and adding the <mark> tags before and after.

    I’m a real beginner to PHP and wordpress code, sorry if these questions are stupid, I still have lots to learn

    • This reply was modified 2 days, 13 hours ago by Terra Adopt.

    In PHP, each function has to have a unique name or it wouldn’t know which one to execute. So you can’t name your function the_taxonomies because WordPress already has a function by that name. You should call WordPress’ function though, and pass it the parameters to make it do what you want. Read that page I linked to understand it (also the function that it calls has more information on the parameters).

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.