WordPress循环按标签

时间:2013-10-16 17:33:14

标签: php wordpress

我正在尝试使用WordPress循环实现一个按标签显示博客帖子的页面。我遇到的问题是将标签设置为显示为页面标题。这是我到目前为止尝试过的代码。

<?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 ;?>

这段代码很好用。但是,当我尝试将标记分配给页面标题时,它不起作用。这是我为此尝试过的代码。我是PHP的新手,所以我希望这只是一个愚蠢的语法。

<?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 ;?>

非常感谢您提供给我的任何帮助。谢谢!

3 个答案:

答案 0 :(得分:1)

在PHP中使用单引号时,变量不会插入到字符串中。

尝试使用双引号:

<?php query_posts("tag=$tag"); ?>

更多信息: What is the difference between single-quoted and double-quoted strings in PHP?

答案 1 :(得分:1)

$tag=strtolower(the_title());

应该是

$tag=strtolower(get_the_title());

the_title();回显输出,get_the_title();返回时引用链接了解更多

答案 2 :(得分:0)

你在循环之前调用the_title()。 它是一个只能在循环内部调用的函数。

如果您想使用该功能,则必须创建两个查询,一个分配$ tag

<?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 ;?>