主页上列出的帖子上指定帖子类型的不同字符串

时间:2018-02-06 作者:X9DESIGN

我使用下面的代码列出主页上标准帖子旁边的自定义帖子类型

    <?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; ?>
循环时,我应该如何更改第二类帖子的“阅读更多”文本?

1 个回复
最合适的回答,由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; ?>
参考文献:

结束

相关推荐

ACF Query Structure

不知道是否有人能教我一些知识。我有一个显示特征图像和链接等的查询,但我想在其中添加一个,如果存在视频,请使用该代码而不是特征图像。所以我想我可以在ACF插件中添加一个带有嵌入代码或视频链接的自定义字段,我正在使用该插件,但不确定如何在现有查询中构造查询,这有意义吗?<?php // the query $the_query = new WP_Query( array( \'category\' => \'Publish\', \'c