- 我已成功向Buddypress个人资料页面添加了一个新标签,我还有自定义标签显示我的自定义食谱模板(buddypress / members / single / recipes.php)
function my_setup_nav() {
global $bp;
bp_core_new_nav_item( array(
'name' => __( 'My Recipes', 'buddypress' ),
'parent_slug' => $bp->profile->slug,
'slug' => 'recipes',
'position' => 30,
'screen_function' => 'my_item_one_template',
'default_subnav_slug' => 'recipes'
) );
}
add_action( 'bp_setup_nav', 'my_setup_nav' );
function my_item_one_template() {
add_action( 'bp_template_content', 'my_item_create_screen' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function my_item_create_screen() {
locate_template( array( 'buddypress/members/single/recipes.php' ), true );
}
?>
现在,我想在其个人资料页面中列出所有用户自定义食谱帖子,以便其他用户可以查看他们当前正在查看的用户创建的食谱。这是我当前的代码,但它无法正常工作。它只显示所有食谱,而不仅仅是那个特定用户的食谱。
<?php
$type = 'recipe';
$args = array (
'post_type' => $type,
'author' => $bp->displayed_user->id,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h2><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h2>';
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
?>
答案 0 :(得分:0)
您尝试使用$bp
而不将其作为全局包含在内。
但你不需要它。
尝试更改
'author' => $bp->displayed_user->id,
到
'author' => bp_displayed_user_id(),