Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组数据传递给循环中的变量_Php_Arrays_Variables - Fatal编程技术网

Php 将数组数据传递给循环中的变量

Php 将数组数据传递给循环中的变量,php,arrays,variables,Php,Arrays,Variables,如何将$the\u slug的内容传递给循环中的$post\u 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'; $pos

如何将
$the\u slug
的内容传递给循环中的
$post\u 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";
}

Thanx用于查找。

您可以使用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";
}

我们需要更多的信息。
$tag->slug
是数组还是字符串?
get\u post
函数是什么样子的?get\u post的第一个参数必须是post ID或post对象。我想说的是,这很可能是$tag->slug的副本,其中包含字符串:welcome、home、about、contact等等。$get_posts=get_posts($tag->slug);。。。get_posts是一个wordpress数组,包含meta:[ID]、[post_content]、[post_title]、[post_摘录]、[post_status]、[comment_status]等。。。我正试图让我的标签描述吸引相同名字/鼻涕虫的帖子摘录。谢谢,这离我想要实现的目标更近了。我得到一个“为foreach()提供的参数无效”,我想它指向我的数组。转储数组包含10个应该可以工作的字符串,不是吗?你能在这里发布结果:var\u dump($The\u slug)?$The\u slug=$tag->slug;var_dump($u slug);字符串(7)“欢迎”字符串(4)“主页”字符串(5)”关于字符串(7)“联系人”。。。。。。我知道这是关于在阵列中循环的,但我很难看到什么。标签正在显示,我可以为我所在的帖子插入摘录,但不能为链接到我标签的帖子插入摘录。我想我应该能够使用一个变量来添加到foreach循环中。它并没有说$the_slug是一个数组,它应该像array(3){[0]=>string(7)“welcome”[1]=>string(4)“home”[2]=>string(5)”关于“[3]=>string(7)“contact”}你粘贴了整个输出了吗?现在有意义了。塔克斯。我想我需要重新编写我的函数。我的印象是变量是一个数组,因为它在foreach循环中循环通过不同的帖子。又是唐克斯。