I have a Wordpress MU installation. Could someone tell me how I can load posts from a blog into the subdomain in my main domain?
I have a Wordpress MU installation. Could someone tell me how I can load posts from a blog into the subdomain in my main domain?
One option is you use RSS Feed Wordpress works with some plugins that might be interesting, at a glance at wordpress.org/plugins/tags/rss-feed
It's just a question of using switch_to_blog()
and switch to the blog you want, make a query blog posts and use the same function to switch back to the original blog.
Create a Must plugin (always active and available at the entire network) and put the following code:
if( !function_exists('get_blog_posts') )
{
function get_blog_posts( $blog_id, $args = array() )
{
$current_blog_id = get_current_blog_id();
switch_to_blog( $blog_id );
$posts = get_posts( $args );
switch_to_blog( $current_blog_id ); // get_posts já foi feito, podemos restaurar
if( $posts )
return $posts;
return false;
}
}
Then in the template of the theme or in some function within functions.php
, use the following, setting $blog_id_consultar
and $parametros
. Note that echo print_r()
is for verification only, loop $posts_do_blog
to print HTML:
<?php
if( function_exists('get_blog_posts') ) {
$blog_id_consultar = '3';
$parametros = array(); // VIDE http://codex.wordpress.org/Function_Reference/get_posts
$posts_do_blog = get_blog_posts( $blog_id_consultar, $parametros );
if( $posts_do_blog ) {
echo '<pre>' . print_r( $posts_do_blog, true ) . '</pre>';
}
}
?>
<?php
query_posts('showposts=4'); //Query WordPress para listar os últimos 4 posts
?>
<?php while (have_posts ()): the_post(); ?>
<h2>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<p><?php the_author(); ?> | <?php the_date(); ?>
<?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
I'm using this code, I want it to load posts from another WordPress blog.