我在从函数返回值时遇到了一些麻烦。该函数本身包含一个循环,它从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....
如您所见,我只想回显从函数外部的循环定义和创建的变量。我不确定返回是否需要进入循环 - 我已经在内部和外部尝试过,并且都不起作用。该变量也是基于循环计数命名的,因此我不确定如何解决该问题或者是否导致问题。
任何想法或建议将不胜感激!
答案 0 :(得分:1)
你不能有这样的多个回报。只执行第一个。收集数组中的值。
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....
答案 1 :(得分:0)
如果要返回大量信息,则不能使用多个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;
}
}
答案 2 :(得分:0)
你不能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());