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

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

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

尝试使用双引号:

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

更多信息在这里:PHP 中的单引号和双引号字符串有什么区别?

$tag=strtolower(the_title());

应该是

$tag=strtolower(get_the_title());

the_title(); 回显输出,而get_the_title();返回输出,请参阅链接了解更多信息

你在循环之前调用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 ;?>

最新更新