PHP return 循环内来自 Wordpress 的值

PHP return values from Wordpress within loop

我在 return 从函数中获取值时遇到一些问题。该函数本身包含一个循环,该循环从 wordpress 博客中获取并分配一堆变量。我想要做的是 return 循环中定义的变量,这样我就可以简单地在其他地方回显它们。我需要多次 运行 相同的循环,但参数不同,因此它们需要在函数内部以防止出现任何问题。

我将 wordpress 用作 CMS - 文件最终将被包含在内,变量 returned 将被回显以填充网站内容。

这是我的代码:

function recentFeatured3(){

    $args = array( 'category' => 4, 'posts_per_page' => 2, 'post_status' =>  'publish', 'order' => 'DESC' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){

        $count++;

        ${'featured_title' . $count}=$recent["post_title"];
        ${'featured_post_id' . $count}=$recent["ID"];

        ${'featured_post_' . $count} = get_post(${'featured_post_id' . $count}); 
        ${'featured_content' . $count} = ${'featured_post_' . $count}->post_content;
        ${'featured_date' . $count} = mysql2date('F j, Y', ${'featured_post_' . $count}->post_date);
        ${'featured_excerpt' . $count} = substr(${'featured_content' . $count},0,160).'...';

    }

    return $featured_title1;
    return $featured_title2;
    return $featured_post_id1;
    return $featured_post_id2;
    return $featured_content1;
    return $featured_content2;
    return $featured_date1;
    return $featured_date2;
    return $featured_excerpt1;
    return $featured_excerpt2;

}

echo $featured_title1; //and so on....

如您所见,我只是想回显在函数外部的循环中定义和创建的变量。我不确定 return 是否需要进入循环 - 我在内部和外部都尝试过,但都不起作用。该变量也是根据循环计数命名的,所以我不确定如何解决这个问题,或者这是否会导致问题。

如有任何想法或建议,我们将不胜感激!

如果你想 return 一大堆信息,你不能用多个 return 语句来做到这一点。只有第一个被执行,其余的都是死代码。您需要做的是创建某种类型的集合(通常在 PHP 一个对象或数组中),将所有数据放在那个单一的东西中,然后 return (对)那个单一的东西. 此外,你想要一个集合的集合!对于每个post,一组值,并且会有多个post。这是一个看起来如何的示例,returning 一个数组数组:

function recentFeatured3(){
$resultsArray = array(); // you can just write '[]' in PHP >= 5.4

$args = array( 'category' => 4, 'posts_per_page' => 2, 'post_status' =>  'publish', 'order' => 'DESC' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    $postInfo = array();
    $postInfo['featured_title'] = $recent["post_title"];
    $postInfo['featured_post_id'] = $recent["ID"];
    $postInfo['featured_post'] = get_post(${'featured_post_id' . $count}); 
    $postInfo['featured_content'] = ${'featured_post_' . $count}->post_content;
    $postInfo['featured_date' = mysql2date('F j, Y', ${'featured_post_' . $count}->post_date);
    $postInfo['featured_excerpt'] = substr(${'featured_content' . $count},0,160).'...';
    array_push($resultsArray, postInfo);
}

return $resultsArray;
}

$results = recentFeatured3();
foreach($results as $index=>$postInfo) {
    echo "results for post #".$index;
    foreach($postInfo as $key=>$value) {
         echo $key . " has value " . $value;
    }
}

你不能有多个这样的 returns。只有第一个被执行。将您的值收集到一个数组中。

function recentFeatured3(){

  $args = array( 'category' => 4, 'posts_per_page' => 2, 'post_status' =>  'publish', 'order' => 'DESC' );
  $recent_posts = wp_get_recent_posts( $args );
  $data = array();
  foreach( $recent_posts as $recent ){

    $count++;

    $data['featured_title' . $count}=$recent["post_title"];
    $data['featured_post_id' . $count}=$recent["ID"];

    $data['featured_post_' . $count] = get_post(${'featured_post_id' . $count}); 
    $data['featured_content' . $count} = ${'featured_post_' . $count}->post_content;
    $data['featured_date' . $count} = mysql2date('F j, Y', ${'featured_post_' . $count}->post_date);
    $data['featured_excerpt' . $count} = substr(${'featured_content' . $count},0,160).'...';

  }

  return $data

}

echo $data['featured_title1']; //and so on....

您不能 return 超过 1 次,因此您应该替换为

$arr = array();
foreach( $recent_posts as $recent ){
    $post = array();
    $post['featured_title']=$recent["post_title"];
    $post['featured_post_id']=$recent["ID"];
    $featuredPost = get_post($recent["ID"]); 
    $post['featured_post'] = $featuredPost; 
    $post['featured_content'] = $featuredPost->post_content;
    $post['featured_date'] = $featuredPost->post_date);
    ${'featured_excerpt' . $count} = substr($post['featured_content'],0,160).'...';
    $arr[] = $post;
}

    return $arr;
}

print_r(recentFeatured3());