2

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!

salxander
  • 1,081
  • 5
  • 20
  • 29

3 Answers3

2

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?

Community
  • 1
  • 1
Simalam
  • 359
  • 1
  • 8
  • Thank you! It is now actually displaying posts. Not the correct ones but I think this has something to do with the page titles and not the loop. Thanks again! – salxander Oct 16 '13 at 17:50
1
$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

Prince Singh
  • 5,023
  • 5
  • 28
  • 32
0

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 ;?>
sbastidasr
  • 637
  • 8
  • 9