1

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?

1 Answer 1

3

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:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.