Wordpress-如何从我关注的用户那里获得帖子 [英] Wordpress - How can i get the posts from the users i follow

查看:98
本文介绍了Wordpress-如何从我关注的用户那里获得帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有此功能来检索指定用户遵循的所有用户。此代码来自插件 用户关注系统,以及我需要从我关注的用户那里获得帖子!

I have this function to retrieves all the users that the specified user follows. This code from plugin "Users Following System", and all i need to get the posts from the users i follow!

<?php
/**
 * Retrieves all users that the specified user follows
 *
 * Gets all users that $user_id followers
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve following for
 * @return      array
 */

function pwuf_get_following( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = get_user_meta( $user_id, '_pwuf_following', true );

    if ( empty( $following ) ) {

    return;

    }
    return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
}
?>

现在我正在尝试显示我关注的用户的所有帖子。我关注的用户以及其他我从未关注过的用户的帖子,分页效果也不佳。

Now i'm trying to bring out all posts of the users i follow.but this bring out the users i follow and more other posts of users i never following them also the pagination not working as well.

<?php
/**
 * Shows the posts from users that the current user follows
 *
 * @access      private
 * @since       1.0
 * @return      string
 */

function pwuf_following_posts_shortcode( $atts, $content = null ) {

    // Make sure the current user follows someone
    if( empty( pwuf_get_following() ) )
        return;

    $items = new WP_Query( array(
        'post_type'      => 'any',
        'posts_per_page' => 12,
                'order' => 'DESC',
        'author__in' => pwuf_get_following()
    ) );

    ob_start(); ?>
    <?php
        if ( $items->have_posts() ) : ?>

            <?php

            $archive_content_layout = get_option( 'archive_content_layout', 'th-grid-2' );
            echo '<div class="posts-wrap ' . esc_attr( $archive_content_layout ) . '">';

                /* Start the Loop */
                while ( $items->have_posts() ) : $items->the_post();

                    /*
                    * Include the Post-Format-specific template for the content.
                    * If you want to override this in a child theme, then include a file
                    * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                    */
                    get_template_part( 'template-parts/content', get_post_format() );


                endwhile;

                wp_reset_postdata();

            echo '</div><!-- .posts-wrap -->';

            the_posts_pagination();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>
<?php 
    return ob_get_clean();

}
?>


推荐答案

这是您的模板文件的代码。上传后,将这个模板-显示关注者帖子分配到您的关注者帖子页面。

Here is the code for your template file. Assign this template - 'Show Followers Posts' to your followers posts page once uploaded.

<?php
/* Template Name: Show Followers Posts */
get_header();
?>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        // Make sure the current user follows someone
        if( !empty( pwuf_get_following() ) )
        {
            $userids = pwuf_get_following();

            $userids = array_values(array_filter($userids));    

            $paged = get_query_var('paged') ? get_query_var('paged') : 1;  //remove if pagination works without this

            $wp_query = new WP_Query( array(
                'post_type' => 'any',
                'posts_per_page' => 12,
                'order' => 'DESC',
                'author__in' => $userids,
                'paged' => $paged   //remove if pagination works without this
            ) );

            if ( $wp_query->have_posts() ) : 

                $archive_content_layout = get_option( 'archive_content_layout', 'th-grid-2' );
                echo '<div class="posts-wrap ' . esc_attr( $archive_content_layout ) . '">';

                /* Start the Loop */
                while ( $wp_query->have_posts() ) : $wp_query->the_post();
                    /*
                    * Include the Post-Format-specific template for the content.
                    * If you want to override this in a child theme, then include a file
                    * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                    */
                    get_template_part( 'template-parts/content', get_post_format() );
                endwhile;

                echo '</div><!-- .posts-wrap -->';

                echo paginate_links( array(
                    'total' => $wp_query->max_num_pages
                ));

                wp_reset_query();
            else :
                get_template_part( 'template-parts/content', 'none' );
            endif;
        }
        ?>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php
get_sidebar();
get_footer();

这篇关于Wordpress-如何从我关注的用户那里获得帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