循环只显示最后一项

时间:2013-10-10 14:42:22

标签: php wordpress

我有一个链接数组,我用它来显示文章列表。创建帖子时,每个链接都会输入到textarea中。

数组的最后一项正确显示,而前三项则拉出文章标题的当前页面标题和作为作者登录的当前用户。标题和作者都不正确。

有什么想法吗?

$ urls Array

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>

2 个答案:

答案 0 :(得分:1)

首先,如果未指定用户标识,get_the_author_meta()将返回有关当前登录用户的元数据。这意味着您使用NULL $author_id作为参数。 get_the_title()有关于当前帖子的类似行为。这意味着$id也是NULL

这只能表示url_to_postid()返回NULL个ID。

以上反过来意味着$urls未按预期初始化。

get_field()就是原因。与所有ACF API调用一样,它需要在 init之后调用(即在动作处理程序中)。

另见:this

答案 1 :(得分: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;