ওয়ার্ডপ্রেস ফাংশনটি switch_to_blog()একটি ইনপুট পরামিতি হিসাবে কোনও পূর্ণসংখ্যার প্রত্যাশা করে। আপনি কোডেক্সে এটি সম্পর্কে আরও পড়তে পারেন:
http://codex.wordpress.org/Function_Reference/switch_to_blog
পরিবর্তে এই ধরণের কাঠামো চেষ্টা করুন:
// Get the current blog id
$original_blog_id = get_current_blog_id();
// All the blog_id's to loop through
$bids = array( 1, 2 );
foreach( $bids as $bid )
{
// Switch to the blog with the blog_id $bid
switch_to_blog( $bid );
// ... your code for each blog ...
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
হালনাগাদ:
আপনি যদি প্রতিটি ব্লগের জন্য বিভিন্ন বিভাগ থেকে পোস্টগুলি পেতে চান তবে আপনি উদাহরণস্বরূপ ব্যবহার করতে পারেন:
// Get current blog
$original_blog_id = get_current_blog_id();
// Setup a category slug for each blog id, you want to loop through - EDIT
$catslug_per_blog_id = array(
1 => 'video',
4 => 'news'
);
foreach( $catslug_per_blog_id as $bid => $catslug )
{
// Switch to the blog with the blog id $bid
switch_to_blog( $bid );
// ... your code for each blog ...
$myposts = get_posts(
array(
'category_name' => $catslug,
'posts_per_page' => 10,
)
);
// ... etc
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
উদাহরণ:
এখানে একটি উদাহরণ যা আপনাকে টেমপ্লেট ট্যাগগুলি ব্যবহার করতে দেয় (এটি আমার মাল্টিসাইট ইনস্টলের উপর কাজ করে):
// Get current blog
$original_blog_id = get_current_blog_id();
// Setup a category for each blog id you want to loop through - EDIT
$catslug_per_blog_id = array(
1 => 'video',
4 => 'news'
);
foreach( $catslug_per_blog_id as $bid => $catslug )
{
//Switch to the blog with the blog id $bid
switch_to_blog( $bid );
// Get posts for each blog
$myposts = get_posts(
array(
'category_name' => $catslug,
'posts_per_page' => 2,
)
);
// Skip a blog if no posts are found
if( empty( $myposts ) )
continue;
// Loop for each blog
$li = '';
global $post;
foreach( $myposts as $post )
{
setup_postdata( $post );
$li .= the_title(
$before = sprintf( '<li><a href="%s">', esc_url( get_permalink() ) ),
$after = '</a></li>',
$echo = false
);
}
// Print for each blog
printf(
'<h2>%s (%s)</h2><ul>%s</ul>',
esc_html( get_bloginfo( 'name' ) ),
esc_html( $catslug ),
$li
);
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
wp_reset_postdata();
বিথোভেন নামের সাইটের 1 এবং বাখ নামের 4 নম্বর সাইট সহ আমাদের উপরের উদাহরণের জন্য এখানে একটি ডেমো স্ক্রিনশট রয়েছে :

দ্রষ্টব্য: ধন্যবাদ প্রদানের @brasofilo করার লিংক যে সুস্পষ্ট আমার ভুল বোঝাবুঝি restore_current_blog();-)
পিপিএস: নিম্নলিখিত মন্তব্য ভাগ করে নেওয়ার জন্য @ ক্রিসটাইনকুপারকে ধন্যবাদ:
শুধু একটি বন্ধুত্বপূর্ণ সতর্কতা। আপনার আসল ব্লগ আইডিটি পরিবর্তনশীল হিসাবে সেট না করার বিষয়টি নিশ্চিত করুন $blog_id- এটি কারণ কারণ switch_to_blog()
প্রক্রিয়া চলাকালীন , $blog_idমূল ফাংশন দ্বারা ওভাররেইন করা হবে, যার অর্থ আপনি যখন মূল ব্লগে ফিরে যাওয়ার চেষ্টা করবেন, আপনি শেষটিতে স্যুইচিংয়ের সাথে শেষ করবেন আপনি যার মধ্য দিয়ে লুপ করেছেন। কিছুটা মন-ধাঁধা। :)