BuddyDev

Search

Loop Subnav Function

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #653

    I have a challenge which i am unable to solve by myself.

    I have a website where users can submit their own posts. And to tag the posts they use custom taxonomies (1 taxonomy term per post maximum) that will “group” the posts in “collections”

    In the profile of the members of my website, i have a nav item called ‘Collections’. So when viewing this tab, I would like to create a list with all the Taxonomy Terms the user has used in the posts.

    My challenge is to get a subnav page where all the posts per taxonomy term can be viewed.

    example:

    User john has submitted 2 posts, 1 with taxonomy term ‘foo’, and the other with taxonomy term ‘bar’. The nav tab ‘Collections’ (http://website.com/member/john/collections) should display Foo and Bar with links to a subnav page.

    Subnav pages:
    http://website.com/member/john/collections/foo -> contains john’s posts with taxonomy term foo
    http://website.com/member/john/collections/bar -> contains john’s posts with taxonomy term bar

    Problem is that the taxonomy terms are not fixed, member can use whichever term they want, so the tabs have to be dynamically created.

    I have already created a nav tab called ‘collections’ and i have below code to create a subnav tab.

    
    function my_custom_subnav() {
    	global $bp;
    	
    	bp_core_new_subnav_item( array( 
    		'name'            => 'TAXONOMY TERM',   // Taxonomy name
    		'slug'            => 'taxonomy-term',   // taxonomy-term with dashes for spaces
    		'screen_function' => 'subnav_function_to_show_screen', 
                    'parent_url'	  => trailingslashit(  bp_displayed_user_domain()  . 'collections' ),
    		'parent_slug'	  => 'collections',
    		'position'			=> 100, // position is not important as i want to hide the subnav menu items and use the list in the parent nav tab
    	
    	) );
    	
    }
    
    function subnav_function_to_show_screen() {
    
    	//add title and content here – last is to call the members plugin.php template
    	add_action( 'bp_template_title', 'subnav_function_to_show_screen_title' );
    	add_action( 'bp_template_content', 'subnav_function_to_show_screen_content' );
    	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    }
    
    function subnav_function_to_show_screen_title() {
    	// WANT TO ECHO TAXONOMY TITLE
    }
    
    function subnav_function_to_show_screen_content() {
    	// GET ALL POSTS FROM DISPLAYED USER WITH THE TAXONOMY TERM
    }
    
    add_action( 'bp_setup_nav', 'my_custom_subnav' );
    

    and I have piece of code which fetches all Taxonomy Terms which John uses

    
    /** GET COLLECTIONS TAXONOMY TERMS */
    function list_author_used_terms($author_id){
    
        // get the author's posts
        $posts = get_posts( array('post_type' => 'post', 'posts_per_page' => -1, 'author' => $author_id) );
        $author_terms = array();
        //loop over the posts and collect the terms
        foreach ($posts as $p) {
            $terms = wp_get_object_terms( $p->ID, 'collection');
            foreach ($terms as $t) {
                $author_terms[] = $t->name;
            }
        }
        return array_unique($author_terms);
    }
    
    

    So what i am looking to achieve is to create a loop, where a subnav tab is created for each of the terms that John has used.

    Is what i am looking to achieve possible?

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #656

    Hi,
    Just trying to find some clarifications here.
    A Term can be used by more than one user, is that correct?

    Also, you only want to display the terms used by the user and not all the terms in that particular taxonomy?

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #671

    Hi Brajesh,

    1) Yes, a term can be used by more than 1 user
    2) Also correct, ONLY the terms used by the displayed user

    The code above (GET COLLECTIONS TAXONOMY TERMS) gets only the terms used by the displayed user.

    
        return array_unique($author_terms);
    

    And i am looking to loop this array and create a subnav tab for each term in this array.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #672

    Ok,
    Here is a slightly modified version of your last function

    
    function list_author_used_terms($author_id){
    
        // get the author's posts
        $posts = get_posts( array('post_type' => 'post', 'posts_per_page' => -1, 'author' => $author_id) );
        $author_terms = array();
        //loop over the posts and collect the terms
        foreach ($posts as $p) {
            $terms = wp_get_object_terms( $p->ID, 'collection');
            foreach ($terms as $t) {
                $author_terms[$t->term_id] = $t;
            }
        }
        return $author_terms;
    }
    
    

    and here is an example use

    
    
    $terms = list_author_used_terms( 1 );
    $links = array();
    foreach( $terms as $term ) {
    	$links[] =  sprintf( "<a href='%s'>%s</a>", get_term_link( $term ), $term->name );
    }
    
    echo join( '', $links );
    
    

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #674

    Thanks for your reply.

    What is seems to do is get the terms and a link, right?

    That is not what i was actually asking your help for. what i was looking to achieve is to create a subnav tab for each term in the buddypress member profile view.

    so by looping functions my_custom_subnav(), function subnav_function_to_show_screen_title() and
    function subnav_function_to_show_screen_content() for each term the user has used within the custom taxonomy.

    nav tab: collection (default fixed profile tab)
    sub nav tab: term1, term3, term 4, term7 (depending on the terms the user has used in the submitted posts)

    I hope that clarifies what i am looking to achieve.

You must be logged in to reply to this topic.

This topic is: not resolved