Reputation: 1091
I am trying to implement a page that displays blog posts by tag using a WordPress loop. The problem that I've been running into is setting the tag to display as the page title. Here is the code that I've tried so far.
<?php query_posts('tag=aetna'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="page-content ">
<?php the_content() ;?>
</div>
<?php endwhile; endif ;?>
This code works just fine. However, when I try to assign the tag to the page title, it doesn't work. Here is the code I've tried for that. I'm new to PHP so I'm hoping this is just a silly syntax thing.
<?php $tag=strtolower(the_title()); ?>
<?php query_posts('tag=$tag'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="page-content ">
<?php the_content() ;?>
</div>
<?php endwhile; endif ;?>
Any help you can provide me is much appreciated. Thank you!
Upvotes: 2
Views: 5851
Reputation: 637
You are calling the_title() before the loop. It is a function that can only be called inside of the loop.
If you want to use that function you would have to create two queries, one that assigns $tag
<?php $tag=strtolower(the_title()); ?>
and the rest in another loop
<?php query_posts('tag=$tag'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="page-content ">
<?php the_content() ;?>
</div>
<?php endwhile; endif ;?>
Upvotes: 0
Reputation: 5183
$tag=strtolower(the_title());
should be
$tag=strtolower(get_the_title());
the_title(); echos the output while get_the_title();
returns it refer Links for more
Upvotes: 1
Reputation: 359
When using single quotes with PHP the variable wont be inserted into the string.
Try using the double quotes:
<?php query_posts("tag=$tag"); ?>
More info here: What is the difference between single-quoted and double-quoted strings in PHP?
Upvotes: 2