• Resolved John Webber

    (@bilion)


    Hi,

    The plugin records views, but not every view, like when I open a page in incognito to test it.

    Would be great if we could use it to display most popular posts.
    This is what I’m using to list latest posts:

    add_shortcode( 'az_latest_posts','latest_posts_595151');
    function latest_posts_595151(){
    
      query_posts( array ( 'posts_per_page' => 3 ) );
        if ( have_posts() ) : while ( have_posts() ) : the_post(); 
          include WP_CONTENT_DIR . '/custom-assets/php/blog-card-img-top.php';
            endwhile; 
          wp_reset_postdata();
        endif; 
      wp_reset_query();
    }

    How can I list the 3 most popular posts over the last 8 days?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Lap

    (@lapzor)

    There is an option feature request for listing top-posts via a shortcode;
    https://github.com/ibericode/koko-analytics/issues/151

    Thread Starter John Webber

    (@bilion)

    Thanks, I’ll add my thoughts.

    Thread Starter John Webber

    (@bilion)

    I finally have the shortcode that displays popular post cards with all of the meta data: thumbnail, title, categories, excerpt, avatar, display name, date and number of comments.

    So, I’ll just leave it here in case someone needs it.

    if (shortcode_exists('koko_analytics_most_viewed_posts')) {
    	
    	add_shortcode('az_most_viewed_posts','popular_posts_334254');
    	function popular_posts_334254( $args, $posts ) {
    	
    		$default_args = array(
    			'number'    => 3,
    			'post_type' => 'post',
    			'show_date' => false,
    			'days'    => 30,
    		);
    	 
    		$args = shortcode_atts( $default_args, $args );
    		$posts = KokoAnalytics\get_most_viewed_posts($args);  // Adding KokoAnalytics/ because inside the original callback it is namespaced
            
    		$html .= '<div class="az-popular-posts">';
    		$html .= '<div class="az-popular-posts-inner">';
    	
    		foreach ( $posts as $p ) {
    			$categories = '';
    			$category_detail=get_the_category($p);//$post->ID
    				foreach($category_detail as $cd){
    					$category_link = get_category_link($cd->cat_ID);
    
    				 $categories = $categories. '  '.'<a href="'.esc_url( $category_link ).'">'.$cd->cat_name.'</a>';
    				}
    
    			$get_author = get_avatar( $id_or_email = get_the_author_meta( 'user_email' ), $size = '30', $default, $alt = 'Author avatar', $args = array( 'class' => array( 'avatar-below-title', 'inline-meta' ) ) );
    			$authorname = get_the_author(); 
    			$authorurl = get_author_posts_url($p);
    			$authordetails = '<a href="'.esc_url( $authorurl ).'" class="inline-meta author-name" >'.$authorname.'</a>';
    			$commentsurl = get_comments_link($p);
    			$commentnumber = get_comments_number($p);
    			$post_comments = '<a href="'.esc_url( $commentsurl ).'" comments_number="'.$commentnumber.'" class="blog-card-comments inline-meta">' .$commentnumber. '</a>';	
    			$post_title   = get_the_title( $p );
    			$title        = $post_title !== '' ? $post_title : esc_html__( '(no title)', 'koko-analytics' );
    			$aria_current = '';
    			$authurthumb = get_the_post_thumbnail( $p, 'medium', ['alt' => $title], array( 'class' => 'blog-card-image' ));
    			$post_thumbnail = '<a href="'.esc_url( get_the_permalink( $p )).'" class="card-img-link">' .$authurthumb. '</a>';
    			
    			if ( get_queried_object_id() === $p->ID ) {
    				$aria_current = ' aria-current="page"';
    			}
    
    			$html .= '<div class="universal-post-card">';
    
    			// POST THUMBNAIL 
    			$html .= '<div class="blog-img-and-cat">';
    			$html .= sprintf($post_thumbnail);
    			// CATEGORIES
    			$html .='<div class="blog-card-cat-top">';
    			$html .= sprintf($categories);
    			$html .= '</div>';
    			$html .= '</div>';	
    			// POST CARD INNER 
    			$html .= '<div class="blog-card-grid">';
    			$html .= sprintf( '<h3 class="blog-card-title"><a href="%s" %s>%s</a></h3>', get_the_permalink( $p ), $aria_current, $title );
    			// POST EXCERRPT
    			$html .= '<p class="blog-card-excerpt">';
    			$html .=  sprintf(get_the_excerpt( $p ));
    			$html .= '</p>';
    
    			/*
    			if ( $args['show_date'] ) {
    				$html .= sprintf( PHP_EOL . ' <span class="post-date wp-block-post-date">%s</span>', get_the_date( '', $p ) );
    			}
    			*/
    
    			// author avatar
    			$html .= sprintf($get_author);
    			// author name 
    			$html .= sprintf($authordetails);
    			// post time 
    			$html .= '<span class="blog-card-date inline-meta">';
    			$html .= sprintf(get_the_date( 'M d, Y', $p ));
    			$html .= '</span>';
    			// commnets
    			$html .= sprintf($post_comments);
    			$html .= '</div>';
    			$html .= '</div>';
    		}
    	
    		$html .= '</div>';
    		$html .= '</div>';
    		return $html;
    	}
    }
    Plugin Support Lap

    (@lapzor)

    Awesome, thanks for sharing your code!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘List popular posts on frontend’ is closed to new replies.