I am using the parameter {cf_rank_math_primary_product_cat} in query loop of my product archive to call on the Primary Category set with RankMath plugin, but it only returns the ID of the category. I want the name of the category instead. How do I do this?
Put this in your functions.php
/**
* Fetched category by id
*/
function get_primary_rankmath_category_rh($rhid, $is_link = false)
{
$term = get_term_by( 'id', $rhid, 'category' );
return $is_link ? get_term_link($term->slug, 'category') : $term->name;
}
add_filter('bricks/code/echo_function_names', function () {
return [
'get_primary_rankmath_category_rh',
];
});
Now, go to bricks and use text link element inside a query loop.
For name use {echo:get_primary_rankmath_category_rh({cf_rank_math_primary_category})}
For link select dynamic data and use {echo:get_primary_rankmath_category_rh({cf_rank_math_primary_category}, true)}
1 Like
You need more error checking.
function get_rankmath_primary_category_rh($rhid = null, $is_link = false)
{
if ( empty( $rhid ) ) {
return null;
}
$term = get_term_by( 'id', $rhid, 'category' );
if ( $term && !is_wp_error($term) ) {
if ($is_link) {
return get_term_link($term->slug, 'category');
} else {
return $term->name;
}
} else {
return null;
}
}
add_filter('bricks/code/echo_function_names', function () {
return [
'get_rankmath_primary_category_name_rh',
];
});

