book this wordpress script for me personally, it's not working. I wish to obtain the first tag from the publish and query other posts who have a similar tag. TY!
<? // Start related posts by tag
$posttags = get_the_tags();
$count=0;
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if (1 == $count) {
$tag = $tag->slug . ' ';
}
}
}
query_posts('tag='.$tag.'&posts_per_page=-1');
while (have_posts()) : the_post(); ?>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<? endwhile;
wp_reset_query(); ?>
Unsure concerning the relaxation from the logic of the code only one problem is always that you're utilizing the same $tag
variable to loop with the $posttags
in addition to store the first tag value. Rather than doing something so complex, why don't you only use the very first value inside your $posttags
array.
Something similar to this -
<? // Start related posts by tag
$posttags = get_the_tags();
if ($posttags) {
query_posts('tag='.$posttags[0].'&posts_per_page=-1');
//rest of your code here