Custom output when listing popular posts
-
Hi there.
Right know I’m using
<?php if(function_exists('tptn_show_pop_posts')) tptn_show_pop_posts(); ?>though I’d like to make it custom, I mean, be able to define what I want the tag to output.Basically all I need is the code to output an img tag with the posts thumbnail.
Thanks!
-
If you need to display the post thumbnail, then why not use the Thumbnail settings in the plugin page?
Because e.g. it’s also outputting an
<a>tag wrapping the<img>tag. All I want is the<img>tag and also it contains custom style for dimensions which are conflicting with my CSS.You could write a function that only extracts out the top posts and then uses query posts to get the posts and loop through them in the format you want.
tptn_pop_posts( 'posts_only=1');This would give you an array of the post ids of the top posts. I’ll be introducing a new function that would make it a bit cleaner, but this is a good option to try for now.
Sorry but I don’t think I understand what I’m supposed to do 😡 I’m not really good at php
Hi, it requires writing custom code in your theme files.
Please see this thread of what a user has done: https://wordpress.org/support/topic/meta-valuecustom-field-name-for-wp-query-sorting?replies=12
Tried to achieve something but no output. All I really want is to be able to define the output, like as if it was inside a loop. I saw what the user tried to do but it’s not working for me.
What is the code you’ve tried to use. Please note that this goes into advanced coding
I tried this
<?php $top_posts = tptn_pop_posts( 'posts_only=1&limit=3' ); $top_posts_array = wp_list_pluck( $top_posts, 'postnumber' ); $args = array( 'order' => 'DESC', 'orderby' => 'meta_value', 'post__in' => $top_posts_array, ); $loop = new WP_Query( $args ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <img src="<?php $url = get_post_thumbnail_id(); $url = wp_get_attachment_image_src($url,'large'); $url = $url[0]; echo $url; ?>"> <?php endwhile; ?> <?php wp_reset_postdata(); ?>But it’s not going by the correct order, I mean, it’s not ordering from the most viewed to the least viewed and, also, the limit tag is not working, it’s showing all posts viewed at least once.
Please try this?
<?php $top_posts = tptn_pop_posts( 'posts_only=1&limit=3&strict_limit=1' ); $top_posts_array = wp_list_pluck( $top_posts, 'postnumber' ); $args = array( 'post__in' => $top_posts_array, ); $loop = new WP_Query( $args ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <img src="<?php $url = get_post_thumbnail_id(); $url = wp_get_attachment_image_src($url,'large'); $url = $url[0]; echo $url; ?>"> <?php endwhile; ?> <?php wp_reset_postdata(); ?>Hi again! First of all thanks for your time.
The limit is now working, but it’s still displaying by newest to older, instead of higher views to lower views :\
Could you please try:
$args = array( 'post__in' => $top_posts_array, 'orderby' => 'post__in', );It works now 🙂 Thank you very much for your time!
You’re welcome 🙂
Am glad we got this resolved.
The topic ‘Custom output when listing popular posts’ is closed to new replies.