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

Customise related posts output (works already, needs adapting) WordPress

  • SOLVED

I need to manually relate posts and display these at the bottom of a post. I have the output working perfectly for a different plugin (YARPP), which offers a lot more flexibility in the output, but need to switch this over to a manual plugin.

Not too fussed which one is used, but [[LINK href="http://wordpress.org/extend/plugins/microkids-related-posts/"]]Microkid's related posts[[/LINK]] looks like the best option.

Here's the code we're using to output at the moment - I need this substituted to work with Microkid's output.

///Custom functions used:

function wcs_build_tag_list_icononly()
{
$output = '';
$tags = get_the_tags();
foreach ($tags as $tag)
{
$tag_link = get_tag_link($tag->term_id);
$tag_name = $tag->name;
$tag_slug = $tag->slug;

$output .= '<span class="flag">';
$output .= '<img src="/_assets/img/icons/flags/16/' . $tag_slug . '.png" /></span>';
}
return $output;
}



///Actual code

<?php if ($related_query->have_posts()):?>
<aside class="related-posts" class="group">
<h3>Related Ideas</h3>
<ol>
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li>
<a class="image-crop" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumb-size' ); ?></a>
</a>
<a href="<?php the_permalink() ?>" class="title-area" rel="bookmark">
<?php //Get country tag
$tag_list = wcs_build_tag_list_icononly();
$utility_text = __( '%2$s', 'toolbox' );
printf(
$utility_text,
get_the_category_list( ', ' ),
$tag_list,
get_permalink(),
the_title_attribute( 'echo=0' )
);
?>
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ol>
</aside>
<?php endif; ?>



And here's what the output looks like (need to do exactly the same for the new plugin)

///Output as follows:

<aside class="related-posts" class="group">
<h3>Related Ideas</h3>
<ol>
<li>
<a class="image-crop" href="/link-here/">
<img width="310" height="185" src="/img/uploads/2011/04/image.jpg" class="attachment-thumb-size wp-post-image" alt="" title=""/></a>
</a>
<a href="/link-here/" class="title-area" rel="bookmark">
<span class="flag"><img src="xxx" /></span>
Title goes here
</a>
</li>
</ol>
</aside>


And here's the whole lot posted elsewhere (in case that's easier to refer to): [[LINK href="http://snipt.org/xPok"]]http://snipt.org/xPok[[/LINK]]

Answers (2)

2011-05-31

Erez S answers:

You mean something like that (I don't think I understood you...):
<?php if ($related_query->have_posts()):?>
<aside class="related-posts" class="group">
<h3>Related Ideas</h3>
<ol>
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li>
<a class="image-crop" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumb-size' ); ?></a>
</a>
<a href="<?php the_permalink() ?>" class="title-area" rel="bookmark">
<?php //Get country tag
$tag_list = MRP_get_related_posts_html( $post_id );
$utility_text = __( '%2$s', 'toolbox' );
printf(
$utility_text,
get_the_category_list( ', ' ),
$tag_list,
get_permalink(),
the_title_attribute( 'echo=0' )
);
?>
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ol>
</aside>
<?php endif; ?>


Erez S comments:

BTW you didn't wrote your current output


Jon comments:

No, sorry - this code works with the current plugin (that function is inside functions.php).

I need the same output (shown at the end of the question), but from a different plugin, which doesn't let me just use custom HTML. See [[LINK href="http://www.microkid.net/wordpress/related-posts/"]]here[[/LINK]]. It will probably involve a quick edit of the plugin output files.


Erez S comments:

Now I think I understood you and this should work:

function wcs_build_tag_list_icononly($id)
{
$output = '';
$relateposts = MRP_get_related_posts( $id );
foreach($relateposts as $k=>$v){
$output .= '<li>';
$output .= '<a class="image-crop" href="'.get_permalink($k).'">
<img width="310" height="185" src="/img/uploads/2011/04/image.jpg" class="attachment-thumb-size wp-post-image" alt="" title=""/></a>
</a>
<a href="'.get_permalink($k).'" class="title-area" rel="bookmark">
<span class="flag"><img src="xxx" /></span>
'.$v.'
</a>';
$output .= '</li>';
}
return $output;
}


<?php if ($related_query->have_posts()):?>
<aside class="related-posts" class="group">
<h3>Related Ideas</h3>
<ol>
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li>
<a class="image-crop" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumb-size' ); ?></a>
</a>
<a href="<?php the_permalink() ?>" class="title-area" rel="bookmark">
<?php //Get country tag
$tag_list = wcs_build_tag_list_icononly(get_the_ID());
$utility_text = __( '%2$s', 'toolbox' );
printf(
$utility_text,
get_the_category_list( ', ' ),
$tag_list,
get_permalink(),
the_title_attribute( 'echo=0' )
);
?>
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ol>
</aside>
<?php endif; ?>


Jon comments:

I really don't think this will work - you're not altering the output of the Microkid plugin at all?


Erez S comments:

I am, in the third row:

$relateposts = MRP_get_related_posts( $id );
try to put the function inside the functions.php file and the rest in your theme


Erez S comments:

My bad, I didn't understood you again:

<?php if ($related_query->have_posts()):?>
<aside class="related-posts" class="group">
<h3>Related Ideas</h3>
<ol>
$relateposts = MRP_get_related_posts( $post->ID );
foreach($relateposts as $k=>$v){
?><li>
<a class="image-crop" href="<?php echo get_permalink($k); ?>">
<img width="310" height="185" src="/img/uploads/2011/04/image.jpg" class="attachment-thumb-size wp-post-image" alt="" title=""/></a>
</a>
<a href="<?php echo get_permalink($k); ?>" class="title-area" rel="bookmark">
<span class="flag"><img src="xxx" /></span>
<?php echo $v; ?>
</a>
<li><?php
}
</ol>
</aside>
<?php endif; ?>


Jon comments:

I'm trying to get that to work, but the function is breaking the theme file? (Stripped out the template code, so it's definitely the function).


