• Hey all, I’m trying to tweak the sidebar widget for the Yet Another Related Posts Plugin a bit. What I’d like to do is:

    1) Display a thumbnail only for the first post that is listed, and;

    2) Add author and post date below the titles.

    Here’s what I tried to do with the template-widget.php. It adds the thumbnails all right but it lists them all on top of one another, above the titles and links.

    <?php
    
    if (have_posts()) {
    	$output .= '<ul>';
    	while (have_posts()) {
    		the_post();
    		$output .= '<a href="'.get_permalink().'" rel="bookmark">'.the_post_thumbnail( 'node-based-thumb', get_the_ID() ).'</a>';
    		$output .= '<li><a href="'.get_permalink().'" rel="bookmark">'.get_the_title().'</a>';
    //		$output .= ' ('.round(get_the_score(),3).')';
    		$output .= '</li>';
    	}
    	$output .= '</ul>';
    } else {
    	$output .= '<p><em>'.__('No related posts.','yarpp').'</em></p>';
    }

    That’s no good. Can someone help?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Modifying template-widget.php directly is incorrect. You have to take one of the yarpp-template...php files, copy it into your own active theme’s directory, then choose that template in your YARPP widget options. More info here:

    https://mitcho.com/blog/projects/yarpp-3-templates/

    Thread Starter Nick Ottens

    (@ottens)

    Thanks for your message. I did that but it only affects the style of the related posts field when it’s displayed in the body of the post. Am I missing something?

    Here’s my yarpp-template-arras.php:

    <?php
    /*
    Template for Arras WP Theme
    This template returns the related posts, Arras way.
    Author: zy (Melvin Lee)
    */
    
    if ( $related_query->have_posts() ) {
    	echo '<ul class="related-posts">';
    	while ( $related_query->have_posts() ) {
    		$related_query->the_post();
    		?>
    			<a href="<?php the_permalink() ?>"><li class="clearfix">
    				<span class="hidden"><?php the_post_thumbnail( 'node-based-thumb', get_the_ID() ) ?></span>
    				<p class="title"><?php the_title() ?></p>
    				<p class="date"><?php the_time( __('F d, Y', 'arras') ); ?></p>
    				<p><?php echo get_the_excerpt() ?></p>
    			</li></a>
    
    <span class="hidden"><a href=""><li class="clearfix">
    				<p class="title">Your Advertisement Could Be Here</p>
    				<p class="date">Advertisement</p>
    				<p>Advertise on a single article page with a promotion that is neatly integrated in the layout of the "Related posts" lineup.</p>
    			</li></a></span>
    
    		<?php
    	}
    	echo '</ul>';
    } else {
    	echo '<span class="textCenter sub">' . __('No posts at the moment. Check back again later!', 'arras') . '</span>';
    }

    Did you choose the template in your YARPP widget options, not in the YARPP options? Here:

    https://cl.ly/3C2v1f0032380t1e0h07

    Thread Starter Nick Ottens

    (@ottens)

    I did, yes, but I suppose I have to make changes to the yarpp-template-arras.php to affect the style of the widgeted related posts, no?

    After all, right now, it only displays the titles of linked posts, not the time and no excerpt which are displayed in the related posts fields that’s in the body of my posts.

    (I want to move the related posts field from the body to the sidebar so I won’t need any styling for the former anymore.)

    Hi
    I would like to make changes to the sidebar.

    using one of the yarpp-template files only has influence in the text after the blog-item.
    It does not affect the sidebar-widget.

    So my only way to do this is to modify thetemplate-widget.php directly.

    Or do I miss something

    Thread Starter Nick Ottens

    (@ottens)

    I think it’s impossible to have different coding for the related posts between the sidebar and the body. It’s not a huge problem for me because I want them only displayed in the sidebar from now on.

    So, the only question I’ve left is: is it possible to display only the thumb on the first post?

    This is my template code now:

    if ( $related_query->have_posts() ) {
    	echo '<ul>';
    	while ( $related_query->have_posts() ) {
    		$related_query->the_post();
    		?>
    		<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'toolbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php
    		if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    		the_post_thumbnail('medium');
    		}
    		?></a>
    		<li>
    		<h1><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'toolbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    		<div class="entry-meta"><?php toolbox_posted_on(); ?></div>
    		</li></a>

    @bvanleeuwen you can check “Display using a custom template file” in the widget options, and select the custom template you want to use.

    @ottens this is more of a PHP quesiton than WordPress. But basically you can add something like $counter, make it zero, and increase it by one for every post. Something like this I guess:

    if ( have_posts() ) {
    	$counter=0;
    	echo '<ul>';
    	while ( have_posts() ) {
    		the_post();
    		if ($counter==0) { ?>
    		<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'toolbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php
    		if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    		the_post_thumbnail('medium');
    		} } ?></a><li>
    		<h1><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'toolbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    		<div class="entry-meta"><?php toolbox_posted_on(); ?></div>
    		</li></a>
    		<?php $counter++; ?>
    Thread Starter Nick Ottens

    (@ottens)

    Thanks chadrew! That seems to work splendidly.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Plugin: Yet Another Related Posts Plugin 3.4.2] Adding thumbnail to widget’ is closed to new replies.