循环将数组数据传递给变量

时间:2018-08-07 14:10:29

标签: php arrays variables

如何在循环中将$the_slug的内容传递给$post_name变量?

$tags = get_the_tags();
foreach ($tags as $tag){

global $post;
$the_slug = $tag->slug; //contains 10ish words that associate with my permalinks: welcome, home, about, contact, etc

$post_id = 'welcome';
$post_name = $the_slug; //fails to populate here
$queried_post = get_post($post_name); //if changed to $post_id works but only 'welcome' post
$excerpt = $queried_post->post_excerpt;
$excerpt = apply_filters('the_content', $excerpt);
$excerpt = str_replace(']]>', ']]>', $excerpt);
echo $excerpt . "\n\n";
}

感谢您的光临。

1 个答案:

答案 0 :(得分:0)

您可以使用foreach循环遍历数组:

foreach($the_slug as $slug){
   $post_name = $slug;
   $queried_post = get_post($post_name); $excerpt = $queried_post->post_excerpt;
   $excerpt = apply_filters('the_content', $excerpt);
   $excerpt = str_replace(']]>', ']]>', $excerpt);
   echo $excerpt . "\n\n";
}