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

Related posts by custom field script WordPress

  • SOLVED

I found this script/tute by Justin Tadlock - http://justintadlock.com/archives/2007/11/01/wordpress-custom-fields-listing-a-series-of-posts

I would like to modify it to show 5 recent posts that match the value of a custom field ('edit_cat') WITH thumbnails.

below is where I have got too, although this shows 5 recent post that HAVE a value in the custom field (not ones that match), it also has a weird 'current post' value which isn't a link (i'd like to get rid of this if possible) AND the thumbnails are all the same.. So basically it's a mess. Please help!

Also I am using Thesis which is why this is set up like it is.


/***** Reader edit item related cat_edit***/
function article_series() {

global $post;
$series = get_post_meta($post->ID, 'edit_cat', true);
if($series) :
$args = array(
'numberposts' => -1,
'meta_key' => 'edit_cat',
'meta_value' => $series,
);

$series_posts = get_posts('numberposts=5&'.$args);
foreach($series_posts as $post)
setup_postdata($post);
$image_link = get_post_image($post);
if($series_posts) :
$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);
$class = strtolower(str_replace(array(' ', ' '), '-', $class));
echo '<div id="dshop-latest-list"><h4 class="series-title">' . __('More like this') . '</h4><ul>';
foreach($series_posts as $serial) :
if($serial->ID == $post->ID)
echo '<li class="current-post">' . $serial->post_title . '</li>';
else
echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($serial->ID) . '" title="' . str_replace('"', '"', $serial->post_title) . '">' . str_replace('"', '"', $serial->post_title) . '</a></li>';
endforeach;
echo '</ul></div>';
endif;
endif;
}

function related_cat_edit() {
if (is_single()) {
if(in_category('4008')) {
?>
<?php article_series();?>
<?php
}
}
}
add_action('thesis_hook_after_post', 'related_cat_edit');

Answers (3)

2011-04-28

Christianto answers:

My thought..
for the thumbnail you should pass content to get_post_image..
$image_link = get_post_image($post->content);

let me check other..


Christianto comments:

BTW, what did you mean by <em>'it also has a weird 'current post' value which isn't a link (i'd like to get rid of this if possible)'</em>?


kateM82 comments:

Hi Christianto,

Have a look at this link and you'll see. It's also on the demo of the tute on Justin tadlocks link.

There is one title in the list which it thinks is the current page (in my case it's not) and so it's not a link. I don't want to show the current page in my list at all.


Christianto comments:

Untested... but please check..

function article_series() {

global $post;

$currentID = $post->ID;

$series = get_post_meta($post->ID, 'edit_cat', true);

if($series) :

$args = array(

'numberposts' => 5,

'meta_key' => 'edit_cat',

'meta_value' => $series,

);



$series_posts = get_posts($args);

echo '<div id="dshop-latest-list"><h4 class="series-title">' . __('More like this') . '</h4><ul>';

foreach($series_posts as $post){

if($post) :

setup_postdata($post);

$image_link = get_post_image($post->content);

$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);

$class = strtolower(str_replace(array(' ', '&nbsp;'), '-', $class));

if($currentID == $post->ID)

echo '<li class="current-post">' . $post->post_title . '</li>';

else

echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

endif;

}

echo '</ul></div>';

endif;

}



function related_cat_edit() {

if (is_single() || in_category('4008'))) {

article_series();

}

}

add_action('thesis_hook_after_post', 'related_cat_edit');


kateM82 comments:

Very close... although I had to put back in
$image_link = get_post_image($post);

to make the images show.

but there is still that weird 'current' post. See link

http://dropdeadgorgeousdaily.com/2011/04/grey-marle/


Christianto comments:

Did you mean by weird 'current' post is 'Grey marle' text with no images??


kateM82 comments:

Yes, that's the one. And I have updated the if current post function to fix that, but i'd rather current posts don't show at all.

here is what I have working so far (change post number to 4 so it fit for now)



/***** Reader edit item related cat_edit***/


function article_series() {

global $post;

$currentID = $post->ID;

$series = get_post_meta($post->ID, 'edit_cat', true);

if($series) :

$args = array(

'numberposts' => 4,

'meta_key' => 'edit_cat',

'meta_value' => $series,

);



$series_posts = get_posts($args);


echo '<div id="dshop-latest-list"><h4 class="series-title">' . __('More like this') . '</h4><ul>';

foreach($series_posts as $post){


if($post) :

setup_postdata($post);

$image_link = get_post_image($post);

//$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);//

//$class = strtolower(str_replace(array(' ', '&nbsp;'), '-', $class));//

if($currentID == $post->ID)

echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

else

echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

endif;

}

echo '</ul></div>';

endif;
}

