WordPress get related pages (tag based) – wp_get_post_tags

Works when I put in single.php but not in page.php, how is that?

Can anyone enlighten me please?

Read More

I’m working on WordPress theme and want to echo all the tags of a page.

The code:

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts</h3><ul>';
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
}
echo '</ul>';
}
}
?>

Related posts

Leave a Reply

1 comment

  1. On page.php wp_get_post_tags($post->ID) tries to get tags for page. WordPress is not supporting page tags. To have such option you have to add tags for pages, do it with plugin or add code following one of many tutorials