繁体   English   中英

如何按类别过滤帖子?

[英]How do I filter posts by catogory?

我正在开发一个wordpress主题,并希望显示名为“ Artikel”的特定类别的帖子。 但是,我的代码无法正常工作。 这是我的代码:

<?php
    $custom_query = new WP_Query([
      'cat' => 'Artikel',
    ]);
    if ($custom_query->have_posts()) {
      while ($custom_query->have_posts()) {
        $custom_query->the_post(); ?>


        /* do things */

        <?php

      }
      wp_reset_postdata();
    }

    ?>

有人知道我在想什么吗?

已使用类别ID未使用类别名称

 $category_id=1;

    $custom_query = new WP_Query([
          'post_type' => 'post','posts_per_page' => -1,'order' => 'ASC','cat' => $category_id
        ]);

我会这样做:

<?php
$args = array(
                post_type => 'post',
                'category_name' => 'Artikel',
                'posts_per_page' => 3,
                'orderby' => 'date',
                'order' => 'ASC'
            );
    $custom_query = new WP_Query($args);

    if ($custom_query->have_posts()) {
      while ($custom_query->have_posts()) {
        $custom_query->the_post(); 

[etc.]
   ?>

请使用此代码显示特定类别的帖子。

 <?php   $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' =>'4',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'name',    
            'terms'    => 'Artikel'
        ),
    ),
);   
$the_query = new WP_Query( $args );  ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM