2017-09-26 109 views
1

我正在开发一个wordpress主题,并希望显示一个名为'Artikel'的特定类别的帖子。但是,我的代码不起作用。这是我的代码:如何通过catogory过滤帖子?

<?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(); 
    } 

    ?> 

有谁知道我错过了什么?

+0

您正在从wordpress中获取数据默认的帖子类型和类别? –

回答

0

没有使用常用的类别编号类别名称

$category_id=1; 

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

我会做这样的:

<?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.] 
    ?> 
+0

不工作..不显示任何东西 –

0

请使用此代码,以显示从特定类别的职位。

<?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); ?>