我使用下面的代码列出主页上标准帖子旁边的自定义帖子类型
<?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; ?>
循环时,我应该如何更改第二类帖子的“阅读更多”文本?
最合适的回答,由SO网友:Iceable 整理而成
您可以在条件中检查帖子类型,并在此基础上回显不同的“阅读更多”文本。
你的$query
是一个WP_Query
对象,并具有$post
所有物这是一个WP_Post
当前帖子的$post_type
所有物
您可以使用直接访问它$query->post->post_type
检查类型。
因此,您的代码类似于:
<?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; ?>
参考文献: