Horje
Customise related posts output (works already, needs adapting) - Wordpress Solution
[Solved] - Customise related posts output (works already, needs adapting) - Wordpress Solution
Try following codes
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]]

Solution - 1

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; ?>


Solution - 2

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..

Date: 2022-10-21 05:13:50
Category: Wordpress
More for You