【问题标题】:Wordpress query posts by tag and categoryWordpress 按标签和类别查询帖子
【发布时间】:2015-03-23 16:17:43
【问题描述】:

我正在尝试创建一组 WP 页面,其中包含所有具有标签 X 和类别 Y 的帖子的索引。

我发现 this 似乎完全符合我的要求,并且我了解如何为每个索引页面编辑它,但我不知道在哪里/如何使用实际代码来创建索引页面。

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 为您提供帮助。

【问题讨论】:

    标签: wordpress tags categories


    【解决方案1】:

    这终于奏效了——请注意,我需要类别 ID,但需要标签 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】:

      您使用该代码替换模板中循环中的基本查询。

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

      【讨论】:

      • 谢谢。如果我理解正确,那将是我的索引页,对吧?我不想更改索引页上的查询;我想创建一组单独的页面:一个带有标签X和类别Y查询结果的页面,一个带有标签X和类别Z查询结果的页面等
      • 是的,您可以在您的索引页面上执行此操作。但是您也可以在运行循环的任何页面上执行此操作。听起来您想要一系列模板,每个模板都修改了循环以根据需要返回帖子。
      • 我在您给我的页面上找到了一个更简单的查询,但我没有得到任何结果。知道可能出了什么问题吗? &lt;?php /* Template Name: A1 Grammar */ get_header(); ?&gt; &lt;div class="row"&gt; &lt;div class="col-md-8 content-area" role="main"&gt; &lt;?php // The Query query_posts( 'cat=6&amp;tag=69' ); // The Loop while ( have_posts() ) : the_post(); echo '&lt;li&gt;'; the_title(); echo '&lt;/li&gt;'; endwhile; // Reset Query wp_reset_query(); ?&gt;
      • 您不会像粘贴在这里那样运行所有代码,对吧?你需要确保所有的php代码都没有与“//”在一行,其实只要取出“// The Query” “// The Loop” “// Reset Query” cmets即可。请记住,您需要创建一个使用“A1 语法”模板的页面。
      • 正确 - 当我将它粘贴到这里时,它全部运行在一起,我没有费心添加所有换行符。是的,我创建了一个页面,但没有显示任何帖子: lawlessfrench.com/verb-conjugations/a1-grammar
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多