简体   繁体   English

尝试在其buddypress个人资料中显示用户的自定义帖子类型帖子?

[英]Trying to show a user's custom post type posts in their buddypress profile?

-I have successfully added a new tab to the Buddypress profile page and I also have my custom tab displaying my custom recipes template located in (buddypress/members/single/recipes.php) -我已经在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 );
}

?>

Now I want to list all the users custom recipe posts in their profile page, so that other users can see what recipes the user they are currently viewing has created. 现在,我想在其个人资料页面中列出所有用户自定义配方帖子,以便其他用户可以看到他们当前正在查看的用户创建了哪些配方。 This is my current code but it does not work properly. 这是我当前的代码,但无法正常工作。 It just displays all recipes, not just the ones from that particular user. 它仅显示所有配方,而不仅仅是该特定用户的配方。

<?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;
?>

You're trying to use $bp without including it as a global. 您正在尝试使用$bp而不将其包含为全局变量。 But you don't need it. 但是您不需要它。

Try changing 尝试改变

'author' => $bp->displayed_user->id,

to

'author' => bp_displayed_user_id(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM