• Resolved LisaOr

    (@lisaor)


    Hello,

    I love you widget, and am using it to display custom format posts.
    I’ve been trying to only make it display posts that have a thumbnail picture, but I can’t figure out a way to make it work.

    <?php while( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post;?>

    I guess there’s a condition I have to add to this line… but how?!!

    Thanks a lot for your time.

    https://ww.wp.xz.cn/plugins/flexible-posts-widget/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author DaveE

    (@dpe415)

    Hi LisaOr,

    While it would be possible to just “skip” posts that don’t have a thumbnail while outputting the widget contents, a better solution would be to actually only get posts that have a thumbnail attached.

    Instead of skipping, add this code to your theme’s functions.php file and FPW will only get posts that have a thumbnail attached in the first place:

    /**
     * Filter the Flexible Post Widget query arguments
     * to only get posts with a attachment thumbnail
     */
    add_filter( 'dpe_fpw_args', 'dpe_fpw_with_thumbs' );
    function dpe_fpw_with_thumbs( $args ) {
    	$args['meta_key'] = '_thumbnail_id';
    	return $args;
    }

    Cheers!

    Thread Starter LisaOr

    (@lisaor)

    Thanks a lot…
    but unfortunately that’s not going to work out for me: I use multiple Flexible Post Widgets. I want to skip posts that don’t have a thumbnail ONLY in one of the widgets… not all of them.
    And I think your code will do that for every Flexible Post Widget right?

    I’ve created a unique php template for every Flexible widget I have… any way I could write that rule down directly on the widget-customname.php file?

    Plugin Author DaveE

    (@dpe415)

    Gotcha. Well, in that case, it will be easiest to just add the “skip” within the template for that widget’s output. Below is an excerpt from the default widget template with the additional if check for a post thumbnail.

    <ul class="dpe-flexible-posts">
    	<?php while( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post; ?>
    		<?php if( has_post_thumbnail() ): ?>
    		<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<a href="<?php echo the_permalink(); ?>">
    				<?php
    					if( $thumbnail == true ) {
    						// If the post has a feature image, show it
    						if( has_post_thumbnail() ) {
    							the_post_thumbnail( $thumbsize );
    						// Else if the post has a mime type that starts with "image/" then show the image directly.
    						} elseif( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
    							echo wp_get_attachment_image( $post->ID, $thumbsize );
    						}
    					}
    				?>
    				<h4 class="title"><?php the_title(); ?></h4>
    			</a>
    		</li>
    		<?php endif; // has_post_thumbnail ?>
    	<?php endwhile; ?>
    </ul><!-- .dpe-flexible-posts -->
    Thread Starter LisaOr

    (@lisaor)

    That’s exactly what I’ve been trying to do for the past few days, but it doesn’t work… no post are showing.. even thought some of them HAVE a thumbnail image.
    It’s like “if( has_post_thumbnail()” blocks all the posts.
    thanks again for your time.

    Plugin Author DaveE

    (@dpe415)

    Sorry about that. I had placed the thumbnail check outside the post loop, which wouldn’t ever work.

    I’ve adjusted my previous code example so it is correctly checking inside the loop and I’ve verified that it works with a FPW and the default 2014 theme.

    This one should do it for you. Cheers!

    Thread Starter LisaOr

    (@lisaor)

    nop… still not working…
    I’ve modified the code so it displays the post as a slides.. maybe that’s why it’s not working (although I don’t see where the problem could be)
    here is my code:

    <?php while( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post;?>
    			<?php if( has_post_thumbnail() ): ?>
    				<div id="img_slider">
    					<img src="<?php echo get_template_directory_uri(); ?>/images/sur-slider.png" id="sur-slider"/>
    					<a href="<?php echo the_permalink(); ?>">
    						<?php
    						if( $thumbnail == true ) {
    							// If the post has a feature image, show it
    							if( has_post_thumbnail() ) {
    								the_post_thumbnail( $thumbsize );
    							// Else if the post has a mime type that starts with "image/" then show the image directly.
    							} elseif( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
    								echo wp_get_attachment_image( $post->ID, $thumbsize );
    							}
    						}
    						?>
    					</a>
    				</div>
    				<div id="infos_producteurs">
    						<a href="<?php echo the_permalink(); ?>" class="nom"><?php the_title(); ?></a>
    						<p><?php the_excerpt(5); ?></p>
    				</div>
    			<?php endif; ?>
    		<?php endwhile; ?>
    Plugin Author DaveE

    (@dpe415)

    Hi Lisa,

    I pasted that code into my default template and it is working fine: showing only posts that have a thumbnail. Removing the thumbnail check also showed all posts (thumbnail or not).

    There must be some other code interfering with the output of that widget for you. :\

    Thread Starter LisaOr

    (@lisaor)

    ok.. I’ll check where it can come from.
    Thanks a lot for your help / time.
    🙂

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Show only posts with thumbnails’ is closed to new replies.