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.
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
<?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 3 years, 10 months ago by
caroline.
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.
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.
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>';?>
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://ww.wp.xz.cn/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.