• Resolved Pranjal

    (@pranjalg)


    First up, thanks for this amazing plugin. This is the most lite plugin I had found for displaying related posts using taxonomies.

    By far, I’ve been using this code to display related posts from taxonomies using this code

    <?php
    // check if the function already exists
    if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) {
      $args = array(
        'posts_per_page' => 10,
      );
      $taxonomies = array('movie');
    
      $related_posts = km_rpbt_related_posts_by_taxonomy( $post->ID, $taxonomies, $args );
      if( $related_posts ) {
        echo '<ul>';
        // loop through related posts
        foreach ( (array) $related_posts as $related ) {
          // title is the post ID if a post doesn't have a title
          $title = ($related->post_title) ? $related->post_title : $related->ID;
          echo '<li><a href="' . get_permalink( $related->ID ) . '">' . $title . '</a></li>';
        }
        echo '</ul>';
      }
    }
    ?>

    But, since I’ve updated to the new version which also supports to display post thumbnails. Can you please tell me how to alter this code to display related posts from a taxonomy, in a single column?

    Thanks!

    https://www.ads-software.com/plugins/related-posts-by-taxonomy/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    The function km_rpbt_related_posts_by_taxonomy still gets the related posts objects. To only get posts that have a post thumbnail you can use this for the arguments:

    $args = array(
        'posts_per_page' => 10,
        'post_thumbnail' => true,
    );

    The widget and shortcode use this function to display the post thumbnails:
    https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/functions/#km_rpbt_related_posts_by_taxonomy_gallery

    But if you want, you can use any of the post thumbnail functions in your own template:
    https://codex.www.ads-software.com/Post_Thumbnails#Function_Reference

    Thread Starter Pranjal

    (@pranjalg)

    Thanks for quick response.

    I tried using this code

    <?php
    // first get the related post objects with the km_rpbt_related_posts_by_taxonomy() function.
    
    // check if the function already exists
    if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) {
    
        $args = array(
            'posts_per_page' => 8,
            'post_thumbnail' => true, // only gets the posts that have a post thumbnail
        );
    
        $taxonomies = array( 'category' );
        $post_id =  $post->ID; // post id available inside the loop.
    
        $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id , $taxonomies, $args );
    
        // check if related post were found
        if ( $related_posts ) {
    
            $defaults = array(
                'columns'    => 2,
                'size'       => 'medium',
            );
    
            // display the related post thumbnail gallery
            echo km_rpbt_related_posts_by_taxonomy_gallery( $args, $related_posts );
        }
    }
    	?>

    It displays the post with thumbnails, but, the ‘columns’ settings doesn’t works for me, I tried, to make them display only in 1 code by no results. Also the thumbnail is not resizing on defining ‘small’ or ‘large.’

    Please help me with this! (BTW I don’t know PHP)

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Arrg, I just noticed an error in the documentation.
    Change this:

    $defaults = array(
                'columns'    => 2,
                'size'       => 'medium',
     );

    to this:

    $args = array(
                'columns'    => 2,
                'size'       => 'medium',
     );

    [updated documentation]

    Thread Starter Pranjal

    (@pranjalg)

    Thank you so much, but one more issue. It seems like the default thumbnail size of the post thumbnails is big (420 x 334) and changing settings to small, medium, and large doesn’t works either.

    Please help me how to define those sizes using my functions.php ?

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Try setting the size to ‘thumbnail’. Default sizes are “thumbnail”, “medium”, “large”, “full”.

    You can set your own thumbnail sizes in your theme’s functions.php with add_image_size():
    https://codex.www.ads-software.com/Function_Reference/add_image_size

    Check if your theme’s functions.php adds any new thumbnail sizes you like.

    Thread Starter Pranjal

    (@pranjalg)

    Brilliant, it completely solved my problem. Thanks a lot for your kind support ??

    Now, the one last thing I just noticed that, indeed the post thumbnail sizes changes, but, actually it just scales the real size of image to the one I had set in the functions file. And despite of the low resolution of the post thumbnail, it size remains same. So, isn’t there a way we can reduce the size as, well?

    Plugin Author keesiemeijer

    (@keesiemeijer)

    So, isn’t there a way we can reduce the size as, well?

    Can you explain a bit more and/or share a link to your site where we can see the problem.

    Thread Starter Pranjal

    (@pranjalg)

    Well, by those words I mean, that the size(kbs) of the cropped thumbnails should be smaller.

    Because a post thumbnail image with 284 x 225 resolution, sizes around 30KB while, the original image compressed using Photoshop with a resolution of 480 x 320 also sizes 32KB. (notice the difference, the post thumbnail size should be lesser, somewhat around 15-20kb)

    However, I think it might be because of poor compression method WordPress might be using to crop post thumbnail images.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    When you upload new images your new (smaller) size will be used. For older images you can regenerate thumbnail sizes with this plugin:
    https://www.ads-software.com/plugins/regenerate-thumbnails/

    Before you use the regenerate thumbnails plugin I think it’s best you create a full backup of your site (images and database): https://codex.www.ads-software.com/WordPress_Backups

    Thread Starter Pranjal

    (@pranjalg)

    Thank you so much Keesiemeijer, for your amazing support, and wonderful plugin ??

    Plugin Author keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you like it and that your issue is resolved ??

    Thread Starter Pranjal

    (@pranjalg)

    Hey there, I was wondering on how to make the related posts with thumbnails to show with hyperlinked title?

    To remind you again, I am using this code to display related post with thumbnails. It works without a flaw. But, the problem is that the related posts shown with this code, only shows the images linked to their respective URLs, and the post title displayed in a stripped plain texts. I just want these related posts titles to be displayed, hyperlinked, along with the post images. I hope you’re getting my issue, please tell me how to do so?

    <?php
    // first get the related post objects with the km_rpbt_related_posts_by_taxonomy() function.
    
    // check if the function already exists
    if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) {
    
        $args = array(
            'posts_per_page' => 10,
            'post_thumbnail' => true, // only gets the posts that have a post thumbnail
        );
    
        $taxonomies = array( 'movie' );
        $post_id =  $post->ID; // post id available inside the loop.
    
        $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id , $taxonomies, $args );
    
        // check if related post were found
        if ( $related_posts ) {
    
            $args = array(
                'columns'    => 2,
                'size'       => 'thumbnail',
            );
    
            // display the related post thumbnail gallery
    
            echo km_rpbt_related_posts_by_taxonomy_gallery( $args, $related_posts );
        }
    }
    	?>

    Thanks!

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Right now this is not possible, but I’m working on version 0.2.1 of the plugin which will also add a new filter to alter the thumbnail (and post title) output. I think it will be ready in the comming weeks.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    @pranjalg

    In the new version (0.2.1) you can change the caption to also link to the related posts.
    See this example: https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/filters/#related_posts_by_taxonomy_caption

    Thread Starter Pranjal

    (@pranjalg)

    Thanks, I had just implemented this filter ??

    Works perfectly!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘How to use PHP code to display related post with thumbnails’ is closed to new replies.