You are viewing an old revision of this post, from April 29, 2014 @ 14:13:40. See below for differences between this version and the current revision.

WordPress Most Related Posts in 4 Steps

Original found here: http://codeitdown.com/wordpress-related-posts/

WordPress Most Related Posts in 4 Steps

To implement a WordPress Related Posts section that displays the posts with the highest number of common tags follow the next steps:
  1. Choose or declare a thumbnail size. You'll probably want to show thumbnails like the ones above. Those are the posts' featured images. You'll have to add featured images to your posts if they don't already have. You don't want to display them in full size. Loading them will slow down your site and WordPress can do the resizing automatically. You can either use a WordPress default image size or define your own. By default, WordPress resizes every image to 150px x 150px, 300px x 300px max and 640px x 640px max. You can customize the sizes in Settings -> Media but then you'll have to regenerate your thumbnails (see next step).The cropping option is only offered for the 150px x 150px thumbnail (in Settings -> Media). Cropped images are shrinked to fit the shortest dimension and then the sides are cropped. If the image has the same aspect ratio as the thumbnail size, nothing is cropped. If cropping is off, images are always shown complete. However, you'll get shorter or narrower thumbnails for featured images that don't fit the aspect ratio. It will never happen if cropping is enabled.You can skip to the next step if you are happy with one of the default sizes and cropping setting. To enable cropping for the 300px x 300px image see Force crop to Medium size.If you want to define your own custom size, you can do it by adding the lines:
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'related-thumb',240,240,true );
    to your theme's functions.php file. The first line should already be there if you are using featured images. Change the two numbers to the width and height (respectively) you'd like the thumbnails to have. Change the last parameter to false to turn off cropping.
  2. Regenerate your thumbnails. You'll have to do it unless you are using a default size without changing the cropping setting. When you change thumbnail sizes or declare your own, existing thumbnails are not regenerated. The Regenerate Thumbnails plugin will recreate them for you. The alternative is re-uploading each and every featured image, which is probably not an option.
  3. Add the following code to your themes single.php. Paste it where you want your related posts to appear, within The Loop:
    [php]<!-- Related Posts --> <?php $orig_post = $post; global $post; $tags = wp_get_post_tags($post->ID); if ($tags): $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $number_of_posts = 4; // number of posts to display $query = " SELECT ".$wpdb->posts.".*, COUNT(".$wpdb->posts.".ID) as q FROM ".$wpdb->posts." INNER JOIN ".$wpdb->term_relationships." ON (".$wpdb->posts.".ID = ".$wpdb->term_relationships.".object_id) WHERE ".$wpdb->posts.".ID NOT IN (".$post->ID.") AND ".$wpdb->term_relationships.".term_taxonomy_id IN (".implode(",",$tag_ids).") AND ".$wpdb->posts.".post_type = 'post' AND ".$wpdb->posts.".post_status = 'publish' GROUP BY ".$wpdb->posts.".ID ORDER BY q DESC LIMIT ".$number_of_posts.""; $related_posts = $wpdb->get_results($query, OBJECT); if($related_posts): ?> <div class="related-posts"> <h3>Related posts</h3> <?php foreach($related_posts as $post): ?> <?php setup_postdata($post); ?> <div class="related-thumb"> <a href="<?php the_permalink()?>"> <?php the_post_thumbnail('medium'); ?><br/> <?php the_title(); ?> </a> </div> <?php endforeach; ?> </div> <?php endif; endif; $post = $orig_post; wp_reset_query(); ?>[/php]
    This code retrieves the most related posts by tag, from any category. Use the_post_thumbnail('my-custom-thumbnail-size') instead of the_post_thumbnail('medium') if you defined a custom thumbnail size. You may use 'thumbnail', 'medium', 'large' or 'full' as well. Use loop functions likethe_excerpt()the_author()the_date() and the_category() to output more details. To narrow down and show only posts from the current post's category, you'd have to add a bit of PHP using wp_get_post_categories or wp_get_post_terms to get all the associated terms and add them to the term_taxonomy_id IN clause (categories and tags are roughly the same internally).
  4. Give Related Posts some style. You could start by adding the following CSS to your theme's style.css:
    [css] .related-posts:after { /* clearfix */ content: " "; visibility: hidden; display: block; height: 0; clear: both; } .related-thumb { width: 25%; padding: 1%; float: left; } .related-thumb a { display: block; } .related-thumb img { width: 100%; height: auto; } [/css]
    This CSS is for an ultra-simple layout like the one in the images above. Add your styles to make your WordPress related posts look great!
