Table of Contents

It is possible to add the “Related Posts” output directly into your site’s PHP files.

The best approach is to use the PHP to include the shortcode inside a PHP snippet, as in this example below.

<?php echo do_shortcode("[taxopress_relatedposts id="1"]"); ?>

Legacy PHP Code #

There is also a legacy option allows you to make choices directly in the PHP code. This is the basic code snippet:

<?php st_related_posts(); ?>

Here are examples of how you can customize that basic code with more details:

'number' => 5,
'order' => 'count-desc',
'format' => 'list',
'include_page' => 'true',
'exclude_posts' => '',
'exclude_tags' => '',
'post_id' => '',
'except_wrap' => '55',
'limit_days' => '0',
'title' => __('<h4>Related posts</h4>', 'simpletags'),
'nopoststext' => __('No related posts.', 'simpletags'),
'dateformat' => $this->dateformat,
'xformat' => __('<a href="%post_permalink%" title="%post_title% (%post_date%)">%post_title%</a> (%post_comment%)', 'simpletags')

Here is an example of that legacy code in use. We want to show the first 10 related post without title, without pages and without comments count.

<?php st_related_posts('number=10&title=&include_page=false&xformat=<a href="%permalink%" title="%title% (%date%)">%title%</a>'); ?>

Here are all the legacy code parameters:

  • number
    • (integer) Number of posts to show
  • order
    • (string) Order posts sorting. Valid values are :
      • count-desc – (default) by number of common tags (most relevance)
      • count-asc – by number of common tags (less relevance)
      • date-desc – by publication’s date descending
      • date-asc – by publication’s date ascending
      • name-asc – by title ascending
      • name-desc – by title descending
      • random – random
  • format
    • (string) show tags as non ordered list UL/LI
      • flat – (Default) posts are separated with spaces
      • list – posts are inside non ordered lists containing class=’related_posts’
      • array -posts are inside a PHP array (use it with st_get_related_posts())
  • include_page
    • (boolean) “true” to add pages, “false” only for posts
  • exclude_posts
    • (string) Separate posts ID to exclude with commas
  • exclude_tags
    • (string) Separate tags ID to exclude with commas
  • post_id
    • (integer) Specified post ID for display related posts
  • except_wrap
    • (integer) Words number for excerpt.
  • limit_days
    • (string) Limit result with a max days
  • title
    • (string) Specify the positioned title before the list
  • nopoststext
    • (string) Specify the text to show when there is no related post
  • dateformat
    • (string) Specify the date format. Same format as PHP date function
  • xformat
    • (string) Extended links format. Available Markups :
      • post_date – Replaced by the date of the post
      • post_permalink – Replaced by the permalink of the link
      • post_title – Replaced by the title of the post
      • post_comment – Replaced by the comments count of the post
      • post_tagcount – Replaced by the number of common tags
      • post_id – Replaced by the ID of the post
      • post_relatedtags – Replaced by common tag list
      • post_excerpt – Replaced by post excerpt