• Resolved MikkelCrunch

    (@mikkelcrunch)


    Hi,

    our client had this never in mind, but as he saw that he can select a primary category, he starts using more than one cat for each article.

    On the front page I have this code usually to display the category name:

    <?php 
       foreach((get_the_category()) as $category){
    	echo $category->name;
       }
    ?>

    I know that WordPress does or at least did the category order from the lowest ID to the highest.

    Now, of course, I get multiple category names displayed in the frontend, as my client does choose them in the backend.

    So, how can I achieve this, that the (a) category name gets displayed, but if there is a primary from yoast selection, this should be displayed instead?

    Thanks for your Help in advance!

    Best regards
    Mikkel

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Suwash

    (@suascat_wp)

    Hi @mikkelcrunch,

    I assumed that you want to display the primary category (as selected via Yoast SEO) somewhere on the front page (not particularly on the Yoast SEO breadcrumb) of your website. If so, upon doing a quick search on Google, I’ve found this gist code for you which might help you get the expected result.

    Thread Starter MikkelCrunch

    (@mikkelcrunch)

    Hi @suascat_wp,

    I also searched before I posted here, but did not find this solution…

    I will give it a try – thank you very much for this.

    Will answer here if it worked!

    Thanks again,
    Best – Mikkel

    Thread Starter MikkelCrunch

    (@mikkelcrunch)

    Hi @suascat_wp,

    yes it worked!

    Had to change the line:

    echo '<span class="post-category">'.htmlspecialchars($category_display).'</span>';

    to

    echo ($category_display);

    ,because it displayed the “&” as “& a m p ;” in the name.

    Thanks a lot!
    Best Regards – Mikkel

    • This reply was modified 4 years, 4 months ago by MikkelCrunch.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display only primary Category in Theme (Frontend)’ is closed to new replies.