Different string for specifed post type on posts listing at homepage

I use below code to list custom post types on homepage beside standard posts

    ?php $args = array('post_type' = array( 'post', 'gallery_posts' )); ?
    ?php $query = new WP_Query( $args ); ?
    ?php if ( $query-have_posts() ) : ?
        ?php while ( $query-have_posts() ) : $query-the_post(); ?
            ?php _e('Read More', ''); ?
        ?php endwhile; ?
    ?php endif; ?

What i should to do for change 'Read more' text for second type of post while looping?

Topic listing wp-query query Wordpress

Category Web


You can check the post type in a conditional and echo a different 'read more' text based on this.

Your $query is a WP_Query object and has a $post property. This is a WP_Post object of the current post and it has a $post_type property.

You can access it directly with $query->post->post_type to check the type.

So your code would be something like:

<?php $args = array('post_type' => array( 'post', 'gallery_posts' )); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <?php if ( 'post' === $query->post->post_type ) : ?>
            <?php _e('Read More', ''); ?>
        <?php elseif ( 'gallery_posts' === $query->post->post_type ) : ?>
            <?php _e('View More', ''); ?>
       <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>

References:

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.