Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环仅正确显示最后一项_Php_Wordpress - Fatal编程技术网

Php 循环仅正确显示最后一项

Php 循环仅正确显示最后一项,php,wordpress,Php,Wordpress,我有一个链接数组,用于显示文章列表。创建帖子时,每个链接都会输入到文本区域 数组的最后一项显示正确,而前三项则为文章标题和以作者身份登录的当前用户提取当前页面标题。标题和作者都不正确 有什么想法吗 $URL数组 Array ( [0] => http://localhost:8888/2013/10/custom-toothbrush-six-second-cleaning.html [1] => http://localhost:8888/2013/10/cli

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

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

有什么想法吗

$URL数组

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>';
 }
$url=explode(PHP_EOL,get_字段('publishing_articles');
foreach($url作为$url){
$id=url\u to\u posted($url);
$theu title=get\u theu title($id);
$author\u id=get\u post\u字段('post\u author',$id);
$author\u displayname=获取作者元($display\u name',$author\u id);
$author\u nicename=获取作者元($author\u nicename',$author\u id);
回显“
  • ”.$id; 回声'; 回声'; 回音“
  • ”; }
    以及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
  • 0
  • 0
  • 210664

  • 首先,
    获取作者的meta()
    返回当前登录用户的meta(如果未指定用户ID)。这意味着您正在使用
    NULL
    $author\u id
    作为参数<代码>获取标题()与当前帖子响应有类似的行为。这意味着
    $id
    也是
    NULL

    这只能意味着
    url\u to\u postid()
    返回
    NULL
    id

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

    get\u field()
    就是原因。与所有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;
    
    $url=explode(PHP_EOL,get_字段('publishing_articles');
    foreach($url作为$url){
    $id=url_to_posted(trim($url));
    $theu title=get\u theu title($id);
    $author\u id=get\u post\u字段('post\u author',$id);
    $author\u url=获取作者元('user\u nicename',$author\u id);
    $author\u name=获取\u author\u元($display\u name',$author\u id);
    $output.='
  • '; $output.=''; $output.=''; $output.='
  • '; } echo$输出;
    您可以发布输出的HTML的外观吗?在每种情况下显示的是正确的
    $id
    ?URL中是什么?您可能是通过引用而不是值来分配数组。。。更多代码。顶部的数组是$URL。我更改了标题以澄清。
    get\u field()
    是高级自定义字段API函数吗?听起来有点熟悉。感谢所有的帮助,但我仍然完全被在init之后调用某个东西弄糊涂了…我尝试了add_action('wp_head','get_field');但是它没有改变任何事情。
    wp\u head
    事件在
    init
    之后运行,是的。但我不是说你应该单独调用
    get\u fied()
    ,而是指你的整个过程(包括调用
    get\u field()
    ),即你发布的代码。