最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Yoast primary category query modification

programmeradmin2浏览0评论

I have a problem with Yoast and displaying my primary category alongside rest of the posts from that category.

Let's say I have 4 posts, all of them are lorem category and one post has set lorem as primary category. Currently the lorem category page (/category/lorem) displays only the three posts that aren't set as primary of its category.

I have in my category.php a simply WP query:

<?php $current_category = single_cat_title('', false); ?>

<?php
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

  $args = array(
    'category_name' => $current_category,
    'paged' => $paged
  );

  $query = new WP_Query($args);
?>

<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
  /* do stuff */
<?php endwhile; endif; ?>

<?php previous_posts_link(); ?>
<?php next_posts_link(); ?>

How can I modify the current WP query to also include the primary category alongside with pagination?

Thanks

I have a problem with Yoast and displaying my primary category alongside rest of the posts from that category.

Let's say I have 4 posts, all of them are lorem category and one post has set lorem as primary category. Currently the lorem category page (/category/lorem) displays only the three posts that aren't set as primary of its category.

I have in my category.php a simply WP query:

<?php $current_category = single_cat_title('', false); ?>

<?php
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

  $args = array(
    'category_name' => $current_category,
    'paged' => $paged
  );

  $query = new WP_Query($args);
?>

<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
  /* do stuff */
<?php endwhile; endif; ?>

<?php previous_posts_link(); ?>
<?php next_posts_link(); ?>

How can I modify the current WP query to also include the primary category alongside with pagination?

Thanks

Share Improve this question asked Jun 13, 2019 at 15:20 VuckoVucko 1631 gold badge1 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I'm not sure this will fix it, but maybe try using the current category slug for the $current_category variable, instead of single_cat_title(), like so:

   $term = get_queried_object();
   $current_category = $term->slug;

This might help because the category_name parameter in WP_Query, despite its name, should be the term slug. single_cat_title() returns the displayed category title, not slug.

发布评论

评论列表(0)

  1. 暂无评论