Custom Title for Related Posts Shortcode

  • Author
    Posts
  • #917903

    Displaying a related posts through shortcode is very useful. But since I’m not familiar with programming, I can’t customize it. Here’s the code:

    // Related Posts
    function related_posts_shortcode( $atts ) {
    extract(shortcode_atts(array(
    'limit' => '5',
    ), $atts));

    global $wpdb, $post, $table_prefix;

    if ($post->ID) {
    $retval = '

      ';

      // Get tags
      $tags = wp_get_post_tags($post->ID);
      $tagsarray = array();

      foreach ($tags as $tag) {
      $tagsarray[] = $tag->term_id;
      }
      $tagslist = implode(',', $tagsarray);
      // Do the query
      $q = "
      SELECT p.*, count(tr.object_id) as count
      FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
      WHERE tt.taxonomy ='post_tag'
      AND tt.term_taxonomy_id = tr.term_taxonomy_id
      AND tr.object_id = p.ID
      AND tt.term_id IN ($tagslist)
      AND p.ID != $post->ID
      AND p.post_status = 'publish'
      AND p.post_date_gmt < NOW()
      GROUP BY tr.object_id
      ORDER BY count DESC, p.post_date_gmt DESC
      LIMIT $limit;";
      $related = $wpdb->get_results($q);

      if ( $related ) {
      foreach($related as $r) {
      $retval .= '

    • ID).'">'.wptexturize($r->post_title).'
    • ';}
      }
      else {
      $retval .= '

    • No related posts found
    • ';
      }
      $retval .= '

    ';
    return $retval;
    }
    return;
    }
    add_shortcode('related_posts', 'related_posts_shortcode');
    I didn’t make that code; I just copy and paste it from a tutorial.

    Now, let’s say I added a “[related_posts] on my content.
    Then, the 5 related posts displayed like this: (for example)
    1.
    2.
    3.
    4.
    5.

    Now what I want to do is this:
    (Add title like related posts)
    1.
    2.
    3.
    4.
    5.
    (add more texts here)

    Thanks.

    The blog I need help with is designrshub.com.

    #917998

    supportbot
    Member

    The blog you specified at designrshub.com does not appear to be hosted at WordPress.com.

    This support forum is for blogs hosted at WordPress.com. If your question is about a self-hosted WordPress blog then you’ll find help at the WordPress.org forums.

    If you don’t understand the difference between WordPress.com and WordPress.org, you may find this information helpful.

    If you forgot to include a link to your blog, you can reply and include it below. It’ll help people to answer your question.

    This is an automated message.

The topic ‘Custom Title for Related Posts Shortcode’ is closed to new replies.