2017-02-27 54 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(或你的愿望的任何名称)

希望这会有所帮助!

相关问题