I am using simple-tags for wordpress and want to show some related posts in my sidebar, when someone is viewing a single page, any ideas?
Wordpress and Webdesign Forum go41 » WordPress Code Snippets
-
Posted 2 years ago #
-
Fritz?.. you could insert a code like the one below. This one checks first if simpletags is running, if yes it will run on a single page only.
The simple tags function st_related_posts will give you back some posts tagged with one or more tags of the current post.
I set it to show 5 posts, no pages (if tagged) and the link is formatted with 'xformat' to show permalink, title and name of the related post.
Try it!
<?php if (class_exists('SimpleTags')) : ?> <?php if ( is_single() ) { ?> <h3>Related posts</h3> <?php st_related_posts('number=5&title=&include_page=false&xformat=<a href="%permalink%" rel="bookmark" title="%title%">%title%</a>'); ?> <?php } ?> <?php endif; ?>Posted 2 years ago # -
finally I realized many errors with this code. I put just for debugging this in the header.php of my theme:
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>beside many other php errors the code with st_related_posts shows errors like:
Notice: Undefined property: stdClass::$terms_id in /wp-content/plugins/simple-tags/inc/client.php on line 715
Notice: Undefined property: stdClass::$post_excerpt in /wp-content/plugins/simple-tags/inc/client.php on line 716
Notice: Undefined property: stdClass::$post_content in /wp-content/plugins/simple-tags/inc/client.php on line 716
Notice: Undefined property: stdClass::$post_password in /wp-content/plugins/simple-tags/inc/client.php on line 716
etc ...Now I generate related posts with this code, which displays almost the same rsults:
<?php // list 5 post titles related to first tag on current post in list style as st_related_posts $tags = wp_get_post_tags($post->ID); if ($tags) { echo '<ul class="st-related-posts">'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; } echo '</ul>'; } ?>code above just replaces the single line
<?php st_related_posts('number=5&title=&include_page=false&xformat=<a href="%permalink%" rel="bookmark" title="%title%">%title%</a>'); ?>Posted 1 year ago #
Reply
You must log in to post.