PHP返回函数参数中的变量


PHP echo variable within function arguments

我目前正在使用一个wordpress功能,以显示从一个特定类别的帖子。简化的示例如下:

<?php query('cat_name=cat1&posts=1') ?>

这基本上从类别cat1中获取1个帖子。但是,我保存了一个变量,该变量获取当前类别(这是在类别页面上):

<?php $thiscat = get_the_category(); ?>
Current Category: <?php echo $thiscat ?>

我现在如何将变量$thiscat回显到上面查询的参数中,以便为我填写类别名称?此函数应用于不同的类别页面,因此将其自动传递给查询的参数可以节省很多麻烦。

提前感谢您的帮助。

只在想要输出到浏览器时才回显,这里我们将查询字符串与变量连接在一起:

<?php $thiscat = get_the_category(); ?>
<?php query('cat_name=' . $thiscat . '&posts=1') ?>

我不确定我理解这个问题,但听起来你想在你的查询中使用$thiscat。应该这样做:

<?php
$thiscat = get_the_category();
query("cat_name=$thiscat&posts=1")
?>

注意双引号,这是必要的。如果你使用单引号,变量将不会被展开。