如何在Buddypress profile链接中使用用户帖子创建新选项卡?
1) 在主题目录下创建一个文件夹/buddypress/
2) 创建bp自定义。目录中的php文件。现在您的主题文件夹有一个结构-->
您的主题目录/buddypress/bp自定义。php
2.1包括bp自定义。主题函数中的php文件。php使用include或include_once
php函数。
3) 创建三个操作以使选项卡正常工作(暂时不要做任何事情。只是解释理论)
add_action( \'bp_setup_nav\', \'buddyboss_child_bp_nav_adder\' );
add_action( \'bp_template_content\', \'profile_buddyboss_child_loop\' );
bp_core_load_template( apply_filters( \'bp_core_template_plugin\', \'members/single/plugins\' ) );
4)首先,您需要应用操作
buddyboss_child_bp_nav_adder
到挂钩
bp_setup_nav
.
5) 函数定义如下所示。
function buddyboss_child_bp_nav_adder() {
global $bp;
$post_count = count_user_posts_by_type( $bp->displayed_user->id );
bp_core_new_nav_item(
array(
\'name\' => sprintf( __( \'Articles <span>%d</span>\', \'my-poems\' ), $post_count ),
\'slug\' => \'Articles\',
\'position\' => 250,
\'show_for_displayed_user\' => true,
\'screen_function\' => \'buddyboss_child_list\',
\'item_css_id\' => \'articles\',
\'default_subnav_slug\' => \'public\'
));
}
6)上述功能的重要部分是
screen_function
使用名称
buddyboss_child_list
7) 创建名为的函数buddyboss_child_list
function buddyboss_child_list() {
add_action( \'bp_template_content\', \'profile_buddyboss_child_loop\' );
bp_core_load_template( apply_filters( \'bp_core_template_plugin\', \'members/single/plugins\' ) );
}
The important filters and functions are ..
buddy Press filter ===>>> bp_template_content
buddyPress function to load template =>>> bp_core_load_template
8)为内容声明和定义新函数。在上面我们有
profile_buddyboss_child_loop
函数获取数据。
/* This is end of the code for above function */
function profile_buddyboss_child_loop() {
$myposts = get_posts( array(
\'posts_per_page\' => -1,
\'author\' => bp_displayed_user_id(),
\'post_type\' => \'post\'
));
if( ! empty($myposts) ) {
foreach($myposts as $post) {
setup_postdata( $post );
if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'sidebar-smallthumbnew\' );
else :
$image[0] = "http://placehold.it/85x60";
endif;
echo \'<li class="sidebar mostpop post-\' . $post->ID . \'"><div id="postimage"><a href="\' . get_permalink($post->ID) . \'"><img src="\' . $image[0] . \'" /></a></div><div id="postinfo"><a href="\' . get_permalink($post->ID) . \'">\' . get_the_title($post->ID) . \'</a></div></li>\';
}
echo \'</ul>\';
wp_reset_postdata();
} else { ?>
<div class="info" id="message">
<p><strong><?php bp_displayed_user_fullname(); ?></strong> has No posts.</p>
</div>
<?php }
}
/* This is end of the code for above function */
9)要获取作者或用户的帖子数量,请在
function count_user_posts_by_type( $userid, $post_type = \'post\' ) {
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $userid, true);
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return apply_filters( \'get_usernumposts\', $count, $userid );
}
就是这样。此时,您的个人资料页面将显示用户/作者页面以及他们自己的帖子。