2012-01-25 117 views
1

我有一個生成從特定的標籤隨機職位代碼,存儲在一個變量標籤,PHP

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

它不以這種方式工作,可能有人解釋爲什麼會這樣呢?

回答

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具有相同的價值觀,這意味着,該數組如果使用變量或字符串,傳遞給該函數將完全相同。

+0

我發現錯誤:)謝謝:) 問題不在那裏 –