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

Combining Two Functions - WP-PostRatings Plugin WordPress

  • SOLVED

Hello WP Geniuses!
I am working on a site that uses the WP-PostRatings Plugin. I need to merge 2 different functions here to display some images attached to the post.

The code below will output the post content when %POST_THUMB% is placed in the template area in the admin.



if (strpos($template, '%POST_THUMB%') !== false) {
if ($post->ID != $post_id) {
$post = &get_post($post_id);
}
$value = str_replace("%POST_THUMB%", get_the_content(), $value);
}



However, I don't need the content of the post. I have a custom "query" that is bringing in my attachment images or video code. That code is as follows:



<?php if ( get_post_meta($post->ID, 'video', true) ) { ?>
<?php echo get_post_meta($post->ID, "video", $single = true); ?>
<?php } else { ?>

<?php
$attargs = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID);

$attachments = get_children($attargs);
if ($attachments) {
foreach ($attachments as $post) {
?>
<img style="max-width:630px; width:100%;" src="<?php echo wp_get_attachment_url($post->ID); ?>" alt="<?php the_excerpt(); ?>" title="<?php the_title(); ?>" class="staff-img" />
<?php
break; //only display first image
}
}
?>

<?php } ?>



So essentially, what I need is to to put put the second block of code into the first block of code so that I can call %POST_THUMB% and get those attached images (or video). I'm guessing the "get_the_content()" function is where it needs to happen, I just don't have the smarts to do it!

Sure hope someone can help me out!

Thanks guys!
Mike

Answers (3)

2010-05-11

Andrzej answers:

Try this

<?php

if (strpos($template, '%POST_THUMB%') !== false) {
if ($post->ID != $post_id) {
$post = &get_post($post_id);
}

ob_start();
?>
<?php if ( get_post_meta($post->ID, 'video', true) ) { ?>
<?php echo get_post_meta($post->ID, "video", $single = true); ?>
<?php } else { ?>

<?php
$attargs = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID);

$attachments = get_children($attargs);
if ($attachments) {
foreach ($attachments as $post) {
?>
<img style="max-width:630px; width:100%;" src="<?php echo wp_get_attachment_url($post->ID); ?>" alt="<?php the_excerpt(); ?>" title="<?php the_title(); ?>" class="staff-img" />
<?php
break; //only display first image
}
}
?>

<?php } ?>
<?php

$output = ob_get_contents();
ob_end_clean();

$value = str_replace("%POST_THUMB%", $output, $value);
}

?>


Mike McAlister comments:

Andrzej,
You've done it! That worked perfectly, first time around. You are the winner!

Thanks to the others who also posted, all very close! I can't believe how fast the responses were, I'll be back here often with my Wordpress issues, you can guarantee that.

Mike


Andrzej comments:

Glad it helped you, cheers!

2010-05-11

Oleg Butuzov answers:

replace this
$value = str_replace("%POST_THUMB%", get_the_content(), $value);
by this
$value = str_replace("%POST_THUMB%", get_post_meta($post->ID, "video", true), $value);

this will effect it only if you have the post_meta field video.

2010-05-11

Utkarsh Kukreti answers:

if (strpos($template, '%POST_THUMB%') !== false) {
if ($post->ID != $post_id) {
$post = &get_post($post_id);
}
$value = str_replace("%POST_THUMB%", custom_post_content(), $value);
}





<?php
function custom_post_content()
{
global $post;
$out = '';
if ( get_post_meta($post->ID, 'video', true) ) { ?>
<?php $out .= get_post_meta($post->ID, "video", $single = true); ?>
<?php } else { ?>

<?php
$attargs = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID);

$attachments = get_children($attargs);
if ($attachments) {
foreach ($attachments as $post) {
$out .= '<img style="max-width:630px; width:100%;" src="' . wp_get_attachment_url($post->ID); . ' alt="' . get_the_excerpt() . ' title="' . get_the_title() . 'class="staff-img" />';
<?php
break; //only display first image
}
}
?>

<?php }
return $out;
}?>