Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Buddypress activity always jumps to TOP


  • MilliR
    Participant

    @millir

    Hi. I have created a shortcode for activity to display in my frontpage.

    /*activity stream*/
    if (!function_exists('wwn_bp_activities')) {
        function wwn_bp_activities( ) {
    		if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . 'per_page=5' ) ) :
    			echo "<ul id=\"activity-stream\" class=\"activity-list item-list\">";
    			while ( bp_activities() ) : bp_the_activity();
    				locate_template( array( 'activity/entry.php' ), true, false );
    			endwhile;
    			echo "</ul>";
    		endif;
    	
    	}
    	add_shortcode('wwn_bp_activities', 'wwn_bp_activities');
    }

    Say this is my layout (entered text) for the Home page. :

    [h3]WELCOME TO MY SITE[/h3]
    <strong>Site Activity</strong>
    [wwn_bp_activities]

    the ideal output should have been:

    WELCOME TO MY SITE
    Site Activity
    Activity 1
    Activity 2
    Activity 3
    Activity 4
    Activity 5

    But I am getting the following output:

    Activity 1
    Activity 2
    Activity 3
    Activity 4
    Activity 5

    WELCOME TO MY SITE
    Site Activity

    No matter what I do, the activity from the shortcode always jumps to the top of the page (article)

    Please help

Viewing 10 replies - 1 through 10 (of 10 total)

  • r-a-y
    Keymaster

    @r-a-y

    You need to wrap your activity loop code in an object buffer so your contents isn’t outputted at run-time.

    Look up ob_start() and ob_end_clean() on php.net.

    Here’s an example:
    http://php.net/manual/en/function.ob-end-clean.php#103817


    MilliR
    Participant

    @millir

    function wwn_bp_activities( ) {
    		ob_start();
    		if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . 'per_page=5' ) ) :
    			echo "<ul id=\"activity-stream\" class=\"activity-list item-list\">";
    			while ( bp_activities() ) : bp_the_activity();
    				locate_template( array( 'activity/entry.php' ), true, false );
    			endwhile;
    			echo "</ul>";
    		endif;
    		ob_end_clean();
    	}

    I tried it but everything in locate_template( array( ‘activity/entry.php’ ), true, false ); doesnt display. No site activity is displayed. I only get the

      fields as output

    MilliR
    Participant

    @millir

    bump… Need help please


    danbp
    Participant

    @danbp

    hi @millir,

    give this a try:
    tested on wp 4.0 bp 2.0.2 theme 2013 – snippet is in bp-custom.php

    /*activity stream on homepage */
    if (!function_exists('wwn_bp_activities')) {
        function wwn_bp_activities( ) {
    		ob_start();
    		
    		// Remove 'bp_replace_the_content' filter to prevent infinite loops
    		remove_filter( 'the_content', 'bp_replace_the_content' );
    		if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . 'per_page=5' ) ) :
    		bp_get_template_part( 'activity/index' );
    		
    		// Remove 'bp_replace_the_content' filter to prevent infinite loops
    		add_filter( 'the_content', 'bp_replace_the_content' );
    		
    		// Get the output buffer contents
    		$output = ob_get_clean();
    		
    		// Echo or return the output buffer contents
    		if ( true === $echo ) {
    			echo $output;
    		} else {
    			return $output;
    		}
    		endif;	
    		
    		ob_end_clean();
    	
    }
    add_shortcode('wwn_bp_activities', 'wwn_bp_activities');
    }

    MilliR
    Participant

    @millir

    hey @danbp

    I tried your code. It’s displaying the entire page of activity that we see default in /activity/. By entire page, I mean the entire PAGE-TEMPLATE is being reproduced in my homepage template. It’s like an embed. It’s pushing all content, i.e. the widgets and footer below it.

    Here’s a rough display of what I am getting:

    SITE ACTIVITY
    Hello, POST UPDATE box
    Activity 1
    Activity 2
    etc..

    <br />
    Footer
    <br />
    widget1
    widget2
    widget3


    r-a-y
    Keymaster

    @r-a-y

    You’re using ob_start() and ob_end_clean() in the wrong manner.

    You need to save the contents of the activity loop into a variable and then return the contents for the shortcode to work.

    Something like:

    ob_start();
    
    // THE CODE YOU HAVE FOR THE ACTIVITY LOOP
    
    // this is the part you're missing
    $output = ob_get_contents();
    
    ob_end_clean();
    
    // this is the part you're missing
    return $output;

    MilliR
    Participant

    @millir

    YAY! it worked…. thanks a lot @r-a-y

    For anyone else looking for the code, here it is:

    /*activity stream*/
    if (!function_exists('wwn_bp_activities')) {
        function wwn_bp_activities( ) {
    		ob_start();
    		if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . 'per_page=5' ) ) :
    			while ( bp_activities() ) : bp_the_activity();
    				$output .= locate_template( array( 'activity/entry.php' ), true, false );
    			endwhile;
    		endif;
    		$output = ob_get_contents();
    		ob_end_clean();
    		
    		return $output;
    	}
    	add_shortcode('wwn_bp_activities', 'wwn_bp_activities');
    }

    danbp
    Participant

    @danbp

    @millir,
    I tested your function and nothing is returned.

    to get the activities, i changed
    $output .= locate_template( array( 'activity/entry.php' ), true, false );

    to
    $output .= bp_get_template_part( 'activity/entry' );


    r-a-y
    Keymaster

    @r-a-y

    @danbp – millir is probably using an older bp-default theme. Your code will work for everyone else 🙂


    danbp
    Participant

    @danbp

    thank you for that clarification @r-a-y

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Buddypress activity always jumps to TOP’ is closed to new replies.
Skip to toolbar