logo

$50
Customise related posts output (works already, needs adapting)

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 Microkid's related posts 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): http://snipt.org/xPok

Jon | 05/31/11 at 10:43am | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    05/31/11
    10:53am
    Erez S says:

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

    • 05/31/11 10:54am

      Erez S says:

      BTW you didn't wrote your current output

    • 05/31/11 10:59am

      Jon says:

      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 here. It will probably involve a quick edit of the plugin output files.

    • 05/31/11 10:59am

      Erez S says:

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

    • 05/31/11 11:02am

      Jon says:

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

    • 05/31/11 11:07am

      Erez S says:

      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

    • 05/31/11 11:12am

      Erez S says:

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

    • 05/31/11 11:15am

      Jon says:

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

    • 05/31/11 11:16am

      Erez S says:

      Try my last code without the function

    • 05/31/11 11:16am

      Erez S says:

      If not working send me the whole file

    • 05/31/11 11:18am

      Jon says:

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

    • 05/31/11 11:19am

      Erez S says:

      erezaton213@gmail.com

  • avatar
    Last edited:
    05/31/11
    11:22am
    Christianto says:

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

    • 05/31/11 11:28am

      Christianto says:

      Any result?

    • 05/31/11 11:36am

      Christianto says:

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

    • 05/31/11 12:23pm

      Christianto says:

      Jon,

      Have you try it?
      Is it work or not?

      Thanks

    • 05/31/11 12:26pm

      Jon says:

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

This question has expired.



Jon voted on this question.



Current status of this question: Completed