How to display CPT Category & Tag title on archive pages as headings?

#451948
  • Resolved Cosmin
    free

    I’ve set Rank Math’s Titles and Meta for each CTP main archive, category and tags. They display correctly inside the <title> tags for each page.

    Is there a way to show the same titles on the page? I’ve tried:

    <?php echo get_the_title(); ?>
    <?php the_title(); ?>

    in the header.php and also tried using a shortcode in functions.php

    add_shortcode('rm_title', 'rm_shortcode_title');
    function rm_shortcode_title(){
    $obj = get_queried_object();
    if($obj instance of WP_Post){
    return get_post_meta($obj->ID, 'rank_math_title', true);
    }

    if($obj instance of WP_Term){
    return get_term_meta($obj->term_id, 'rank_math_title', true);
    }
    }

    But what I get is what the plugin that created these CPTs generates.

    Any suggestions?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Thank you for reaching out to us and bringing your concern to our attention.

    You need to process the retrieved variables from the database. To do that, you need our replace_vars helper function. Please refer to this code instead:

    ...
      return RankMath\Helper::replace_vars(YOUR_TITLE_FROM_RM_META_HERE);

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hello Jeremy, and thank you for the quick reply.

    Unfortunately, I’m not sure I understand.

    Are you referring to this page: https://rankmath.com/kb/filters-hooks-api-developer/ under “Use the Description from Global Setting, if the description is missing in the Post metabox”?

    Hello,

    yes, the replace_vars function is used in the article you refered to.

    The example snippet below shows how your shortcode would look with the replace_vars function:

    
    add_shortcode('rm_title', 'rm_shortcode_title');
    function rm_shortcode_title(){
    	$obj = get_queried_object();
    	if($obj instance of WP_Post){
    		$title =get_post_meta($obj->ID, 'rank_math_title', true); 
    		return RankMath\Helper::replace_vars($title);
    	}
    }
    

    Hope that helps. Please let us know if you have questions.

    Hi Nigel, thank you for your comprehensive reply.

    Unfortunately, I’m getting an error on:

    if($obj instance of WP_Post){

    Call stack:

    `require_once()
    wp-config.php:129
    require_once()
    wp-load.php:50
    require_once()
    wp-blog-header.php:13
    require()
    index.php:17`

    Hello,

    I’m sorry I assumed you knew that the if($obj instance of WP_Post){ condition was a placeholder. I’m not sure what the exact condition that should be placed there is, but the following revised snippet should work:

    
    add_shortcode('rm_title', 'rm_shortcode_title');
    	function rm_shortcode_title(){
    	$obj = get_queried_object();
    	if(is_single()){
    		$title =get_post_meta($obj->ID, 'rank_math_title', true); 
    		return RankMath\Helper::replace_vars($title);
    	}
    	if(is_tax()){
    		$title = get_term_meta($obj->term_id, 'rank_math_title', true);
    		return RankMath\Helper::replace_vars($title);
    	}
    }
    

    Hope that helps. Please let us know if you have questions.

    You are awesome! That did the trick! Silly me 🙂

    Is there also a way to use the global titles (the ones set in Rank Math > Titles & Meta > Post Types > CPT Archive Title and in Rank Math > Titles & Meta > CPT > CPT Categories)?

    Hello,

    The code will work for the title set for the post or category. If you are using the global title set in the titles & meta settings then the code with output that for the shortcode.

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

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

You must be logged in to reply to this ticket.