Skip to:
Content
Pages
Categories
Search
Top
Bottom

Making the users’ articles appear on profiles


  • franklintragic
    Participant

    @franklintragic

    Hey, guys!

    I’ve got a working code (copied below), which allows me to create a tab on our profiles for articles that were written by the users. It’s working but I would like to customise it a little bit, and I don’t know a lot about coding. If I click on the tab, the full articles become visible including the title. What I would like however is to be able to click on the title, see the excerpt instead of the full article, and the full-sized featured images above the titles. Also, by the Latest Posts option, is there a way to see how many articles were written by our users?

    Thanks a lot!

    function bpfr_get_post() {
    	
    	/*
    	 * to get all post, comment the line 'author'
    	 */
    	$myposts = get_posts(  array(
    	'posts_per_page' => 3, // set the number of post to show
    	'author'         => bp_displayed_user_id(), // show only this member post
    	'post_type'      => 'post',
    	'orderby'          => 'post_date',
    	'order'            => 'DESC',
    	'post_status'      => 'publish'
    	));
    	
    	if( ! empty( $myposts ) ) { 
    		
    		foreach($myposts as $post) {
    			setup_postdata( $post );
    			$page_object = get_post( $post );			
    			
    			/* 
    			 * uncomment next line to show only a list of titles linked to full post
    			 * if you uncomment, you have to comment the 2 echo below
    			 */
    			
    		// echo '<a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a>';			
    			
    			/*
    			 * comment the 2 following echo (or remove them) if you use the above
    			 */
    			
    		 echo '<h3 class="profile_post">'. $page_object->post_title .'</h3>';
    		 echo $page_object->post_content;
    		}	
    		
    		wp_reset_postdata();	
    	} else { 
    		
    		echo '<div class="info" id="message">---</div>';
    	}
    }
    add_action ( 'my_profile_post', 'bpfr_get_post' );
    
    function bpfr_post_profile_setup_nav() {
    	global $bp;
    	$parent_slug = 'articles';
    	$child_slug = 'posts_sub';	
    	
    	bp_core_new_nav_item( array(
    	'name' => __( 'Articles' ),
    	'slug' => $parent_slug,
    	'screen_function' => 'bpfr_profile_post_screen',
    	'position' => 40,
    	'default_subnav_slug' => $child_slug 
    	) );
    	
    	//Add subnav item 	 
    	bp_core_new_subnav_item( array( 
    	'name' => __( 'My articles' ), 
    	'slug' => $child_slug, 
    	'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/', 
    	'parent_slug' => $parent_slug, 
    	'screen_function' => 'bpfr_profile_post_screen'
    	) );
    }
    
    function bpfr_profile_post_screen() {	
    	add_action( 'bp_template_content', 'bpfr_profile_post_screen_content' );
    	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    }
    
    function bpfr_profile_post_screen_content() {
    	do_action( my_profile_post);
    }
    
    add_action( 'bp_setup_nav', 'bpfr_post_profile_setup_nav' );
Viewing 1 replies (of 1 total)

  • danbp
    Moderator

    @danbp

    Hi,

    your specs:

    1. be able to click on the title
    2. see the excerpt instead of the full article
    3. the full-sized featured images above the titles
    4. a way to see how many articles were written by our users

    The number of posts is showed beside the item, like the other BP counts on the buddybar.

    The new code to use:

    function bpfr_get_post() {
    
    	$myposts = get_posts(  array(
    	'posts_per_page'	=> 3, // set the number of post to show
    	'author'			=> bp_displayed_user_id(), // show only this member post
    	'post_type'			=> 'post',
    	'orderby'			=> 'post_date',
    	'order'				=> 'DESC',
    	'post_status'		=> 'publish'
    	));
    	
    	if( ! empty( $myposts ) ) { 
    		
    		foreach($myposts as $post) {
    			setup_postdata( $post );
    			$page_object = get_post( $post );					
    
    		if ( has_post_thumbnail( $post->ID ) ) {
    			echo get_the_post_thumbnail( $post->ID, 'full-size', array( 'class' => 'alignleft' ) );
    		} 
    			echo '<h3 class="profile_post"><a href="'. esc_url( get_permalink(  $post->ID ) ) .'">'. $page_object->post_title .'</a></h3>';
    
    			if ( empty( $post->post_excerpt ) ) {
    				echo wp_kses_post( wp_trim_words( $post->post_content, 20 ) );
    
    			} else {
    				echo wp_kses_post( $post->post_excerpt );
    			}
    		}	
    		
    		wp_reset_postdata();	
    
    		} else { 
    		
    			echo '<div class="info" id="message">No post actually</div>';
    	}
    }
    add_action ( 'my_profile_post', 'bpfr_get_post' );
    
    function bpfr_post_profile_setup_nav() {
    global $bp;
    
    	$parent_slug = 'articles';
    	$child_slug = 'posts_sub';	
    	$post_count = count_user_posts(  bp_displayed_user_id(), array('post', 'post' ) );
    
    		//Add nav item with posts count
    		bp_core_new_nav_item( array(
    			'name' => __( 'Articles' ) .'<span>'.$post_count.'</span>',
    			'slug' => $parent_slug,
    			'screen_function' => 'bpfr_profile_post_screen',
    			'position' => 40,
    			'default_subnav_slug' => $child_slug 
    		) );
    		
    		//Add subnav item 	 
    		bp_core_new_subnav_item( array( 
    			'name' => __( 'My 3 latest posts' ),
    			'slug' => $child_slug, 
    			'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/', 
    			'parent_slug' => $parent_slug, 
    			'screen_function' => 'bpfr_profile_post_screen'
    		) );
    
    	}
    
    	function bpfr_profile_post_screen() {	
    		add_action( 'bp_template_content', 'bpfr_profile_post_screen_content' );
    		bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    	}
    
    	function bpfr_profile_post_screen_content() {
    		do_action( 'my_profile_post' );
    	}
    
    add_action( 'bp_setup_nav', 'bpfr_post_profile_setup_nav' );
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar