WordPress按标签和类别查询帖子
问题描述:
我正在尝试创建一组WP页,并为其所有带有标签X和类别Y的帖子建立索引。
I'm trying to create a set of WP pages with indices to all posts which have tag X as well as category Y.
我发现此,它似乎完全可以满足我的要求,而且我了解如何为每个索引页面进行编辑,但是我不知道在哪里/如何使用实际代码创建索引页面。
I found this which seems to do exactly what I want and I understand how to edit it for each index page, but I don't know where/how to use the actual code to create the index pages.
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1); //get all posts
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;
TIA寻求帮助。
答
这是最终有效的方法-请注意,我需要类别ID,但标记应该是Slug。
This is what finally worked - note that I needed the category ID but the tag slug.
<?php if ( have_posts() ) : ?>
<?php query_posts( 'cat=6&tag=a1&&orderby=title&order=asc' );?>
<?php while ( have_posts() ) : the_post();?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>