yi100
Forum Replies Created
Viewing 1 replies (of 1 total)
-
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 '';
}
Viewing 1 replies (of 1 total)