【问题标题】:storing a tag in a variable , PHP将标签存储在变量中,PHP
【发布时间】:2012-01-25 15:59:36
【问题描述】:

我有从特定标签生成随机帖子的代码,

global $post;
$postid = $post->ID;
$args = array(
'orderby' => 'rand',
'showposts' => 10,
'tag' => 'ABC',             
'post__not_in' => array($postid)
);
query_posts($args);
echo '<ul>';
while (have_posts()) : the_post();
echo '<li><a href="'.get_permalink().'" title="'.the_title('','',false).'">'.the_title('','',false).'</a></li>';
endwhile;
echo '</ul>';

这里的标签是“ABC”,但是当我将 ABC 存储在变量中时,

$tagABC = 'ABC';

然后在这里调用变量

global $post;
$postid = $post->ID;
$args = array(
'orderby' => 'rand',
'showposts' => 10,
'tag' => $tagABC,             
'post__not_in' => array($postid)
);
query_posts($args);
echo '<ul>';
while (have_posts()) : the_post();
echo '<li><a href="'.get_permalink().'" title="'.the_title('','',false).'">'.the_title('','',false).'</a></li>';
endwhile;
echo '</ul>';

这样不行,有人能解释一下为什么会这样吗?

【问题讨论】:

    标签: php function random tags


    【解决方案1】:

    您确定使用相同的变量名吗?请注意,以下内容按预期工作:

    $tagABC = 'ABC';
    
    $args = array(
    'orderby' => 'rand',
    'showposts' => 10,
    'tag' => 'ABC',             
    'post__not_in' => array(3)
    );
    
    $args2 = array(
    'orderby' => 'rand',
    'showposts' => 10,
    'tag' => $tagABC,             
    'post__not_in' => array(3)
    );
    
    var_dump($args2);
    var_dump($args);
    

    得到以下输出:

    array(4) {
      ["orderby"]=>
      string(4) "rand"
      ["showposts"]=>
      int(10)
      ["tag"]=>
      string(3) "ABC"
      ["post__not_in"]=>
      array(1) {
        [0]=>
        int(3)
      }
    }
    array(4) {
      ["orderby"]=>
      string(4) "rand"
      ["showposts"]=>
      int(10)
      ["tag"]=>
      string(3) "ABC"
      ["post__not_in"]=>
      array(1) {
        [0]=>
        int(3)
      }
    }
    

    您可以看到数组$args$args2 具有相同的值,这意味着如果您使用变量或字符串,传递给函数的数组将完全相同。

    【讨论】:

      猜你喜欢
      • 2016-04-08
      • 2018-04-11
      • 2021-06-22
      • 2014-03-24
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      相关资源
      最近更新 更多