循环仅正确显示最后一项


Loop only displaying last item correctly

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

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

有什么想法吗?

$urls阵列

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>

首先,如果未指定用户 ID,get_the_author_meta()返回有关当前登录用户的元。这意味着您使用NULL $author_id作为参数。 get_the_title()对目前的职位和职位有类似的行为。这意味着$id也是NULL

这只能意味着url_to_postid()返回一个NULL id。

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

get_field()是造成这种情况的原因。与所有 ACF API 调用一样,它需要在 init 之后(即在操作处理程序中(调用。

另请参阅:此。

似乎这个问题远没有我想象的那么复杂。我没有修剪构建$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;