如何在WP_Query数组中使用PHP变量?

时间:2019-03-27 16:26:12

标签: php wordpress variables

我正在尝试将变量拉入WP_Query数组,但是预期的输出不是我期望的。

这是我的代码:

<?php
$country_name = the_title();
$populate_jhcarousel = new WP_Query( array(
    'post_type' => 'story',
        'tax_query' => array(
            array(
                'taxonomy' => 'location',
                'field'    => 'name',
                'terms'    => "'"$country_name"'"
            ),
        ),
    'posts_per_page' => 20,
    'order'=> 'DESC'
        )  );
?>

我的目的是带入所有位置均为其所在国家页面标题的所有帖子。在本地设置中,我正在与坦桑尼亚进行测试。当我使用以下'terms' => 'Tanzania'时,滑块会正确填充。但是,当我使用变量时,它会抛出500错误,或者只是输出“ Tanzania”一词代替我的滑块。以下是我尝试过的结果:

'terms' => "'"$country_name"'" = HTTP错误500

'terms' => '$country_name' =“坦桑尼亚”

'terms' => $country_name =“坦桑尼亚”

'terms' => 'Tanzania' =预期结果

欢迎在WP_Query数组中使用变量的任何帮助。提前非常感谢!!!

1 个答案:

答案 0 :(得分:1)

问题是您正在使用the_title(),它具有回显标题的默认行为。将其更改为get_the_title()

相关问题