WordPress按标签和类别查询帖子 [英] Wordpress query posts by tag and category
本文介绍了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 : ?>
这篇关于WordPress按标签和类别查询帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文