无法通过 foreach 循环处理数组 - 请帮忙?

发布于 2025-01-05 21:09:01 字数 1654 浏览 0 评论 0原文

我正在构建一个目录站点,并且一直坚持让 foreach 循环遍历多站点的博客 ID。从代码中的注释可以看出,博客 ID 的查询工作正常,print_r 检查显示数组已填满,但是当函数到达第一个 foreach 时,循环每次都返回相同的结果, foreach 循环中 site_blog_id 上的 print_r 显示它是空的。在循环中手动设置 site_blog_id 可以使其余代码正常工作,因此这肯定与 foreach 对数组的处理有关。

我非常困惑,因为这似乎与我在开发人员网站上看到的许多 array-foreach 代码示例相同,包括查询文档页面上的代码。我想知道是否需要对保存数组的 site_blog_ids 变量做一些事情,以使其与 foreach 一起使用,但坦率地说,我被困住了。 任何帮助将不胜感激! 大卫

/* get all subsite blog ids */
global $wpdb; 
$site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs where blog_id > 1")); 

print_r( $site_blog_ids );
/* check output - shows "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) ". Looks fine? */

/* loop to iterate through the ids and display info from each blog */
foreach( $site_blog_ids AS $site_blog_id ) { 

print_r( "siteid= ".$site_blog_id."</br>" ); 
/* check output - this shows $site_blog_id is empty, no value. That's not right, should be each blog ID in turn from the site_blog_ids array. */

$oldblog = $wpdb->set_blog_id( $site_blog_id ); 
/* sets the system to use the loop blog ID instead of the current one. Since site_blog_id is empty, this doesn't work so the rest of the code uses the current blog, not the intended one. */

global $post;
$first_posts = get_posts('p=1'); //get the first post
foreach ( $first_posts AS $post ) {
    setup_postdata();
    the_title();
    the_excerpt();
    the_content();
    the_category();
}
}
/* Tell the $wpdb object to go back to using the current site */
$wpdb->set_blog_id( $oldblog );

I'm building a directory site and am stuck on getting the foreach to loop through the multisite's blog IDs. As you can see from the comments in the code, the query for the blog IDs is working fine, the print_r checks show that the array is filled, but when the function gets to the first foreach, the loop returns the same result each time, and print_r on site_blog_id within the foreach loop shows it's empty. Setting site_blog_id manually in the loop makes the rest of the code work fine, so it's definitely something with the foreach's processing of the array.

I'm highly puzzled because this seems identical to many examples of array-foreach code I've seen on the developer's site, including the ones on the query docs page. I'm wondering if I need to do something with the site_blog_ids variable holding the array to make it work with the foreach, but frankly I'm stuck.
Any help will be greatly appreciated!
David

/* get all subsite blog ids */
global $wpdb; 
$site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs where blog_id > 1")); 

print_r( $site_blog_ids );
/* check output - shows "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) ". Looks fine? */

/* loop to iterate through the ids and display info from each blog */
foreach( $site_blog_ids AS $site_blog_id ) { 

print_r( "siteid= ".$site_blog_id."</br>" ); 
/* check output - this shows $site_blog_id is empty, no value. That's not right, should be each blog ID in turn from the site_blog_ids array. */

$oldblog = $wpdb->set_blog_id( $site_blog_id ); 
/* sets the system to use the loop blog ID instead of the current one. Since site_blog_id is empty, this doesn't work so the rest of the code uses the current blog, not the intended one. */

global $post;
$first_posts = get_posts('p=1'); //get the first post
foreach ( $first_posts AS $post ) {
    setup_postdata();
    the_title();
    the_excerpt();
    the_content();
    the_category();
}
}
/* Tell the $wpdb object to go back to using the current site */
$wpdb->set_blog_id( $oldblog );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2025-01-12 21:09:02

请尝试以下操作:

/* loop to iterate through the ids and display info from each blog */
foreach( $site_blog_ids AS $site_blog_id ) { 

print_r( "siteid= ". $site_blog_id->blog_id ."</br>"); 
/* check output - this shows $site_blog_id is empty, no value. That's not right, should be each blog ID in turn from the site_blog_ids array. */

Try the following:

/* loop to iterate through the ids and display info from each blog */
foreach( $site_blog_ids AS $site_blog_id ) { 

print_r( "siteid= ". $site_blog_id->blog_id ."</br>"); 
/* check output - this shows $site_blog_id is empty, no value. That's not right, should be each blog ID in turn from the site_blog_ids array. */
一梦等七年七年为一梦 2025-01-12 21:09:02

print_r( "siteid= ".$site_blog_id."
" );

是不正确的。

当它到达 printr 部分时,它会将其视为字符串。

我认为您试图做的是:

echo“siteid=”。 print_r($site_blog_id, true) ."
";

print_r( "siteid= ".$site_blog_id."</br>" );
is incorrect.

By the time it hits the printr section, it's looking at that as a string.

I think what you were attempting to do was:

echo "siteid=". print_r($site_blog_id, true) ."</br>";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文