• Resolved yi100

    (@yi100)


    Hello, I would like to know how to automatically display the titles of the latest few posts in the meta description for a category. I couldn’t find a relevant variable for this.

    For example, if the latest three post titles in Category A are 1, 2, and 3, I would like the meta description to automatically display “1, 2, 3.”

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @yi100,

    Thank you for contacting Rank Math support.

    You can use the following filter to use the latest post titles as the category description:

    add_filter('rank_math/frontend/description', function ($description) {
    if (is_category()) {
    $cat = get_queried_object_id();
    $myposts = get_posts(array('numberposts' => 3, 'offset' => 0, 'category__in' => array($cat), 'post_status'=>'publish', 'order'=>'DESC' ));
    $titles = [];
    foreach ($myposts as $post) {
    $titles[] = get_the_title($post->ID);
    }
    return $titles[0]. ', ' .$titles[1]. ', '.$titles[2];
    }
    return $description;
    });

    Here is how to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

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

    Thread Starter yi100

    (@yi100)

    Thank you very much. However, I would like to add a new variable while retaining the old one, so that both can be used together. I asked ChatGPT to write a piece of code for this, but I’m not sure if it’s correct.

    add_action('rank_math/vars/register_extra_replacements', function () {

    ??? rank_math_register_var_replacement(

    ??????? 'category_titles',

    ??????? [

    ??????????? 'name'??????? => esc_html__('Category Titles', 'rank-math'),

    ??????????? 'description' => esc_html__('Displays the titles of the latest posts in the current category', 'rank-math'),

    ??????????? 'variable'??? => 'category_titles',

    ??????????? 'example'???? => 'Post Title 1, Post Title 2, Post Title 3, ...',

    ??????? ],

    ??????? 'get_category_titles'

    ??? );

    });

    function get_category_titles() {

    ??? if (is_category()) {

    ??????? $cat = get_queried_object_id();

    ??????? $myposts = get_posts(array(

    ??? ????????'numberposts' => 10,

    ??????????? 'offset' => 0,

    ??????????? 'category__in' => array($cat),

    ??????????? 'post_status' => 'publish',

    ???? ???????'order' => 'DESC'

    ??????? ));

    ??????? $titles = [];

    ??????? foreach ($myposts as $post) {

    ??????????? $titles[] = get_the_title($post->ID);

    ??????? }

    ??????? return implode(', ', $titles);

    ??? }

    ??? return '';

    }
    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @yi100,

    Here is a filter to create a variable to output the title of the three latest posts of a category:

    add_action('rank_math/vars/register_extra_replacements', function () {
    rank_math_register_var_replacement(
    'post_titles',
    [
    'name' => esc_html__('Post Titles', 'rank-math'),
    'description' => esc_html__('Title of posts', 'rank-math'),
    'variable' => 'post_titles',
    'example' => post_titles_callback(),
    ],
    'post_titles_callback'
    );
    });
    function post_titles_callback() {
    if (is_category()) {
    $cat = get_queried_object_id();
    $myposts = get_posts(array('numberposts' => 3, 'offset' => 0, 'category__in' => array($cat), 'post_status'=>'publish', 'order'=>'DESC' ));
    $titles = [];
    foreach ($myposts as $post) {
    $titles[] = get_the_title($post->ID);
    }
    return implode(', ', $titles);
    }
    }

    You can use the %post_titles% variable after adding the filter.

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.