Erez S comments:

Try my last code without the function


Erez S comments:

If not working send me the whole file


Jon comments:

Do you have a direct email/skype? It would be a lot quicker!


2011-05-31

Christianto answers:

Hi Jon,

There are 2 function that need to be change "wcs_build_tag_list_icononly" and new the_title_attribute function that I name "custom_the_title_attribute", this is because we dont use any loop so have to pass the post ID manually.

Please try my code

<?php

function wcs_build_tag_list_icononly($ID)
{
$output = '';
$tags = get_the_tags($ID);
foreach ($tags as $tag)
{
$tag_link = get_tag_link($tag->term_id);
$tag_name = $tag->name;
$tag_slug = $tag->slug;

$output .= '<span class="flag">';
$output .= '<img src="/_assets/img/icons/flags/16/' . $tag_slug . '.png" /></span>';
}
return $output;
}

function custom_the_title_attribute( $args = '', $ID ) {
$title = get_the_title($ID);

if ( strlen($title) == 0 )
return;

$defaults = array('before' => '', 'after' => '', 'echo' => true);
$r = wp_parse_args($args, $defaults);
extract( $r, EXTR_SKIP );


$title = $before . $title . $after;
$title = esc_attr(strip_tags($title));

if ( $echo )
echo $title;
else
return $title;
}

$related_post = MRP_get_related_posts( $post->ID, true );

if (is_array($related_post)){ ?>

<aside class="related-posts" class="group">

<h3>Related Ideas</h3>

<ol>

<?php

foreach ($related_post as $rpost){

?>

<li>

<a class="image-crop" href="<?php the_permalink($rpost->ID); ?>">

<?php echo get_the_post_thumbnail( $rpost->ID, 'thumb-size' ); ?></a>

</a>

<a href="<?php the_permalink($rpost->ID) ?>" class="title-area" rel="bookmark">

<?php //Get country tag

$tag_list = wcs_build_tag_list_icononly($rpost->ID);

$utility_text = __( '%2$s', 'toolbox' );

printf(

$utility_text,

get_the_category_list( ', ','', $rpost->ID ),

$tag_list,

get_permalink($rpost->ID),

custom_the_title_attribute( 'echo=0', $rpost->ID )

);

?>

<?php echo $rpost->post_title; ?>

</a>

</li>

<?php

} // end foreach

?>

</ol>

</aside>

<?php } ?>


Blind code so could be any error..


Christianto comments:

Any result?


Christianto comments:

sorry the_permalink should be get_permalink, try code below

<?php

function wcs_build_tag_list_icononly($ID)
{
$output = '';
$tags = get_the_tags($ID);
foreach ($tags as $tag)
{
$tag_link = get_tag_link($tag->term_id);
$tag_name = $tag->name;
$tag_slug = $tag->slug;

$output .= '<span class="flag">';
$output .= '<img src="/_assets/img/icons/flags/16/' . $tag_slug . '.png" /></span>';
}
return $output;
}

function custom_the_title_attribute( $args = '', $ID ) {
$title = get_the_title($ID);

if ( strlen($title) == 0 )
return;

$defaults = array('before' => '', 'after' => '', 'echo' => true);
$r = wp_parse_args($args, $defaults);
extract( $r, EXTR_SKIP );


$title = $before . $title . $after;
$title = esc_attr(strip_tags($title));

if ( $echo )
echo $title;
else
return $title;
}

$related_post = MRP_get_related_posts( $post->ID, true );

if (is_array($related_post)){ ?>

<aside class="related-posts" class="group">

<h3>Related Ideas</h3>

<ol>

<?php

foreach ($related_post as $rpost){

?>

<li>

<a class="image-crop" href="<?php get_permalink($rpost->ID); ?>">

<?php echo get_the_post_thumbnail( $rpost->ID, 'thumb-size' ); ?></a>

</a>

<a href="<?php get_permalink($rpost->ID) ?>" class="title-area" rel="bookmark">

<?php //Get country tag

$tag_list = wcs_build_tag_list_icononly($rpost->ID);

$utility_text = __( '%2$s', 'toolbox' );

printf(

$utility_text,

get_the_category_list( ', ','', $rpost->ID ),

$tag_list,

get_permalink($rpost->ID),

custom_the_title_attribute( 'echo=0', $rpost->ID )

);

?>

<?php echo $rpost->post_title; ?>

</a>

</li>

<?php

} // end foreach

?>

</ol>

</aside>

<?php } ?>


Christianto comments:

Jon,

Have you try it?
Is it work or not?

Thanks


Jon comments:

Sorry Christianto - Erez has managed to get this pretty much working now. Thanks for your help, anyway.