Php Wordpress-如何从我关注的用户那里获得帖子
我有这个函数来检索指定用户跟随的所有用户。这段代码来自插件“”,我需要从我关注的用户那里获得帖子Php Wordpress-如何从我关注的用户那里获得帖子,php,wordpress,posts,author,Php,Wordpress,Posts,Author,我有这个函数来检索指定用户跟随的所有用户。这段代码来自插件“”,我需要从我关注的用户那里获得帖子 <?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
<?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 );
}
?>
现在,我试图把我关注的用户的所有帖子都显示出来,但这会显示我关注的用户和更多我从未关注过的用户的帖子,而且分页也不起作用
<?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();
}
?>
试试这个功能。确保
pwuf\u get\u following()
返回正确的用户ID
function pwuf_following_posts_shortcode( $atts, $content = null ) {
// Make sure the current user follows someone
if( empty( pwuf_get_following() ) )
return;
else
$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
$items = new WP_Query( array(
'post_type' => 'any',
'posts_per_page' => 12,
'order' => 'DESC',
'author__in' => $userids,
'paged' => $paged //remove if pagination works without this
) );
ob_start();
if ( $items->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 ( $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;
echo '</div><!-- .posts-wrap -->';
echo paginate_links( array(
'total' => $items->max_num_pages
));
wp_reset_postdata();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
return ob_get_clean();
}
函数pwuf\u后面的\u posts\u短码($atts,$content=null){
//确保当前用户跟踪某人
if(空(pwuf\u get\u following())
返回;
其他的
$userids=pwuf_get_following();
$userids=array_值(array_过滤器($userids));
$paged=get_query_var('paged')?get_query_var('paged'):1;//如果分页没有此选项,则删除
$items=新的WP\u查询(数组(
“post_type”=>“any”,
“每页帖子数”=>12,
“订单”=>“描述”,
'author\uu in'=>$userid,
“paged'=>$paged//如果分页工作没有此选项,请删除
) );
ob_start();
如果($items->have_posts()):
$archive_content_layout=get_选项('archive_content_layout','th-grid-2');
回声';
/*开始循环*/
而($items->have_posts()):$items->the_post();
/*
*包括内容的特定于帖子格式的模板。
*如果要在子主题中覆盖此项,请包含一个文件
*称为content-_____;.php(其中___;是Post格式名称),将改用它。
*/
获取模板部分(“模板部分/内容”,获取发布格式());
结束时;
回声';
回显分页链接(数组(
“总计”=>$items->max\u num\u页面
));
wp_reset_postdata();
其他:
获取模板部分(“模板部分/内容”,“无”);
endif;
返回ob_get_clean();
}
试试这个功能。确保pwuf\u get\u following()
返回正确的用户ID
function pwuf_following_posts_shortcode( $atts, $content = null ) {
// Make sure the current user follows someone
if( empty( pwuf_get_following() ) )
return;
else
$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
$items = new WP_Query( array(
'post_type' => 'any',
'posts_per_page' => 12,
'order' => 'DESC',
'author__in' => $userids,
'paged' => $paged //remove if pagination works without this
) );
ob_start();
if ( $items->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 ( $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;
echo '</div><!-- .posts-wrap -->';
echo paginate_links( array(
'total' => $items->max_num_pages
));
wp_reset_postdata();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
return ob_get_clean();
}
函数pwuf\u后面的\u posts\u短码($atts,$content=null){
//确保当前用户跟踪某人
if(空(pwuf\u get\u following())
返回;
其他的
$userids=pwuf_get_following();
$userids=array_值(array_过滤器($userids));
$paged=get_query_var('paged')?get_query_var('paged'):1;//如果分页没有此选项,则删除
$items=新的WP\u查询(数组(
“post_type”=>“any”,
“每页帖子数”=>12,
“订单”=>“描述”,
'author\uu in'=>$userid,
“paged'=>$paged//如果分页工作没有此选项,请删除
) );
ob_start();
如果($items->have_posts()):
$archive_content_layout=get_选项('archive_content_layout','th-grid-2');
回声';
/*开始循环*/
而($items->have_posts()):$items->the_post();
/*
*包括内容的特定于帖子格式的模板。
*如果要在子主题中覆盖此项,请包含一个文件
*称为content-_____;.php(其中___;是Post格式名称),将改用它。
*/
获取模板部分(“模板部分/内容”,获取发布格式());
结束时;
回声';
回显分页链接(数组(
“总计”=>$items->max\u num\u页面
));
wp_reset_postdata();
其他:
获取模板部分(“模板部分/内容”,“无”);
endif;
返回ob_get_clean();
}
这是模板文件的代码。上传后,将此模板-“显示关注者帖子”分配给关注者帖子页面
<?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();
这是模板文件的代码。上传后,将此模板-“显示关注者帖子”分配给关注者帖子页面
<?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();
首先尝试打印(pwuf_get_following()),它必须返回由于某种原因没有跟踪的用户的ID。关于分页问题:正如Ash0ur提到的,print_r(pwuf_get_following())
返回什么?谢谢大家,当我添加print_r时。。。返回Array([1]=>94[3]=>154[4]=>236[5]=>109[6]=>278[8]=>306[9]=>6)
!首先尝试打印(pwuf_get_following()),它必须返回由于某种原因没有跟踪的用户的ID。关于分页问题:正如Ash0ur提到的,print_r(pwuf_get_following())
返回什么?谢谢大家,当我添加print_r时。。。返回Array([1]=>94[3]=>154[4]=>236[5]=>109[6]=>278[8]=>306[9]=>6)
!非常感谢@Outsource,同样的问题,我认为函数无法找到以下用户ID,仍然无法找到我跟踪的用户的帖子和其他人的更多帖子,而且两个分页都不起作用,可能他无法识别pwuf_get_following()代码>做一件事-只要有2个新用户进行检查,每个都跟踪,你能检查这个函数吗,-它返回了什么?我创建了一个新用户,我跟踪一个用户,他只有3篇帖子!现在,我在内容的顶部和底部得到的是我关注的用户的帖子,在我没有关注的用户的其他帖子之间。我尝试了另一个用户,这个用户跟踪了22个用户,同样的问题也存在,但是分页工作正常!!而'posts\u per\u page'=>12,
不起作用,因为我在一个页面中获得了30多篇帖子。我希望我已经作出了应有的解释。对不起,我的英语不好:(你可以发布新用户的print\u r(pwuf\u get\u following())
的返回值吗?这是以下用户的ID吗?是的,Array([0]=>250)
非常感谢你在@Outsource,同样如此