Wordpress shows messages in a table, otherwise echo <div>

I am having trouble displaying messages from mine custom_post_typein a table.

Currently used

<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>

And it shows the_titlefor each message correctly when the message exists. However, it does not show else : echowhen messages do not exist.

This image shows a table working with messages, I removed the columns from the above code to simplify the query

With messages

and without messages - no message

No message

Thank you for your time!

+4
source share
1 answer

Wp_Query have_posts()

if (have_posts()) :

if ($query->have_posts()) :

while wp_reset_postdata(); $post

+4

Source: https://habr.com/ru/post/1684192/


All Articles