• Resolved pknack

    (@pknack)


    Hello.

    Is there any option to display today’s website statistics as a shortcode? I want to insert it into the page

    Thanks!

Viewing 1 replies (of 1 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    My plugin does not do this out of the box, but you could use the following code snippet, added in a functionality plugin, to create a new jp_post_view_today shortcode:

    
    add_shortcode(
    	'jp_post_view_today',
    	function () {
    		// Get the post ID.
    		$post_id = get_the_ID();
    
    		if ( ! isset( $post_id ) || empty( $post_id ) ) {
    			return;
    		}
    		
    		if ( ! function_exists( 'stats_get_from_restapi' ) ) {
    			return;
    		}
    
    		// Get extra views for that post ID.
    		$stats = stats_get_from_restapi(
    			array( 'fields' => 'views' ),
    			sprintf(
    				'post/%d',
    				$post_id
    			)
    		);
    
    		$today_data = array();
    		if (
    			isset( $stats )
    			&& ! empty( $stats )
    			&& isset( $stats->data )
    		) {
    			$today_data = end( $stats->data );
    		}
    
    		if ( empty( $today_data ) || ! isset( $today_data[1] ) ) {
    			return esc_html__( 'no views', 'post-views-for-jetpack' );
    		}
    
    		return sprintf(
    			esc_html(
    				_n(
    					'%s view today',
    					'%s views today',
    					$today_data[1],
    					'post-views-for-jetpack'
    				)
    			),
    			number_format_i18n( $today_data[1] )
    		);
    	}
    );
    

    You can then use that shorcode in your posts, or use the method I highlighted here to use that shortcode in your theme:
    https://ww.wp.xz.cn/plugins/post-views-for-jetpack/#faq

Viewing 1 replies (of 1 total)

The topic ‘Views today shortcode’ is closed to new replies.