【问题标题】:PHP echo variable within function arguments函数参数中的 PHP 回显变量
【发布时间】:2011-09-21 17:33:34
【问题描述】:

我目前正在使用 wordpress 功能来显示特定类别的帖子。 简化的示例如下所示:

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

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

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

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

提前感谢您的帮助。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    只有当你想输出到浏览器时才回显它,这里我们将查询字符串与变量连接起来:

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

    【讨论】:

    • 当然更好,因为它连接变量...提高可读性;-)
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 2012-04-18
      • 1970-01-01
      • 2013-09-07
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多