2014-04-18 57 views
0

嗨我想創建一個函數來顯示WordPress的管理菜單中的職位列表,但因爲我在同一頁面上調用了幾次功能我需要添加一些額外的聲明,但它打破了輸出,我不知道爲什麼WordPress的作爲函數獲取職位

這裏是我當前的代碼輸出的基礎知識:

function test() { 
    // The Query 
    query_posts(array ('posts_per_page' => -1)); 
    // The Loop 
    while (have_posts()) : the_post(); 
    ?> 
    <option value="<?php the_permalink() ?>"><?php the_title(); ?></option> 
    <?php endwhile; 
    // Reset Query 
    wp_reset_query(); 
    } 

輸出代碼:

<select> 
<?php test(); ?> 
</select> 

返回的輸出:

<select> 
<option value="http://website/posttitle1">POST TITLE 1</option> 
<option value="http://website/posttitle2">POST TITLE 2</option> 
<option value="http://website/posttitle3">POST TITLE 3</option> 
</select> 

但我需要添加一個選擇選項上是這樣的:

function test($select) { 
    // The Query 
    query_posts(array ('posts_per_page' => -1)); 
    // The Loop 
    while (have_posts()) : the_post(); 
    if ($select == the_permalink()) { $selected = " selected"; } 
    ?> 
    <option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?> 
    <?php endwhile; 
    // Reset Query 
    wp_reset_query(); 
    } 

輸出代碼:

<select> 
<?php test("..GET Permalink from Database.."); ?> 
</select> 

但隨後這是我的輸出:

<select> 
http://website/posttitle1<option value="http://website/posttitle1">POST TITLE 1</option> 
http://website/posttitle2<option value="http://website/posttitle2">POST TITLE 2</option> 
http://website/posttitle3<option value="http://website/posttitle2">POST TITLE 3</option> 
</select> 

我不明白?

回答

1

the_permalink()打印值並get_permalink()返回值。

試試這個。

更改以下行

if($select == the_permalink()) { $selected = " selected"; } 

if ($select == get_permalink()) { $selected = " selected"; } 

這行

<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?> 

<option value="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></option><?php echo "\n"; ?> 
+0

布里爾謝謝你這樣做的技巧 –

+0

@JosephGregory您的歡迎。 –

相關問題