If you have any suggestions and/or questions, please leave them in the comments below. I'll be glad to answer and improve the article.

Revisions

Revision Differences

April 29, 2014 @ 14:13:40Current Revision
Content
Unchanged: Original found here: <a href="http:// codeitdown.com/wordpress- related-posts/" target="_blank" >http://codeitdown.com/wordpress- related-posts/</a>Unchanged: Original found here: <a href="http:// codeitdown.com/wordpress- related-posts/" target="_blank" >http://codeitdown.com/wordpress- related-posts/</a>
Unchanged: <h2>WordPress Most Related Posts in 4 Steps</h2>Unchanged: <h2>WordPress Most Related Posts in 4 Steps</h2>
Unchanged: To implement a WordPress Related Posts section that displays the posts with the highest number of common tags follow the next steps:Unchanged: To implement a WordPress Related Posts section that displays the posts with the highest number of common tags follow the next steps:
Unchanged: <ol>Unchanged: <ol>
Unchanged: <li><strong>Choose or declare a thumbnail size</strong>. You'll probably want to show thumbnails like the ones above. Those are the posts' featured images. You'll have to add featured images to your posts if they don't already have. You don't want to display them in full size. Loading them will slow down your site and WordPress can do the resizing automatically. You can either use a WordPress default image size or define your own. By default, WordPress resizes every image to 150px x 150px, 300px x 300px max and 640px x 640px max. You can customize the sizes in Settings -&gt; Media but then you'll have to regenerate your thumbnails (see next step).The cropping option is only offered for the 150px x 150px thumbnail (in Settings -&gt; Media). Cropped images are shrinked to fit the shortest dimension and then the sides are cropped. If the image has the same aspect ratio as the thumbnail size, nothing is cropped. If cropping is off, images are always shown complete. However, you'll get shorter or narrower thumbnails for featured images that don't fit the aspect ratio. It will never happen if cropping is enabled.You can skip to the next step if you are happy with one of the default sizes and cropping setting. To enable cropping for the 300px x 300px image see <a title="WordPress Support question on cropping medium size images" href="http:// wordpress.org/ support/topic/ force-crop-to- medium-size">Force crop to Medium size</a>.If you want to define your own custom size, you can do it by adding the lines:Unchanged: <li><strong>Choose or declare a thumbnail size</strong>. You'll probably want to show thumbnails like the ones above. Those are the posts' featured images. You'll have to add featured images to your posts if they don't already have. You don't want to display them in full size. Loading them will slow down your site and WordPress can do the resizing automatically. You can either use a WordPress default image size or define your own. By default, WordPress resizes every image to 150px x 150px, 300px x 300px max and 640px x 640px max. You can customize the sizes in Settings -&gt; Media but then you'll have to regenerate your thumbnails (see next step).The cropping option is only offered for the 150px x 150px thumbnail (in Settings -&gt; Media). Cropped images are shrinked to fit the shortest dimension and then the sides are cropped. If the image has the same aspect ratio as the thumbnail size, nothing is cropped. If cropping is off, images are always shown complete. However, you'll get shorter or narrower thumbnails for featured images that don't fit the aspect ratio. It will never happen if cropping is enabled.You can skip to the next step if you are happy with one of the default sizes and cropping setting. To enable cropping for the 300px x 300px image see <a title="WordPress Support question on cropping medium size images" href="http:// wordpress.org/ support/topic/ force-crop-to- medium-size">Force crop to Medium size</a>.If you want to define your own custom size, you can do it by adding the lines:
Unchanged: <pre title="PHP">add_ theme_support( 'post-thumbnails' );Unchanged: <pre title="PHP">add_ theme_support( 'post-thumbnails' );
Unchanged: add_image_size( 'related-thumb',240,240,true );</pre>Unchanged: add_image_size( 'related-thumb',240,240,true );</pre>
Unchanged: to your theme's functions.php file. The first line should already be there if you are using featured images. Change the two numbers to the width and height (respectively) you'd like the thumbnails to have. Change the last parameter to false to turn off cropping.</li>Unchanged: to your theme's functions.php file. The first line should already be there if you are using featured images. Change the two numbers to the width and height (respectively) you'd like the thumbnails to have. Change the last parameter to false to turn off cropping.</li>
Unchanged: <li><strong>Regenerate your thumbnails</strong>. You'll have to do it unless you are using a default size without changing the cropping setting. When you change thumbnail sizes or declare your own, existing thumbnails are not regenerated. The <a title="WordPress Regenerate Thumnails plugin page" href="http:// wordpress.org/ plugins/regenerate- thumbnails/">Regenerate Thumbnails plugin</a> will recreate them for you. The alternative is re-uploading each and every featured image, which is probably not an option.</li>Unchanged: <li><strong>Regenerate your thumbnails</strong>. You'll have to do it unless you are using a default size without changing the cropping setting. When you change thumbnail sizes or declare your own, existing thumbnails are not regenerated. The <a title="WordPress Regenerate Thumnails plugin page" href="http:// wordpress.org/ plugins/regenerate- thumbnails/">Regenerate Thumbnails plugin</a> will recreate them for you. The alternative is re-uploading each and every featured image, which is probably not an option.</li>
Unchanged: <li>Add the following code to your themes single.php. Paste it where you want your related posts to appear, within <a title="The Loop Codex doc" href="http:// codex.wordpress.org/ The_Loop">The Loop</a>:Unchanged: <li>Add the following code to your themes single.php. Paste it where you want your related posts to appear, within <a title="The Loop Codex doc" href="http:// codex.wordpress.org/ The_Loop">The Loop</a>:
Unchanged: <div>Unchanged: <div>
Unchanged: <div id="highlighter_ 257441">[php]&lt;!-- Related Posts --&gt;Unchanged: <div id="highlighter_ 257441">[php]&lt;!-- Related Posts --&gt;
Deleted: &lt;?php $orig_post = $post; Added: &lt;?php $orig_post = $post;
Deleted: global $post; Added: global $post;
Deleted: $tags = wp_get_post_tags( $post-&gt;ID); Added: $tags = wp_get_post_tags( $post-&gt;ID);
Deleted:  
Unchanged: if ($tags):Unchanged: if ($tags):
Deleted: $tag_ids = array(); Added: $tag_ids = array();
Unchanged: foreach($tags as $individual_tag) $tag_ids[] = $individual_tag- &gt;term_id;Unchanged: foreach($tags as $individual_tag) $tag_ids[] = $individual_tag- &gt;term_id;
Unchanged: $number_of_posts = 4; // number of posts to displayUnchanged: $number_of_posts = 4; // number of posts to display
Unchanged: $query = &quot;Unchanged: $query = &quot;
Unchanged: SELECT &quot;.$wpdb- &gt;posts.&quot;.*, COUNT(&quot;.$wpdb- &gt;posts.&quot;.ID) as qUnchanged: SELECT &quot;.$wpdb- &gt;posts.&quot;.*, COUNT(&quot;.$wpdb- &gt;posts.&quot;.ID) as q
Unchanged: FROM &quot;.$wpdb- &gt;posts.&quot; INNER JOIN &quot;.$wpdb- &gt;term_relationships.&quot;Unchanged: FROM &quot;.$wpdb- &gt;posts.&quot; INNER JOIN &quot;.$wpdb- &gt;term_relationships.&quot;
Unchanged: ON (&quot;.$wpdb- &gt;posts.&quot;.ID = &quot;.$wpdb- &gt;term_relationships.&quot;.object_id)Unchanged: ON (&quot;.$wpdb- &gt;posts.&quot;.ID = &quot;.$wpdb- &gt;term_relationships.&quot;.object_id)
Unchanged: WHERE &quot;.$wpdb- &gt;posts.&quot;.ID NOT IN (&quot;.$post- &gt;ID.&quot;)Unchanged: WHERE &quot;.$wpdb- &gt;posts.&quot;.ID NOT IN (&quot;.$post- &gt;ID.&quot;)
Unchanged: AND &quot;.$wpdb- &gt;term_relationships.&quot; .term_taxonomy_id IN (&quot;.implode( &quot;,&quot; ,$tag_ids).&quot;)Unchanged: AND &quot;.$wpdb- &gt;term_relationships.&quot; .term_taxonomy_id IN (&quot;.implode( &quot;,&quot; ,$tag_ids).&quot;)
Unchanged: AND &quot;.$wpdb- &gt;posts.&quot;.post_type = 'post'Unchanged: AND &quot;.$wpdb- &gt;posts.&quot;.post_type = 'post'
Unchanged: AND &quot;.$wpdb- &gt;posts.&quot; .post_status = 'publish'Unchanged: AND &quot;.$wpdb- &gt;posts.&quot; .post_status = 'publish'
Unchanged: GROUP BY &quot;.$wpdb- &gt;posts.&quot;.IDUnchanged: GROUP BY &quot;.$wpdb- &gt;posts.&quot;.ID
Unchanged: ORDER BY qUnchanged: ORDER BY q
Unchanged: DESC LIMIT &quot;.$number_ of_posts.&quot;&quot;;Unchanged: DESC LIMIT &quot;.$number_ of_posts.&quot;&quot;;
Deleted: $related_posts = $wpdb-&gt;get_ results($query, OBJECT); Added: $related_posts = $wpdb-&gt;get_ results($query, OBJECT);
Unchanged: if($related_posts): ?&gt;Unchanged: if($related_posts): ?&gt;
Deleted: &lt;div class=&quot;related- posts&quot;&gt; Added: &lt;div class=&quot;related- posts&quot;&gt;
Deleted: &lt;h3&gt;Related posts&lt;/h3&gt; Added: &lt;h3&gt;Related posts&lt;/h3&gt;
Unchanged: &lt;?php foreach($related_posts as $post): ?&gt;Unchanged: &lt;?php foreach($related_posts as $post): ?&gt;
Unchanged: &lt;?php setup_postdata($post); ?&gt;Unchanged: &lt;?php setup_postdata($post); ?&gt;
Deleted: &lt;div class=&quot;related- thumb&quot;&gt; Added: &lt;div class=&quot;related- thumb&quot;&gt;
Unchanged: &lt;a href=&quot;&lt;?php the_permalink( )?&gt;&quot;&gt;Unchanged: &lt;a href=&quot;&lt;?php the_permalink( )?&gt;&quot;&gt;
Unchanged: &lt;?php the_post_thumbnail('medium'); ?&gt;&lt;br/&gt;Unchanged: &lt;?php the_post_thumbnail('medium'); ?&gt;&lt;br/&gt;
Unchanged: &lt;?php the_title(); ?&gt;Unchanged: &lt;?php the_title(); ?&gt;
Deleted: &lt;/a&gt; Added: &lt;/a&gt;
Deleted: &lt;/div&gt; Added: &lt;/div&gt;
Unchanged: &lt;?php endforeach; ?&gt;Unchanged: &lt;?php endforeach; ?&gt;
Unchanged: &lt;/div&gt;Unchanged: &lt;/div&gt;
Unchanged: &lt;?php endif;Unchanged: &lt;?php endif;
Unchanged: endif;Unchanged: endif;
Deleted: $post = $orig_post; Added: $post = $orig_post;
Deleted: wp_reset_query(); ?&gt;[/php]</div> Added: wp_reset_query(); ?&gt;[/php]
Unchanged: </div>Unchanged: </div>
Unchanged: </div>Unchanged: </div>
Unchanged: This code retrieves the most related posts by tag, from any category. Use <code><a title="the_post_thumbnail in WordPress Codex" href="https:/ /codex.wordpress.org/Function_ Reference/the_ post_thumbnail" >the_post_thumbnail</a>('my- custom-thumbnail-size')</ code> instead of <code>the_ post_thumbnail( 'medium')</code> if you defined a custom thumbnail size. You may use 'thumbnail', 'medium', 'large' or 'full' as well. Use loop functions like<a title="the_excerpt in WP Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ excerpt">the_ excerpt()</a>, <a title="the_author in WP Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ author">the_ author()</a>, <a title="the_date in WordPress Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ date">the_date( )</a> and <a title="the_category in WordPress Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ category">the_ category()</a> to output more details.Unchanged: This code retrieves the most related posts by tag, from any category. Use <code><a title="the_post_thumbnail in WordPress Codex" href="https:/ /codex.wordpress.org/Function_ Reference/the_ post_thumbnail" >the_post_thumbnail</a>('my- custom-thumbnail-size')</ code> instead of <code>the_ post_thumbnail( 'medium')</code> if you defined a custom thumbnail size. You may use 'thumbnail', 'medium', 'large' or 'full' as well. Use loop functions like<a title="the_excerpt in WP Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ excerpt">the_ excerpt()</a>, <a title="the_author in WP Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ author">the_ author()</a>, <a title="the_date in WordPress Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ date">the_date( )</a> and <a title="the_category in WordPress Codex" href="http:// codex.wordpress.org/Function_ Reference/the_ category">the_ category()</a> to output more details.
Unchanged: To narrow down and show only posts from the current post's category, you'd have to add a bit of PHP using wp_get_post_categories or wp_get_post_terms to get all the associated terms and add them to the <code>term_taxonomy_id IN</code> clause (categories and tags are roughly the same internally).</li>Unchanged: To narrow down and show only posts from the current post's category, you'd have to add a bit of PHP using wp_get_post_categories or wp_get_post_terms to get all the associated terms and add them to the <code>term_taxonomy_id IN</code> clause (categories and tags are roughly the same internally).</li>
Unchanged: <li><strong>Give Related Posts some style</strong>. You could start by adding the following CSS to your theme's style.css:Unchanged: <li><strong>Give Related Posts some style</strong>. You could start by adding the following CSS to your theme's style.css:
Unchanged: <div>Unchanged: <div>
 Added: <div id="highlighter_ 135036">[css]
Deleted: <div id="highlighter_ 135036"><code> </code>[css]&lt; code&gt;.related-posts:after { &lt;/code&gt; &lt;code&gt;/* clearfix */&lt;/code&gt;Added: .related-posts:after { /* clearfix */
Deleted: &lt;div&gt;&lt; code&gt;   &lt;/code&gt; &lt;code&gt;content&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;&quot; &quot;&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;   &lt;/code&gt; &lt;code&gt;visibility&lt; /code&gt;&lt;code&gt;: &lt;/code&gt; &lt;code&gt;hidden&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
 Added: content: &quot; &quot;;
 Added: visibility: hidden;
 Added: display: block;
 Added: height: 0;
 Added: clear: both;
 Added: }
 Added: .related-thumb {
 Added: width: 25%;
 Added: padding: 1%;
 Added: float: left;
 Added: }
 Added: .related-thumb a {
Deleted: &lt;div&gt;&lt; code&gt;   &lt;/code&gt; &lt;code&gt;display&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;block&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt;Added: display: block;
Deleted: &lt;div&gt;&lt; code&gt;   &lt;/code&gt; &lt;code&gt;height&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;0&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;   &lt;/code&gt; &lt;code&gt;clear&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;both&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;}&lt; /code&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;.related-thumb {&lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;    &lt;/code&gt; &lt;code&gt;width&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;25%&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;    &lt;/code&gt; &lt;code&gt;padding&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;1%&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;    &lt;/code&gt; &lt;code&gt;float&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;left&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;}&lt; /code&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;.related-thumb a {&lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;    &lt;/code&gt; &lt;code&gt;display&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;block&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;}&lt; /code&gt;&lt;/div&gt; 
 Added: }
Deleted: &lt;div&gt;&lt; code&gt;.related-thumb img {&lt;/code&gt; &lt;/div&gt;Added: .related-thumb img {
Deleted: &lt;div&gt;&lt; code&gt;    &lt;/code&gt; &lt;code&gt;width&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;100%&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt;Added: width: 100%;
Deleted: &lt;div&gt;&lt; code&gt;    &lt;/code&gt; &lt;code&gt;height&lt;/code&gt; &lt;code&gt;: &lt;/code&gt; &lt;code&gt;auto&lt;/code&gt; &lt;code&gt;; &lt;/code&gt; &lt;/div&gt; 
Deleted: &lt;div&gt;&lt; code&gt;}&lt; /code&gt;&lt;/div&gt; 
 Added: height: auto;
 Added: }
Unchanged: [/css]Unchanged: [/css]
Deleted: <div></div> 
Unchanged: </div>Unchanged: </div>
Unchanged: </div>Unchanged: </div>
Unchanged: This CSS is for an ultra-simple layout like the one in the images above. Add your styles to make your WordPress related posts look great!</li>Unchanged: This CSS is for an ultra-simple layout like the one in the images above. Add your styles to make your WordPress related posts look great!</li>
Unchanged: </ol>Unchanged: </ol>
Unchanged: If you have any suggestions and/or questions, please leave them in the comments below. I'll be glad to answer and improve the article.Unchanged: If you have any suggestions and/or questions, please leave them in the comments below. I'll be glad to answer and improve the article.

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags:

No comments yet.

Leave a Reply