2015-04-01 107 views
0

我試圖從帖子類別與頁面標題相匹配的多個頁面上的自定義帖子類型「文章」中列出帖子。所以在產品頁面上會列出所有類別產品的文章,評論頁面會列出所有類別評論文章的列表等。顯示帖子類型,其中類別等於頁面標題

當我使用category_name時,沒有帖子返回,所有帖子沒有它返回。

<?php 
    // Get articles that have a category that matches the page title (ie: On pagination page get articles with pagination category) 
    $pageTitle = get_the_title(); 

    $postType = 'article'; 

    $args= array(
     'post_type' => $postType, 
     'post_status' => 'publish', 
     'category_name' => $pageTitle 

    ); 

    $articles = new WP_Query($args); 

    if($articles->have_posts()) { 
     while ($articles->have_posts()) : $articles->the_post(); ?> 
     <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
      <p class="tags"><?php the_category()?></p> 

     <?php 
     endwhile; 
    } 
    wp_reset_query(); // Restore global post data stomped by the_post(). 
    ?>     

回答

0

'category_name'參數要求您使用類別slug,而不是名稱。您可以從名稱中獲取類別ID ...

$catID = get_cat_ID($pageTitle); 

...然後在查詢中使用'cat'參數。

'cat' => $catID, 
相關問題