0

I'm having a bit of trouble trying to understand the Wordpress functions to display posts from a Wordpress blog to a website.

Basically I have a website www.site.com and a blog B with Wordpress structure located in www.site.com/blog. I want to learn to display B's recent posts in A.

I know I'll have to use PHP to call B's posts and display them in A as HTML elements. I messed around with the PHP functions but wasn't able to execute a solution correctly.

Can you help me please?

Luiz Cruz
  • 17
  • 7

3 Answers3

3

Load the wp-blog-header.php file from site B in site A.

Like this:

<?php 
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
?>

To load the posts:

<ul>
<?php

$args = array( 'posts_per_page' => -1 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();?>

</ul>
  • Thanks, WordpressDave. I already did this but don't I need other functions to call the post themselves? – Luiz Cruz Sep 27 '16 at 16:33
  • WordpressDave I feel we're getting close, thanks. I'm using a test file, say www.site.com/test.php and I added the PHP functions you mentioned on this file using www.site.com/blog/wp-blog-header.php as the php "require". Is that correct? – Luiz Cruz Sep 27 '16 at 16:46
1

Thanks for all the help.

As I said, I installed Wordpress on site.com/blog and I wanted site.com's homepage (index.php) to show, say, 3 Wordpress posts on the "News" section of the page.

So, for site.com I had to add:

<div id="news">
<p>Here you can see our latest News</p>
<?php 
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php'); //Note the folder hierarchy to find wp-blog-header.php
?>
<?php
$my_query = new WP_Query('showposts=3'); //"3" being the number of posts to be shown
while ($my_query->have_posts()): $my_query->the_post();
?>
<h3><?php the_title() ?></h3> //This makes the post title show inside a h3 tag
<p><?php the_excerpt() ?></p> //This makes the post excerpt show inside a p tag
<?php endwhile;
?>
</div>
Luiz Cruz
  • 17
  • 7
0

You shouold look into RSS feeds and parsing those out into the data you need. Most WP installations will have the RSS available, SEE HERE.

There are many ways to do this, but I have found that grabbing the data already available is simple and writing out a controller to parse and display the data you seek is simple enough. This is just one way, may or may not work for your application but I have had great luck.

Good luck and hope this can steer you in a direction toward your solution.

NSTuttle
  • 3,237
  • 2
  • 17
  • 26
  • Thanks, llldapt. I found my RSS feed but I have no idea how to display its content on my HTML. – Luiz Cruz Sep 27 '16 at 16:30
  • Take a look at this [ANSWER HERE](http://stackoverflow.com/questions/10943544/how-to-parse-an-rss-feed-using-javascript) This goes into the parsing of the data and displaying HTML. I'm not sure about your skill level with code, if this is above what you know I will try to clarify further. – NSTuttle Sep 27 '16 at 16:33
  • Also, "Blog B" lives on a different website than "Blog A" correct? – NSTuttle Sep 27 '16 at 16:34
  • llldapt, A is a website like www.site.com and B is the Wordpress blog living on www.site.com/blog – Luiz Cruz Sep 27 '16 at 16:39
  • If it is on the same site, I would recommend you continue on with WordpressDave above. Keep RSS parsing in mind if you were looking at cross domain, Mobile App "blog" implementation, Native app "blog" implementation and the like. While you _could_ use this, you would be doing more work. – NSTuttle Sep 27 '16 at 18:46