2013-10-10 79 views
0

我有一個鏈接數組,用於顯示文章列表。創建帖子時,每個鏈接都會輸入到textarea中。Loop僅正確顯示最後一項

數組的最後一項正確顯示,而前三項拉動文章標題的當前頁面標題和以作者身份登錄的當前用戶。標題和作者都不正確。

任何想法?

$網址陣列

Array ( 
    [0] => http://localhost:8888/2013/10/custom-toothbrush-six-second-cleaning.html 
    [1] => http://localhost:8888/2013/10/climate-change-global-warming-al-gore.html 
    [2] => http://localhost:8888/2013/10/custom-toothbrush-six-second-cleaning.html 
    [3] => http://localhost:8888/2013/10/climate-change-global-warming-al-gore.html 
    ) 

這裏是PHP代碼

$urls=explode(PHP_EOL, get_field('publishing_articles')); 
foreach($urls as $url){  
    $id=url_to_postid($url); 
    $the_title=get_the_title($id); 
    $author_id=get_post_field('post_author',$id); 
    $author_displayname=get_the_author_meta('display_name',$author_id); 
    $author_nicename=get_the_author_meta('user_nicename',$author_id); 

    echo '<li>'.$id; 
    echo '<a href="/author/'.$author_nicename.'" title="'.$the_title.'"><img width="50" src="/wp-content/uploads/authors/'.$author_id.'.jpg" alt="'.$author_displayname.'"/></a>'; 
    echo '<a href="'.$url.'" title="'.$the_title.'">'.$the_title.'</a>'; 
    echo '</li>'; 
} 

和HTML輸出

<li>0<a href="/author/admin" title="The Future Of Light"><img width="50" src="/wp-content/uploads/authors/362.jpg" alt="admin"/></a><a href="http://localhost:8888/2013/10/custom-toothbrush-six-second-cleaning.html" title="The Future Of Light">The Future Of Light</a></li> 
<li>0<a href="/author/admin" title="The Future Of Light"><img width="50" src="/wp-content/uploads/authors/362.jpg" alt="admin"/></a><a href="http://localhost:8888/2013/10/climate-change-global-warming-al-gore.html" title="The Future Of Light">The Future Of Light</a></li> 
<li>0<a href="/author/admin" title="The Future Of Light"><img width="50" src="/wp-content/uploads/authors/362.jpg" alt="admin"/></a><a href="http://localhost:8888/2013/10/custom-toothbrush-six-second-cleaning.html" title="The Future Of Light">The Future Of Light</a></li> 
<li>210664<a href="/author/daniela-walker" title="How Al Gore Is Making Global Warming Personal"><img width="50" src="/wp-content/uploads/authors/384.jpg" alt="Daniela Walker"/></a><a href="http://localhost:8888/2013/10/climate-change-global-warming-al-gore.html" title="How Al Gore Is Making Global Warming Personal">How Al Gore Is Making Global Warming Personal</a></li> 
+0

你可以發佈輸出的HTML的樣子 –

+0

是否在每種情況下都顯示正確的'$ id'? – Hobo

+0

添加HTML輸出 – ok1ha

回答

0

看來這個問題比我想象的要少得多。我沒有修剪構建$ id數組的行。另外,我已經根據geomagas的建議修復了作者函數。

$urls=explode(PHP_EOL, get_field('publishing_articles')); 

foreach($urls as $url){  

    $id=url_to_postid(trim($url)); 

    $the_title=get_the_title($id); 
    $author_id=get_post_field('post_author',$id); 
    $author_url=get_the_author_meta('user_nicename',$author_id); 
    $author_name=get_the_author_meta('display_name',$author_id); 

    $output.='<li>'; 
    $output.='<a class="author" href="/author/'.$author_url.'" title="More Articles By '.$author_name.'"><img src="/wp-content/uploads/authors/'.$author_id.'.jpg" alt="'.$author_name.'" onerror="author_img_error(this);" /></a>'; 
    $output.='<a class="link" href="'.$url.'" title="'.$the_title.'">'.$the_title.'</a>'; 
    $output.='</li>'; 
} 
echo $output; 
1

首先,get_the_author_meta()返回元左右的電流,已登錄如果未指定用戶標識,則以用戶身份登錄。這意味着您正在使用NULL$author_id作爲參數。 get_the_title()有關於當前帖子resp的類似行爲。這意味着$id也是NULL

這隻能表示url_to_postid()返回一個NULL id。

上述情況意味着$urls未按預期進行初始化。

get_field()是原因。與所有的ACF API調用一樣,它需要在 init(即在一個動作處理程序)之後調用。還有:this

+0

感謝所有的幫助,但我仍然完全困惑,通過調用初始化後的東西...我試着add_action('wp_head','get_field');但它沒有改變任何東西。 – ok1ha

+1

''wp_head'事件在'init'後面運行,是的。 [這是一個典型請求期間動作序列的參考](http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request)。但我並不是說你應該單獨調用'get_fied()',而是你的整個過程(包括調用'get_field()')_即你發佈的代碼。 – geomagas