循环将数组数据传递给变量 [英] Passing array data to a variable in a loop

查看:133
本文介绍了循环将数组数据传递给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



How do I pass the contents of the $the_slug to the $post_name variable in the loop?

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

感谢您的关注。

推荐答案

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

You can iterate over the array with a foreach loop:

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

这篇关于循环将数组数据传递给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