• Resolved timothy12

    (@timothy12)


    I have this code snippet

    <?php
    // Import necessary libraries
    require_once('wp-load.php');

    $post_id = get_the_ID(); // Get the current post ID
    $primary_category = get_post_meta($post_id, '_yoast_wpseo_primary_category', true);
    $categories = get_the_category($post_id);

    if ($primary_category === 'Prezi' || $primary_category === 'Kahoot') {
    echo 'A';
    } elseif (count($categories) === 1 && ($categories[0]->slug === 'Prezi' || $categories[0]->slug === 'Kahoot')) {
    echo 'B';
    } else {
    echo 'C';
    }
    ?>

    What it should do (and doesn’t at the moment)

    ;)

    is getting the primary category of the post. That primary category is created by the Yoas SEO plugin. When the primary category is “Prezi” or “Kahoot”, it should echo ‘A’. Wanneer there is just one category selected (and the isn’t a primary one) and that category is either “Prezi” or “Kahoot”, it should echo dan ‘B’. If the category isn’t “Prezi” or”Kahoot”, ‘C’ should be echoed.

    In my case, only ‘C’ is echoed, while that categories in the posts are properly set. Is there an error in the code I don’t see?

    Earlier, I had this code snippet, but is showed the parent category, always the first selected category of a post. If this code snippet could be adapted by getting the primary category and echoing ‘A’ of ‘C’, I’m happy too. Part ‘B’ isn’t that important.

    <?php
    $post_categories = get_the_category($post->ID);
    $parent_category_name = '';

    if (!empty($post_categories)) {
    $parent_category = $post_categories[0]->parent ? get_category($post_categories[0]->parent) : $post_categories[0];
    $parent_category_name = $parent_category->name;
    }

    if ($parent_category_name === 'Prezi' || $parent_category_name === 'Kahoot') {

    echo
    "<span>A</span>";
    }
    else
    {
    echo "<span>C</span>";
    }
    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • “Primary categories” is not something supported by WordPress natively. This is an invention introduced by Yoast, hence, you should go to the Yoast plugin support subforums to ask for this.

    Thread Starter timothy12

    (@timothy12)

    @sirlouen Thanks for your quick reply. Do you think i’ve to copy the post to the Yoast plugin support or can my thread moved toward that forum?

    They are like subforums, so you should copy it.

    Anyway, If I’m not wrong $primary_category = get_post_meta($post_id, '_yoast_wpseo_primary_category', true); provides an ID, not the object. So you should get_term to retrieve the object afterwards and then get the object property accordingly (and I would rather use the categories slugs instead of the names).

    Thread Starter timothy12

    (@timothy12)

    I will copy my my question and replace it in the Yoast SEO section.

    About your comment, you think I should replace the get_post_meta by get_term?

    Thread Starter timothy12

    (@timothy12)

    @sirlouen

    You were right. With the following code snippet I got it working as I wanted. Thanks for your tip! The option B also works when there is just one single category selected. It automatically becomes the primary category,

    <?php                                                

    $primary_term_id = yoast_get_primary_term_id( 'category', $post_id );

    $term = get_term( $primary_term_id ); // Get the term object to access its name

    if ( $term->name == 'Prezi' || $term->name == 'Kahoot' ) {

        echo "A.";

    } else {

        echo "C";

    }

      ?>       


    Moderator t-p

    (@t-p)

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

The topic ‘Echo content based on primary category’ is closed to new replies.