Php Wordpress在表中显示帖子,否则echo<;部门>;

Php Wordpress在表中显示帖子,否则echo<;部门>;,php,wordpress,loops,custom-post-type,Php,Wordpress,Loops,Custom Post Type,在表格中显示mycustom\u post\u type中的帖子时遇到问题 当前使用 <table> <thead> <tr> <th>Title</th> </tr> </thead> <tbody> <?php $query = new WP_Query( array( 'post_type' => 'custom_post_type' ) );?>

在表格中显示my
custom\u post\u type
中的帖子时遇到问题

当前使用

<table>
<thead>
    <tr>
        <th>Title</th>
    </tr>
</thead>
<tbody>
<?php $query = new WP_Query( array( 'post_type' => 'custom_post_type' ) );?>
<?php if (have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <tr>
        <td>
        <?php the_title(); ?>
        </td>
    </tr>
<?php endwhile; ?>
<?php else: echo "<tr>" . __( "Sorry, there are no posts." ) . "</tr>";endif; ?>
</tbody>

标题

当一篇文章存在时,这将正确显示每篇文章的标题。但是,当不存在帖子时,它不会显示
else:echo

此图显示了使用POST的表,我从上面的代码中删除了列以简化所要求的内容

没有帖子-没有消息


谢谢你抽出时间

对于自定义查询,您需要使用
Wp\u Query
对象来检查
have\u posts()

只要换个新的

if (have_posts()) :

更多信息

额外建议


在while循环之后,添加还原全局$post变量

为什么要这样做?除了表格数据,使用表格做任何事情都是不好的做法。谢谢你,纳撒尼尔,我在上面添加了一些截图来展示。你能建议另一种方法吗?该页面显示每个订阅者创建的活动帖子。在这里,他们可以编辑、查看或查看自己拥有的每个活动的报告。干杯。啊,你使用的是表格数据。是的,为了让问题简单,我刚刚删除了大多数。我已经阅读了很多你对其他问题的回答,随着时间的推移,这些回答有助于回答问题。感谢您抽出时间回答我的问题。太棒了,谢谢您的反馈@DarrenMathers还添加了wp_reset_postdata();过一会儿谢谢你,安克尔。是的。
if ($query->have_posts()) :