Custom meta description if have custom template for posts

#505439
  • Resolved Puneet
    Rank Math free

    Hello,
    I want to insert custom meta description automatically for some posts which have custom-template.php.

    I check the code change meta description (PHP Code), but how to set condition for template check and insert value in description variable.

    
    add_filter( 'rank_math/frontend/description', function( $description ) {
    	return $description;
    });
Viewing 2 replies - 1 through 2 (of 2 total)
  • Puneet
    Rank Math free

    *update

    The condition is now working but how to define $description if have custom field value also.

    add_filter( 'rank_math/frontend/description', function( $description ) {
    	if ( is_single() && 'post' == get_post_type() && get_post_meta( get_the_ID(), 'Color', true ) ) {
    $description = '{
    ?>
    <?php echo get_post_meta(get_the_ID(),'Color',true); ?> will trend in <?php echo get_the_date('Y');?>
    <?php
     }';
        return $description;
    });

    This is notworking yet and throwing error.

    Nigel
    Rank Math business

    Hello,

    Thank you for contacting Rank Math for help with setting a custom meta description on your website.

    If I understand correctly, you would like the meta description to display something like: Gold will trend in 2023

    I have edited the filter to create a description as above:

    
    
    add_filter( 'rank_math/frontend/description', function( $description ) {
    	if ( is_single() && 'post' == get_post_type() && get_post_meta( get_the_ID(), 'Color', true ) ) {
    		$description =  get_post_meta(get_the_ID(),'Color',true). " will trend in ".get_the_date('Y');
    	};
        return $description;
    });
    

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

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Custom meta description if have custom template for posts’ is closed to new replies.