2017-09-26 146 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); ?>