[英]Trying to show a user's custom post type posts in their buddypress profile?
-我已经在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;
?>
您正在尝试使用$bp
而不将其包含为全局变量。 但是您不需要它。
尝试改变
'author' => $bp->displayed_user->id,
至
'author' => bp_displayed_user_id(),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.