• Resolved caroline

    (@makeupedia)


    Thanks to @marcorroma I managed to get the primary category! However, the link is missing. How can I add it to this code?

     <?php $category = get_the_category(); $category[0]->cat_name; $category_link = get_category_link( $category[0] ); ?>
    <?php echo $category[0]->cat_name; ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @makeupedia,

    Thank you for contacting support.

    This question is not related to Rank Math but rather the core WordPress functionality.

    Having said that, it seems that you are getting the category link saved in the variable $category_link, so you need to echo that variable on the page to get the link of the category.

    Don’t hesitate to get in touch if you have any questions about our plugin.

    Thread Starter caroline

    (@makeupedia)

    Thank you @rankmathteam !

    I changed it, but it only echoes the full URL and not a link.

    Do you have a better solution to display the rank math primary category?

    Thank you!
    /Caroline

    Thread Starter caroline

    (@makeupedia)

    <?php $category = get_the_category(); $category_link[0]->cat_name; $category_link = get_category_link( $category[0] ); ?>
    <?php echo '<a href="'.esc_url( $category_link ).'" title="'.esc_attr($category).'">'.$category.'</a>';  ?></span

    So I tweaked the code, but now it says ARRAY.

    • This reply was modified 2 years, 3 months ago by caroline.
    Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @makeupedia,

    To output this as a link with the category name, you need to add the HTML markup as well as the data from the variables.

    You can try something similar to this:

    
    echo '<a href="' . $category_link . '">';
    echo $category[0]->cat_name;;
    echo '</a>';
    

    Please note that you might need to perform additional changes to this code depending on how you need the anchor tag to appear on the page.

    Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @makeupedia,

    Also, you have called the $category_link variable to get the category name. Please try this filter:

    <?php
    $categories = get_categories();
    $category_link = get_category_link($categories[0]->cat_ID);
    echo '<a href="'.esc_url( $category_link ).'" title="'.esc_attr($categories[0]->name).'">'.$categories[0]->name.'</a>';?>

    Hope this helps. Let us know if you need any other assistance.

    Thread Starter caroline

    (@makeupedia)

    Thank you so much @rankmathteam
    I didn’t manage to get it worked with the filter you provided though!
    This is working for me:

    <?php $category = get_the_category(); $category_link[0]->cat_name; $category_link = get_category_link( $category[0] ); ?>
    <?php echo '<a class="primary-cat" href="' . $category_link . '">';
    echo $category[0]->cat_name;
    echo '</a>';?>
    Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @makeupedia,

    Glad that you were able to sort it out in the end.

    Don’t hesitate to get in touch if you need any assistance with Rank Math in the future.

    If it isn’t too much to ask for – would you mind leaving us a review here?
    https://www.ads-software.com/support/plugin/seo-by-rank-math/reviews/#new-post

    It only takes a couple of minutes but helps us tremendously.

    It would mean so much to us and would go a really long way.

    Thank you.??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display Primary Category link’ is closed to new replies.