Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] New Blog Post Styling on Site Wide Feed


  • lomaymi
    Participant

    @lomaymi

    I’m trying to change the way new blog posts are seen on the Site Wide Activity page and I’m halfway there, but having a few issues. Here what I have so far (in bp-custom.php):

    
    <?php
    
    // Edits how blog posts appears on activity feed.
    
    function bp_full_posts_in_activity_feed( $content, $activity ) {
    
         if ( 'new_blog_post' == $activity->type ) {
    
            $post = get_post( $activity->secondary_item_id );
    
         if ( 'post' == $post->post_type )
                 $title = isset( $post->post_title ) ? $post->post_title : '';
                 $content = apply_filters( 'the_content', $post->post_content );
                 $author = "<h5 align=center>By Author Name Here</h5>";
                
         }
        
        echo "<h2 align=center>" .$title. "</h2>";
        echo $author;
        echo $content;
         
    }    
    add_filter( 'bp_get_activity_content_body', 'bp_full_posts_in_activity_feed', 10, 2 );
    

    What I’m trying to do is instead of having the full post I want for the post to break whenever the more tag is used in a post. After the more tag a text or button should appear saying “Read More”. I tried a few things with no success, will appreciate some pointers in the right direction.

    Another thing (although not that important) is that instead of having a default text with “By Author Name Here” it would be great if it can print the name of the post author. I tried using get_the_author(), the_author() and the_author_link(), but none of them were able to show the name in the activity feed.

    Thanks in advance

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

  • danbp
    Participant

    @danbp

    The problem is that the activity stream stripes shortcodes like wp’s quicktag “more”.
    Comment in bp-activity-filter.php:464 says
    If the text returned by bp_create_excerpt() is different from the original text (ie it’s
    been truncated), add the “Read More” link. Note that bp_create_excerpt() is stripping
    shortcodes, so we have strip them from the $text before the comparison.

    So if you’re used to write posts with excerpts, using the_excerpt instead of the_content would effectively bring the excerpt on the feed, but if the excert counts 1500 words, you will see them all !

    If you use the_content, BP will add a read more link after 55 word by defaults, what ever the real amount of words in the post.

    But with the above function active, SWA is no more in default configuration for this activity type.

    So you’ll probably have better to use this function with the_content > full post and add a custom length for post activities.
    Read here how to do that.

    For the author name, you have to use get_userdata.

    This should work:

    function bp_full_posts_in_activity_feed( $content, $activity ) {
    
       if ( 'new_blog_post' == $activity->type ) {
          $post = get_post( $activity->secondary_item_id );
          if ( 'post' == $post->post_type )
    	$title = isset( $post->post_title ) ? $post->post_title : '';
    	$content = apply_filters( 'the_content', $post->post_content ); // or post_excerpt
    	$author = get_userdata( $post->post_author )->user_nicename;  // or display_name;            
         }
        
        echo "<h2 align=center>" .$title. "</h2>";
        echo '<h5 align=center>By '. $author .'</h5>';
        echo $content;
         
    }    
    add_filter( 'bp_get_activity_content_body', 'bp_full_posts_in_activity_feed', 10, 2 );

    lomaymi
    Participant

    @lomaymi

    Thanks danbp for your help, it really point me in the right direction. I decided to use post_excerpt instead of the_content because that one proved quite problematic. The post excerpt worked wonderfully!! Here is the fixed code for anyone to use as reference (note that I also added feature image, post url and a read more button):

    <?php
    
    // Edits how blog posts appears on activity feed.
    
    function bp_posts_in_activity_feed( $content, $activity ) {
    
         if ( 'new_blog_post' == $activity->type ) {
    
            $post = get_post( $activity->secondary_item_id );
    
         if ( 'post' == $post->post_type )
                 $title = isset( $post->post_title ) ? $post->post_title : '';
                 $before_author = 'By';
                 $author = get_userdata( $post->post_author )->user_nicename;
                 $content = get_post( $post->ID )->post_excerpt;
                 $feature_img = wp_get_attachment_image( get_post_thumbnail_id($post->ID), $size ='medium' );
                 $post_url = get_permalink($post->ID);
                 $read_more = "<button type='button'>Read More</button>";
                
         }
        
        echo "<a href='$post_url'><h2 align=center>" .$title. "</h2></a>";
        echo "<h5 align=center>" .$before_author . ' ' . $author. "</h5>";
        echo "<a href='$post_url'><div align=center>" .$feature_img. "</div></a>";
        echo "<p>" .$content. "</p>";
        echo "<a href='$post_url'><h3 align=right>" .$read_more. "</h3></a>";
        
         
    }    
    add_filter( 'bp_get_activity_content_body', 'bp_posts_in_activity_feed', 10, 2 );

    I wanted to point out that on your reply above you suggested…

    echo '<h5 align=center>By '. $author .'</h5>';

    to show the author, but with that method the text “By” appeared above every single entry in the SWA. To fix that I just added the “By” text on a variable and then combined them on the echo. That way it only appears if there is a new blog post.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar