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:- 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. - 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.
- 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]
the_post_thumbnail('my-custom-thumbnail-size')
instead ofthe_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 theterm_taxonomy_id IN
clause (categories and tags are roughly the same internally). - 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]
Revisions
- April 29, 2014 @ 14:15:54 [Current Revision] by PeterLugg
- April 29, 2014 @ 14:15:54 by PeterLugg
- April 29, 2014 @ 14:15:21 by PeterLugg
- April 29, 2014 @ 14:14:56 by PeterLugg
- April 29, 2014 @ 14:13:40 by PeterLugg
- April 29, 2014 @ 14:11:35 by PeterLugg
Revision Differences
April 29, 2014 @ 14:15:21 | Current 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 -> 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 <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 -> 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 <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]<!-- Related Posts --> | Unchanged: <div id="highlighter_ 257441">[php]<!-- Related Posts --> | ||
Deleted: <?php $orig_post = $post; | Added: <?php $orig_post = $post; | ||
Deleted: global $post; | Added: global $post; | ||
Deleted: $tags = wp_get_post_tags( $post->ID); | Added: $tags = wp_get_post_tags( $post->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- >term_id; | Unchanged: foreach($tags as $individual_tag) $tag_ids[] = $individual_tag- >term_id; | ||
Unchanged: $number_of_posts = 4; // number of posts to display | Unchanged: $number_of_posts = 4; // number of posts to display | ||
Unchanged: $query = " | Unchanged: $query = " | ||
Unchanged: SELECT ".$wpdb- >posts.".*, COUNT(".$wpdb- >posts.".ID) as q | Unchanged: SELECT ".$wpdb- >posts.".*, COUNT(".$wpdb- >posts.".ID) as q | ||
Unchanged: FROM ".$wpdb- >posts." INNER JOIN ".$wpdb- >term_relationships." | Unchanged: FROM ".$wpdb- >posts." INNER JOIN ".$wpdb- >term_relationships." | ||
Unchanged: ON (".$wpdb- >posts.".ID = ".$wpdb- >term_relationships.".object_id) | Unchanged: ON (".$wpdb- >posts.".ID = ".$wpdb- >term_relationships.".object_id) | ||
Unchanged: WHERE ".$wpdb- >posts.".ID NOT IN (".$post- >ID.") | Unchanged: WHERE ".$wpdb- >posts.".ID NOT IN (".$post- >ID.") | ||
Unchanged: AND ".$wpdb- >term_relationships." .term_taxonomy_id IN (".implode( "," ,$tag_ids).") | Unchanged: AND ".$wpdb- >term_relationships." .term_taxonomy_id IN (".implode( "," ,$tag_ids).") | ||
Unchanged: AND ".$wpdb- >posts.".post_type = 'post' | Unchanged: AND ".$wpdb- >posts.".post_type = 'post' | ||
Unchanged: AND ".$wpdb- >posts." .post_status = 'publish' | Unchanged: AND ".$wpdb- >posts." .post_status = 'publish' | ||
Unchanged: GROUP BY ".$wpdb- >posts.".ID | Unchanged: GROUP BY ".$wpdb- >posts.".ID | ||
Unchanged: ORDER BY q | Unchanged: ORDER BY q | ||
Unchanged: DESC LIMIT ".$number_ of_posts.""; | Unchanged: DESC LIMIT ".$number_ of_posts.""; | ||
Deleted: $related_posts = $wpdb->get_ results($query, OBJECT); | Added: $related_posts = $wpdb->get_ results($query, OBJECT); | ||
Unchanged: if($related_posts): ?> | Unchanged: if($related_posts): ?> | ||
Deleted: <div class="related- posts"> | Added: <div class="related- posts"> | ||
Deleted: <h3>Related posts</h3> | Added: <h3>Related posts</h3> | ||
Unchanged: <?php foreach($related_posts as $post): ?> | Unchanged: <?php foreach($related_posts as $post): ?> | ||
Unchanged: <?php setup_postdata($post); ?> | Unchanged: <?php setup_postdata($post); ?> | ||
Deleted: <div class="related- thumb"> | Added: <div class="related- thumb"> | ||
Unchanged: <a href="<?php the_permalink( )?>"> | Unchanged: <a href="<?php the_permalink( )?>"> | ||
Unchanged: <?php the_post_thumbnail('medium'); ?><br/> | Unchanged: <?php the_post_thumbnail('medium'); ?><br/> | ||
Unchanged: <?php the_title(); ?> | Unchanged: <?php the_title(); ?> | ||
Deleted: </a> | Added: </a> | ||
Deleted: </div> | Added: </div> | ||
Unchanged: <?php endforeach; ?> | Unchanged: <?php endforeach; ?> | ||
Unchanged: </div> | Unchanged: </div> | ||
Unchanged: <?php endif; | Unchanged: <?php endif; | ||
Unchanged: endif; | Unchanged: endif; | ||
Deleted: $post = $orig_post; | Added: $post = $orig_post; | ||
Unchanged: wp_reset_query(); ?>[/php] | Unchanged: wp_reset_query(); ?>[/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> | ||
Deleted: <div id=" | Added: <div id="highlighter_ 135036">[css] | ||
Deleted: [css] | |||
Unchanged: .related-posts:after { /* clearfix */ | Unchanged: .related-posts:after { /* clearfix */ | ||
Deleted: content: " "; | Added: content: " "; | ||
Unchanged: visibility: hidden; | Unchanged: visibility: hidden; | ||
Unchanged: display: block; | Unchanged: display: block; | ||
Unchanged: height: 0; | Unchanged: height: 0; | ||
Unchanged: clear: both; | Unchanged: clear: both; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: .related-thumb { | Unchanged: .related-thumb { | ||
Unchanged: width: 25%; | Unchanged: width: 25%; | ||
Unchanged: padding: 1%; | Unchanged: padding: 1%; | ||
Unchanged: float: left; | Unchanged: float: left; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: .related-thumb a { | Unchanged: .related-thumb a { | ||
Unchanged: display: block; | Unchanged: display: block; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: .related-thumb img { | Unchanged: .related-thumb img { | ||
Unchanged: width: 100%; | Unchanged: width: 100%; | ||
Unchanged: height: auto; | Unchanged: height: auto; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: [/css] | Unchanged: [/css] | ||
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.
No comments yet.