2013-10-10 68 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