我在具有photos
的自定义帖子类型的WordPress站点中工作。我添加了一个自定义分类呼叫者category
。我想用URL显示自定义category
名称。但是由于某种原因,它显示为空白。我该如何修复?这是我尝试的代码
<?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; ?>
如果您的问题是为什么在while循环中出现" DIV"标签,这是由于未满足时循环的条件。您的一个或多个论点正在返回错误。
<?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
正在搜索查看是否有适合您指定的标准的帖子,"照片"one_answers"已发布"状态的帖子类型。我认为它没有找到这些条件,并且以错误的值返回。
更多信息:
wp_query
我认为问题与您的自定义分类名称category
,因为category
已经与POST类型post
关联。因此,您将获得空白输出。
如何修复 ::重命名 category
to photos_cat
(或带有您愿望的任何名称)。
希望这会有所帮助!