Wordpress query posts by tag and category

。_饼干妹妹 提交于 2019-12-13 01:28:36

问题


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 for your help.


回答1:


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 : ?>



回答2:


You use that code to replace the basic query in the loop in your template.

https://codex.wordpress.org/Function_Reference/query_posts


来源:https://stackoverflow.com/questions/29215181/wordpress-query-posts-by-tag-and-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!