我已经搜索了上下,但一直无法找到一个解决我的问题。希望这不是一个重复的问题,我无法找到通过搜索这里和谷歌。
我试图让wp_query返回一组结果(10),这些结果由当前页面类别中发现的任何帖子填充。我可以用……
$postCategories = get_the_category();
$atts = array (
'posts_per_page' => 10,
'tag' => 'sticky',
'category_name' => $postCategories[0]->slug,
);
但是我遇到麻烦的地方是标签。我希望任何帖子,有标签"粘"采取优先级的任何帖子正在带来的类别匹配,而不添加任何超过10个结果仍然。
任何帮助或指导将非常感激,因为我是一种php新手。由于
我认为这可能对你有用,但在不了解项目细节的情况下很难确定。
<ul>
<?php $sticky = get_option( 'sticky_posts' ); // Get sticky posts ?>
<?php $args_sticky = array(
'post__in' => $sticky,
'posts_per_page' => 10, // Limit to 10 posts
'ignore_sticky_posts' => 1
); ?>
<?php $sticky_query = new WP_Query( $args_sticky ); ?>
<?php $sticky_count = count($sticky); // Set variable to the number of sticky posts found ?>
<?php $remaining_posts = 10 - count($sticky); // Determine how many more non-sticky posts you should retrieve ?>
<?php if ($sticky_count > 0) : // If there are any sticky posts display them ?>
<?php while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<li><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php if ($remaining_posts > 0) : // If there are non-sticky posts to be displayed loop through them ?>
<?php $postCategories = get_the_category(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => $remaining_posts,
'post__not_in' => get_option( 'sticky_posts' ),
'category_name' => $postCategories[0]->slug
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php endif; ?>
</ul>