function related_cat_edit() {
if (is_single()) {
if(in_category('4008')) {

article_series();

}
}

}

add_action('thesis_hook_after_post', 'related_cat_edit');



Christianto comments:

From your code the 'current' post isn't showing any images only title..
if you want the 'current' post showing images with class 'current', please use code below..

function article_series() {

global $post;

$currentID = $post->ID;

$series = get_post_meta($post->ID, 'edit_cat', true);

if($series) :

$args = array(

'numberposts' => 5,

'meta_key' => 'edit_cat',

'meta_value' => $series,

);



$series_posts = get_posts($args);

echo '<div id="dshop-latest-list"><h4 class="series-title">' . __('More like this') . '</h4><ul>';

foreach($series_posts as $post){

if($post) :

setup_postdata($post);

$image_link = get_post_image($post->content);

$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);

$class = strtolower(str_replace(array(' ', '&nbsp;'), '-', $class));

if($currentID == $post->ID)

echo '<li class="current-post"><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

else

echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

endif;

}

echo '</ul></div>';

endif;

}

function related_cat_edit() {

if (is_single() || in_category('4008'))) {

article_series();

}

}

add_action('thesis_hook_after_post', 'related_cat_edit');


Christianto comments:

sorry...
Change the code

$image_link = get_post_image($post->content);


to


$image_link = get_post_image($post);


like you said ^_^


Christianto comments:

Sorry, you don't want to show current page so this the code

function article_series() {

global $post;

$currentID = $post->ID;

$series = get_post_meta($post->ID, 'edit_cat', true);

if($series) :

$args = array(

'numberposts' => 5,

'meta_key' => 'edit_cat',

'meta_value' => $series,

);



$series_posts = get_posts($args);

echo '<div id="dshop-latest-list"><h4 class="series-title">' . __('More like this') . '</h4><ul>';

foreach($series_posts as $post){

if($post) :

setup_postdata($post);

$image_link = get_post_image($post);

$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);

$class = strtolower(str_replace(array(' ', '&nbsp;'), '-', $class));

if($currentID != $post->ID)

echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

endif;

}

echo '</ul></div>';

endif;

}

function related_cat_edit() {

if (is_single() || in_category('4008'))) {

article_series();

}

}

add_action('thesis_hook_after_post', 'related_cat_edit');


kateM82 comments:

still showing current post, but I can live with that, thanks for you help


Christianto comments:

Did you use my last code?

2011-04-28

Julian Lannigan answers:

Hi Kate,

I cleaned up your code a bit. You had some logic blocks that were incomplete and they may be the cause of your extra output. See below:

function article_series() {
global $post;
$series = get_post_meta($post->ID, 'edit_cat', true);
if ($series) {
$args = array(
'numberposts' => 5,
'meta_key' => 'edit_cat',
'meta_value' => $series,
);
$series_posts = get_posts($args);

foreach($series_posts as $post) {
setup_postdata($post);
$image_link = get_post_image($post);

if ($series_posts) {
$class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);
$class = strtolower(str_replace(array(' ', '&nbsp;'), '-', $class));
echo '<div id="dshop-latest-list"><h4 class="series-title">' . __('More like this') . '</h4><ul>';
foreach($series_posts as $serial) {
if($serial->ID == $post->ID) {
echo '<li class="current-post">' . $serial->post_title . '</li>';
} else {
echo '<li><img style="margin:0px 10px 10px 0px; height: 100px; width: 100px; display: block; float:left;" src="' . $image_link . '" alt="All the latest"/><a href="' . get_permalink($serial->ID) . '" title="' . str_replace('"', '"', $serial->post_title) . '">' . str_replace('"', '"', $serial->post_title) . '</a></li>';
}
}
echo '</ul></div>';
}
}
}
}

function related_cat_edit() {
if (is_single() && in_category('4008')) {
article_series();
}
}
add_action('thesis_hook_after_post', 'related_cat_edit');


kateM82 comments:

Hi Julian,
Getting closer i tihnk, although this output five lots of five posts see pic.

You can see it live here - http://dropdeadgorgeousdaily.com/2011/04/magenta/