Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Related posts? WordPress

Please can someone modify the following so that it gets 3 posts from the current category and displays each post_thumbnail in a column. This currently gets the category list and displays it over 3 columns.

<?php

$get_cats = wp_list_categories( 'orderby=name&echo=0&depth=1&show_count=1&title_li=' );

$cat_array = explode('</li>',$get_cats);

$results_total = count($cat_array);

$cats_per_list = ceil($results_total / 4);

$list_number = 1;

$result_number = 0;
?>

<div class="cat-col-<?php echo $list_number; ?>">

<?php
foreach($cat_array as $category) {
$result_number++;

if($result_number % $cats_per_list == 0) {
$list_number++;
echo $category.'</li>
</div>

<div class="cat-col-'.$list_number.'">';

}

else {

echo $category.'</li>';

}

} ?>

Answers (3)

2014-02-18

Sébastien | French WordpressDesigner answers:

Maybe could you paste the full code.

Maybe could you replace your code
<?php
$get_cats = wp_list_categories( 'orderby=name&echo=0&depth=1&show_count=1&title_li=' );
$cat_array = explode('</li>',$get_cats);
$results_total = count($cat_array);
$cats_per_list = ceil($results_total / 4);
$list_number = 1;
$result_number = 0;
?>
<div class="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number % $cats_per_list == 0) {
$list_number++;
echo $category.'</li>
</div>
<div class="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
} ?>


by


this code



<?php
$get_cats = wp_list_categories( 'orderby=name&echo=0&depth=1&show_count=1&title_li=' );
$cat_array = explode('</li>',$get_cats);
$results_total = count($cat_array);
$cats_per_list = ceil($results_total / 4);
$list_number = 1;
$result_number = 0;
?>
<div class="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
echo $category.'</li>';
} ?>


julesphoto comments:

Hi, I don't have any code other than what i have already posted. The code I posted displays the category_list in 3 columns. I want some code that will display 3 related posts in columns. So it needs to check the current cat and then display the post_thumb in each column.


julesphoto comments:

please could you start it as follows:

function show_related_posts($postID) {

global $post;

$orig_post = $post;
$categories = get_the_category($postID);

if ($categories)

{


etc


Sébastien | French WordpressDesigner comments:

you can read this post : http://www.hongkiat.com/blog/wordpress-related-posts-without-plugins/
or use this plugin :
http://wordpress.org/plugins/wordpress-23-related-posts-plugin/


Sébastien | French WordpressDesigner comments:

add this in functions.php

add_theme_support( 'post-thumbnails' );


addd this, where you want display your related posts :

<div class="relatedposts">
<h3>Related posts</h3>
<?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;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>3, // Number of related posts to display.
'caller_get_posts'=>1
);

$my_query = new wp_query( $args );

while( $my_query->have_posts() ) {
$my_query->the_post();
?>

<div class="relatedthumb">
<a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(); ?><br />
<?php the_title(); ?>
</a>
</div>

<? }
}
$post = $orig_post;
wp_reset_query();
?>
</div>


Sébastien | French WordpressDesigner comments:

if
add_theme_support( 'post-thumbnails' );
is already in functions.php
don't paste it in your functions.php


Sébastien | French WordpressDesigner comments:

and add this code in your style.css

.relatedposts {width: 640px; margin: 0 0 20px 0; float: left; font-size: 12px;}
.relatedposts h3 {font-size: 20px; margin: 0 0 5px 0; }
.relatedthumb {margin: 0 1px 0 1px; float: left; }
.relatedthumb img {margin: 0 0 3px 0; padding: 0;}
.relatedthumb a {color :#333; text-decoration: none; display:block; padding: 4px; width: 150px;}
.relatedthumb a:hover {background-color: #ddd; color: #000;}


Sébastien | French WordpressDesigner comments:

You could send me your backoffice identifiant, and I look at this problem.

2014-02-18

Arnav Joy answers:

try this

<?php



$get_cats = wp_list_categories( 'orderby=name&echo=0&depth=1&show_count=1&title_li=' );



$cat_array = explode('</li>',$get_cats);



$results_total = count($cat_array);



$cats_per_list = ceil($results_total / 4);



$list_number = 1;



$result_number = 0;

?>



<div class="cat-col-<?php echo $list_number; ?>">



<?php

foreach($cat_array as $category) {

$result_number++;



if($result_number % $cats_per_list == 0) {

$list_number++;

echo $category.'</li>

</div>



<div class="cat-col-'.$list_number.'">';



}



else {



echo $category.'</li>';



}
wp_reset_query();
query_posts('cat='.$category.'&posts_per_page=3');
if( have_posts() ) :
echo '<ul>';
while( have_posts() );the_post();
echo '<li><span>'.get_the_post_thumbnail().'</span>'.get_the_title().'</li>';
endwhile;
echo '</ul>';
endif;
wp_reset_query();
} ?>

2014-02-20

Bob answers:

Here is function that might help.
let me know if this function give you related posts.
if it works then we need to add only css to manage it.


function show_related_posts($title = 'Related Posts', $count = 3) {

global $post;

$tag_ids = array();

$current_cat = get_the_category($post->ID);
$current_cat = $current_cat[0]->cat_ID;
$this_cat = '';

$tags = get_the_tags($post->ID);

if ( $tags ) {
foreach($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
} else {
$this_cat = $current_cat;
}

$args = array(
'post_type' => get_post_type(),
'numberposts' => $count,
'orderby' => 'rand',
'tag__in' => $tag_ids,
'cat' => $this_cat,
'exclude' => $post->ID
);

$related_posts = get_posts($args);

/**
* If the tags are only assigned to this post try getting
* the posts again without the tag__in arg and set the cat
* arg to this category.
*/
if ( empty($related_posts) ) {
$args['tag__in'] = '';
$args['cat'] = $current_cat;
$related_posts = get_posts($args);
}

if ( empty($related_posts) ) {
return;
}

$post_list = '';

foreach($related_posts as $related) {
$img = "";

if(has_post_thumbnail( $related->ID ))
$img = get_the_post_thumbnail($related->ID, 'thumbnail');

$post_list .= '<li><a href="' . get_permalink($related->ID) . '">' . $img .'<br />'. $related->post_title . '</a></li>';
}

return sprintf('
<div class="related-posts">
<h4>%s</h4>
<ul>%s</ul>
</div> <!-- .related-posts -->
', $title, $post_list );
}


You need to add <strong>show_related_posts();</strong> in single.php


Bob comments:

add above function in functions.php and you need to add

echo show_related_posts(); in single.php

not only

show_related_posts();

I have tested above function in localhost and it works.
only require is adding some css to match it with your theme and arrange unordered list of related posts.