0

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

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

有任何想法吗?

$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>
4

2 回答 2

1

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

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

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

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

另请参阅:.

于 2013-10-10T16:18:54.187 回答
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;
于 2013-10-11T20:07:42.143 回答