【问题标题】:Post title is duplicating if have_posts如果 have_posts 则帖子标题重复
【发布时间】:2019-12-08 00:56:52
【问题描述】:

我正在尝试从自定义帖子类别中添加一些帖子标题。我目前让它打印正确数量的<li>,但不幸的是它是同名的。你会看到我使用 meta 缩小了范围,所以我得到了 5 个同名的结果。

我不假装是这方面的专家,所以我虚心地向社区寻求任何帮助。

提前致谢。

我试过做一个 foreach 和 if have_posts。两者都产生了相同的结果。

$page_title = get_the_title();

$args = array(
        'orderby' => 'title',
        'post_type' => 'person',
        'post_status' => 'publish',
        'meta_key' => 'division',
        'meta_value' => 'Singles'
    );

    $string = '';
    $query = new WP_Query( $args );
    if( $query->have_posts() ):
        $string .= '<ul>';
            while( $query->have_posts() ):
                $query->the_post();
                $string .= '<li>' . get_the_title() . '</li>';
            endwhile;
        $string .= '</ul>';
            echo $string;
            wp_reset_postdata();
    else :
    // No, we don't have any posts, so maybe we display a nice message
    echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
    endif;

所以我们要做的是查找“Singles”中存在多少帖子(在我的情况下是 5 个),并在 &lt;li&gt; 中打印每个帖子的标题。目前它打印 5 次中的 1 次。

【问题讨论】:

    标签: php wordpress post while-loop duplicates


    【解决方案1】:

    get_the_title() 有以下实现

    function get_the_title( $post = 0 ) {
        $post = get_post( $post );
    
        $title = isset( $post->post_title ) ? $post->post_title : '';
        $id    = isset( $post->ID ) ? $post->ID : 0;
    
        if ( ! is_admin() ) {
            if ( ! empty( $post->post_password ) ) {
    
                /**
                 * Filters the text prepended to the post title for protected posts.
                 *
                 * The filter is only applied on the front end.
                 *
                 * @since 2.8.0
                 *
                 * @param string  $prepend Text displayed before the post title.
                 *                         Default 'Protected: %s'.
                 * @param WP_Post $post    Current post object.
                 */
                $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
                $title                  = sprintf( $protected_title_format, $title );
            } elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
    
                /**
                 * Filters the text prepended to the post title of private posts.
                 *
                 * The filter is only applied on the front end.
                 *
                 * @since 2.8.0
                 *
                 * @param string  $prepend Text displayed before the post title.
                 *                         Default 'Private: %s'.
                 * @param WP_Post $post    Current post object.
                 */
                $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
                $title                = sprintf( $private_title_format, $title );
            }
        }
    
        /**
         * Filters the post title.
         *
         * @since 0.71
         *
         * @param string $title The post title.
         * @param int    $id    The post ID.
         */
        return apply_filters( 'the_title', $title, $id );
    }
    

    如您所见,您可以将帖子传递给它,然后它将获得其标题。由于您没有将帖子传递给它,因此它没有为您获得标题。建议:

    $page_title = get_the_title();
    
    $args = array(
            'orderby' => 'title',
            'post_type' => 'person',
            'post_status' => 'publish',
            'meta_key' => 'division',
            'meta_value' => 'Singles'
        );
    
        $string = '';
        $query = new WP_Query( $args );
        if( $query->have_posts() ):
            $string .= '<ul>';
                while( $query->have_posts() ):
                    $string .= '<li>' . get_the_title($query->the_post()) . '</li>';
                endwhile;
            $string .= '</ul>';
                echo $string;
                wp_reset_postdata();
        else :
        // No, we don't have any posts, so maybe we display a nice message
        echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
        endif;
    

    【讨论】:

    • 非常感谢您的回复。它实际上产生了相同的结果。它确实在为我打印帖子标题。问题是它打印相同的帖子标题 5 次。我们正在查看的元值中有 5 个帖子。它返回 5 个中的 1 个并打印 1、5 次不同的时间。不幸的是,您的代码产生的结果与我原来的结果相同
    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2012-03-04
    • 2019-09-28
    • 2021-04-27
    • 2012-06-30
    相关资源
    最近更新 更多