BuddyDev

Search

[Resolved] Buddyblog post views counter

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #44346

    Hello Tosin,

    Try this one:

    
    
    add_filter( 'the_excerpt', function ( $excerpt ) {
    	if ( ! function_exists( 'Post_Views_Counter' ) ) {
    		return $excerpt;
    	}
    
    	return $excerpt . do_shortcode( '[post-views]' );
    } );
    

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #44347

    The code worked but caused another issue in my blog archive pages.
    I have already added the post views shortcode into my content.php file but now your code adds another counter to the excerpt in my archive page.

    Your code should only display in buddyblog excerpt

    see screenshot https://ibb.co/5vmtTbZ

    Thanks Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #44371

    Hello Tosin,

    You can use ‘bp_is_buddyblog_component’ for BuddyBlog or ‘bblpro_get_queried_post_id’ for BuddyBlog Pro plugin to limit the_excerpt call.

    Please try the following code:

    
    
    add_filter( 'the_excerpt', function ( $excerpt ) {
    
    	if ( ! function_exists( 'Post_Views_Counter' ) ) {
    		return $excerpt;
    	}
    
    	$alter = false;
    
    	if ( function_exists( 'bp_is_buddyblog_component' ) && bp_is_buddyblog_component() ) {
    		$alter = true;
    	} elseif ( function_exists( 'bblpro_get_queried_post_id' ) && bblpro_get_queried_post_id() ) {
    		$alter = true;
    	}
    
    	if ( ! $alter ) {
    		return $excerpt;
    	}
    
    	return $excerpt . do_shortcode( '[post-views]' );
    } );
    
    

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #44372

    Hello the code did not work for me

    The counter did not display in buddyblog at all

    
    /* Display post views counter plugin in buddyblog post management */
    add_filter( 'pvc_display_views_count', function( $display ) {
    	if ( ! function_exists( 'buddyblog_pro' ) ) {
    		return $display;
    	}
    	$pvc = Post_Views_Counter();
    	if ( empty( $pvc->options['display'] ) || empty( $pvc->options['display']['post_types_display'] ) ) {
    		return $display;
    	}
    	$current_post_type = bblpro_get_current_post_type();
    	if ( ! $display && $current_post_type && in_array( $current_post_type, (array) $pvc->options['display']['post_types_display'] ) ) {
    		$display = true;
    	}
    	return $display;
    } );
    
    add_filter( 'the_excerpt', function ( $excerpt ) {
    	if ( ! function_exists( 'Post_Views_Counter' ) ) {
    		return $excerpt;
    	}
    	$alter = false;
    	if ( function_exists( 'bp_is_buddyblog_component' ) && bp_is_buddyblog_component() ) {
    		$alter = true;
    	} elseif ( function_exists( 'bblpro_get_queried_post_id' ) && bblpro_get_queried_post_id() ) {
    		$alter = true;
    	}
    	if ( ! $alter ) {
    		return $excerpt;
    	}
    	return $excerpt . do_shortcode( '[post-views]' );
    } );
    
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #44467

    Hello Tosin,

    SOrry for the delayed reply. Is this issue fixed or not. Please let me know?.

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #44479

    Hello Ravi,

    This is not resolved, like I stated in the last thread that the code did not work for me.

    The counter did not display in buddyblog at all.

    Thanks

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #44482

    Hello Tosin,

    Thank you for the acknowledgement. Please try the following code:

    
    
    /* Display post views counter plugin in buddyblog post management */
    add_filter( 'pvc_display_views_count', function ( $display ) {
    	if ( ! function_exists( 'buddyblog_pro' ) ) {
    		return $display;
    	}
    	$pvc = Post_Views_Counter();
    	if ( empty( $pvc->options['display'] ) || empty( $pvc->options['display']['post_types_display'] ) ) {
    		return $display;
    	}
    	$current_post_type = bblpro_get_current_post_type();
    	if ( ! $display && $current_post_type && in_array( $current_post_type, (array) $pvc->options['display']['post_types_display'] ) ) {
    		$display = true;
    	}
    
    	return $display;
    } );
    
    add_filter( 'the_excerpt', function ( $excerpt ) {
    
    	if ( ! function_exists( 'Post_Views_Counter' ) || ! function_exists( 'buddypress' ) || ! bp_is_user() ) {
    		return $excerpt;
    	}
    
    	$alter = false;
    
    	if ( function_exists( 'bblpro_get_current_post_type' ) && bblpro_get_current_post_type() ) {
    		$alter = true;
    	}
    
    	if ( ! $alter ) {
    		return $excerpt;
    	}
    
    	return $excerpt . do_shortcode( '[post-views]' );
    } );
    
    

    Please let me know if it works or not.

    Regards
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #44483

    Hello Tosin,

    Please ignore my last reply and use the following code only:

    
    add_filter( 'the_excerpt', function ( $excerpt ) {
    
    	if ( ! function_exists( 'Post_Views_Counter' ) || ! function_exists( 'buddypress' ) || ! bp_is_user() ) {
    		return $excerpt;
    	}
    
    	if ( ! function_exists( 'bblpro_get_current_post_type' ) ) {
    		return $excerpt;
    	}
    
    	$current_post_type = bblpro_get_current_post_type();
    
    	if ( $current_post_type && in_array( $current_post_type, (array) Post_Views_Counter()->options['display']['post_types_display'] ) ) {
    		$excerpt = $excerpt . do_shortcode( '[post-views]' );
    	}
    
    	return $excerpt;
    } );
    
    

    Please let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #44485

    Hello Ravi,

    Your code worked perfectly!

    Thanks a lot

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #44486

    Hello Tosin,

    Thank you for the acknowledgement. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Buddyblog post views counter’ is closed to new replies.

This topic is: resolved