• Resolved Abid

    (@hussain76)


    Hello,

    first of all thanks for this great, fully customizable plugin.

    We’re using a self developed theme in our site (based on Blankslate).

    Instead of customizing WPP’s output via custom theme, I’d like to use an existing post template (‘entry-small.php’) for rendering the popular post list.

    Therefore I hooked into the wpp_custom_html filter:

    function my_12345_wpp_posts_html($popular_posts, $instance) {
    	// loop the array of popular posts objects
    	$output = '<div>';
    	foreach( $popular_posts as $popular_post ) {
    		setup_postdata( $popular_post->id );
    		ob_start();
    		get_template_part( 'entry-small' );
    		$post_html = ob_get_clean();
    		$output .= $post_html;
    	}
    	$output .= '</div>';
    	return $output;
    }
    add_filter('wpp_custom_html', 'my_12345_wpp_posts_html', 10, 2);

    Unfortunately this doesn’t work. The only thing which is shown in the list of popular posts is the post’s author name (all other fields are displayed as an ellipsis ‘…’).

    What am I missing? Is there a better way to achieve this?

    Here’s the (shortened) content of entry-small.php

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<div class="article-item">
    		<div class="row g-0">
    		<div class="col-2">
    	<?php if ( ( has_post_thumbnail() ) ) : ?>
    	<a class="dark-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    	<?php 
    	$img_class = 'img-left';
    	if (strlen(get_post_custom_values('Fokus')[0]) > 0) {
    		$focus = get_post_custom_values('Fokus')[0];
    		$focus = strtolower($focus);
    		if ($focus == 'links') {
    			$img_class = $img_class . ' left';
    		} elseif ($focus == 'rechts') {
    			$img_class = $img_class . ' right';
    		}
    	}
    	if (in_category('Kommentar')) {
    		get_template_part( 'commentary-img', null, 
    		array('img_class' => $img_class));
    		} else {
    		the_post_thumbnail('medium_large', array('class' => $img_class));
    		}?>
    	</a>
    	/* more content ... */

    • This topic was modified 6 months, 1 week ago by Abid.
    • This topic was modified 6 months, 1 week ago by Abid.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @hussain76,

    I have never tried using a theme template like that with WPP so I’m not sure what’s going on.

    Could you please share your entire entry-small.php file (use https://pastebin.com or https://gist.github.com) and all of its dependencies (like function declarations for example) so I can replicate this?

    Thread Starter Abid

    (@hussain76)

    Thanks for your quick response!

    You find the files here: I hope this is sufficient and I forgot nothing.

    EDIT: I did not include any CSS files. I hope that’s ok as it’s only important if the post’s data is displayed at all.

    • This reply was modified 6 months, 1 week ago by Abid.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Yeah, that’s fine. CSS is indeed irrelevant here ??

    Thanks, I’ll give that a try and report back as soon as I can.

    Thread Starter Abid

    (@hussain76)

    Dear @hcabrera ,

    I finally solved it. I had to set the global post variable.

    Sorry if I caused you any work, still thanks a lot for your awesome plugin.

    If there is anything you would recommend to improve in my code, please let me know.

    function my_12345_wpp_posts_html($popular_posts, $instance) {
    	// loop the array of popular posts objects
    	$output = '<div>';
    	foreach( $popular_posts as $popular_post ) {
    		// set the global post variable
    		global $post; 
    		$post = get_post($popular_post->id);
    		// now render the template
    		setup_postdata( $post );
    		ob_start();
    		get_template_part( 'entry-small' );
    		$post_html = ob_get_clean();
    		$output .= $post_html;
    	}
    	$output .= '</div>';
    	return $output;
    }
    add_filter('wpp_custom_html', 'my_12345_wpp_posts_html', 10, 2);

    Plugin Author Hector Cabrera

    (@hcabrera)

    Don’t worry, and thanks for letting me know.

    Untested but I think this should work and should be a tad better in terms of performance since you won’t need to call the get_post() function:

    function my_12345_wpp_posts_html($popular_posts, $instance) {
    	// loop the array of popular posts objects
    	$output = '<div>';
    	foreach( $popular_posts as $popular_post ) {
    		// set the global post variable
    		setup_postdata( $popular_post->id );
    		global $post;
    		// capture output
    		ob_start();
    		get_template_part( 'entry-small' );
    		$post_html = ob_get_clean();
    		$output .= $post_html;
    	}
    	$output .= '</div>';
    	return $output;
    }
    add_filter('wpp_custom_html', 'my_12345_wpp_posts_html', 10, 2);
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to use a post template to display in popular post list’ is closed to new replies.