2017-02-27 53 views
0

我正在一個WordPress網站中工作,該網站的自定義帖子類型爲photos。我添加了一個自定義分類標準調用程序category。我想用URL顯示自定義名稱category。但由於某種原因,它顯示空白。我該如何解決它? 這裏是我的代碼我想在WordPress網站中顯示帶有URL的自定義帖子類型分類

<?php 
$args = array(
    'post_type' => 'photos', 
    'post_status' => 'publish', 
    'paged' => get_query_var('paged') 
); 
$the_query = new WP_Query($args); 

?> 

<?php 
if ($the_query->have_posts()): 

    while ($the_query->have_posts()): 
     $the_query->the_post(); 
     ?> 

     <div>Meal type: <?php echo get_the_term_list($post->ID, 'category', '', ', ', ''); ?></div>      

    <?php endwhile; ?> 
    <?php wp_reset_postdata(); ?> 

<?php else: ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 

回答

1

如果你的問題是,爲什麼在while循環的「格」標籤顯示爲空白,這是因爲沒有滿足while循環的條件。一個或多個參數返回false。

<?php 
$args = array(
    'post_type' => 'photos', 
    'post_status' => 'publish', 
    'paged' => get_query_var('paged') 
); 
$the_query = new WP_Query($args); 

?> 

<?php 
if ($the_query->have_posts()): 

    while ($the_query->have_posts()): 
     $the_query->the_post(); 
     ?> 

從本質上講,WP_Query呼籲多類,如WP_Tax_Query和WP_Meta_Query及以下,根據您所提供的標準在做SQL請求:它的搜索,看是否有帖子

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts 
WHERE 1=1 AND wp_posts.post_type = 'photos' AND ((wp_posts.post_status = 'publish')) 
ORDER BY wp_posts.post_date DESC LIMIT 0, 10 

那符合您指定的標準,發佈類型爲「照片」和「已發佈」狀態。我會假設它沒有找到這些標準,並返回false值。

的更多信息:

WP_QUERY

+0

可否請您解釋一下。謝謝 –

+0

爲您增加了更多信息。 – AmarB

+0

@AmarB如果他們不是帖子的作者,最好標記他人。因爲你是特定問題的提問者,所以我實際上並不需要給你加標籤,但是想告訴你如何。另外**非常好的工作**在你編輯這個問題。 –

0

我認爲這個問題是你的自定義分類名稱category,因爲category已與交型post相關。所以你得到一個空白輸出。

如何解決 ::重命名categoryphotos_cat(或你的願望的任何名稱)

希望這會有所幫助!

相關問題