PHP在循环中从Wordpress返回值


PHP return values from Wordpress within loop

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

我使用wordpress作为CMS——文件最终会被包括在内,返回的变量会被回显以填充网站的内容。

这是我的代码:

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....

正如您所看到的,我只想回显从函数外部的循环中定义和创建的变量。我不确定返回是否需要进入循环内部——我已经尝试了内部和外部,但都不起作用。变量的命名也基于循环的计数,所以我不确定如何解决这个问题,或者这是否是问题的根源。

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

不能有多个这样的返回。只执行第一个。在数组中收集您的值。

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语句。只有第一个被执行,其余的都是死代码。您需要做的是创建某种类型的集合(通常在PHP中是Object或Array),将所有数据放在那个单一的东西中,然后返回(对该单一东西的引用)。此外,您想要一个集合!对于每个帖子,都有一组值,并且会有多个帖子。下面是一个返回数组的例子:

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;
    }
}

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());