WordPress.org

Ready to get started?Download WordPress

Forums

PHP: Displaying recent posts from multiple WP blogs on non-WP static pg (16 posts)

  1. fizzybeep
    Member
    Posted 6 years ago #

    I'm using this PHP code to display the most recent post from a WordPress blog on a separate, static, non-Wordpress page. The blog and static page are part of the same site. The code works fine for one blog.

    <?php
    		$how_many=1; //how many posts to display
    		require('blog1/wp-config.php'); //the path to the wp-config file of the blog I want to use
    		?>
    		<?
    		$news=$wpdb->get_results("SELECT 'ID','post_title','post_content' FROM $wpdb->posts
    		WHERE 'post_type'=\"post\" AND 'post_status'=\"publish\" ORDER BY post_date DESC LIMIT $how_many"); ?>
    		<?
    		foreach($news as $np){
    		printf ("<div class='normalText'>%s</div>", $np->post_content);
    		}?>

    The code isn't of my own creation; I've just edited it a little. I'm not very familiar with PHP, so I'm not sure why when I try the same thing twice on one page, with different blogs (i.e. once as above, then once again later in the page with "blog2/wp-config" rather than "blog1/wp-config"), it won't work. In fact, it simply displays (twice) the first post from whichever blog I refer to in the first section of code.

    I have managed to display what I want, by putting the code for each blog excerpt in its own webpage, and then using iframes to display all of them on the main page. However, any advice toward achieving a neater solution would be much appreciated!

    Thanks a lot!

  2. Marcel Brinkkemper
    Member
    Posted 6 years ago #

    why don't you just display a feed from the wordpress blog?

  3. KentCurtis
    Member
    Posted 5 years ago #

    I am kind of looking to do something like the original poster, but I would just like to display recent posts in a given category. What php include would I use?

  4. mores
    Member
    Posted 5 years ago #

    @fizzybeep ... I guess when you call the second loop, it ignores the settings in the config.php file for the second blog and just uses the first blog's information.
    I'm not good with that kind of stuff, but maybe you need to use require-once for the config file ?

  5. CrimsonLB42
    Member
    Posted 5 years ago #

    I am very interested in this as well. I house a WP-blog on a custom-built static website and would like to display the latest blog postings on my main page. Can I simply paste some PHP on the static main page and have it read and display the blog feed?

    If so, please post it in a response. It would be greatly appreciated.

    ***Note: I tried pasting the code at the top of this string, but it did not work. The area on the main page was simply blank.

  6. Brendon Boshell
    Member
    Posted 5 years ago #

    This is likely to cause some problems as some global variables and constants may not be set, or functions may be re-defined. Personally, I would just query the database directly (ie, without loaded WordPress classes and files).

  7. Janis Elsts
    Member
    Posted 5 years ago #

    I second the RSS feed suggestion. Here's one example using the SimplePie parser :

    <?php
    $feed_url = 'http://w-shadow.com/feed/';
    $max_items = 5;
    
    //Load SimplePie
    include 'includes/simplepie.inc';
    
    //Fetch the RSS feed
    $feed = new SimplePie($feed_url);
    
    //Check for errors
    if ($feed->error())
    	echo 'Error : ',$feed->error();
    
    //Output up to $max_items posts
    foreach ($feed->get_items(0, $max_items) as $item): ?>
    
    	<div class="item">
    		<h3 class="title"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h3>
    		<?php echo $item->get_description(); ?>
    		<p><small>
    		Posted  <?php if ($author = $item->get_author()){ echo ' by '.$author->get_name(); }?>
    		on <?php echo $item->get_date('j F Y | g:i a'); ?>
    		</small></p>
    	</div>
    
    <?php
    endforeach;
    ?>

    I've posted a more detailed tutorial on my blog.

  8. SJWilson
    Member
    Posted 5 years ago #

    I have a client that is very anxious for me to do this as well - get the last 3 blog feeds added to a non WordPress page as links to those articles on her blog. The RSS way should be fine.

    Whiteshadow - I am VERY interested in viewing the above mentioned tutorial on your blog but the server seems to be down.

  9. ericr23
    Member
    Posted 5 years ago #

    I have feeds from several blogs on the site's home page. If the blogs are all your own, or under your management, here's what I did.

    In each blog directory, create a php file:

    <?php require_once('wp-blog-header.php');
    $posts = get_posts('numberposts=9&order=DESC&orderby=post_date');
    foreach ($posts as $post) : start_wp(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
    <?php endforeach;  ?>

    (Specify whatever other number of posts you want ['9' here] and add whatever loop material and styling you want along with the linked title shown here.)

    Then include the file for each blog in the home page script.

    I actually run them via AJAX after the page is loaded, but that's another story.

    See it at work.

  10. pkoury
    Member
    Posted 5 years ago #

    I don't think anyone is really understanding quite properly. I have the same basic need with a small twist. I have a feed to my homepage from my blog.

    Blog

    Homepage

    The homepage feed is based on Google's Feed API. I would like however to be able to select a blog post as my feature on the front page above where the feed of recent data is coming in. Right now there is currently a banner there that leads to the post that I am interested in having as my homepage content.

    Is there some plugin or something that would allow me to place some code on my homepage and then select a post for it to display within that outside code? For instance if I wanted a post that I made 3 posts ago to show up I would go in to WP admin tool and select that post and it would show up on the homepage until I defined another post to take its place.

  11. ericr23
    Member
    Posted 5 years ago #

    I actually also do something similar using the DS-rating plugin to mark some posts as "select", which are displayed on the the home page by including the following code residing in a php file in the blog directory:

    <?php
    require_once('wp-blog-header.php');
    $selposts = $wpdb->get_results("SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = '_ds_rating'
    AND wpostmeta.meta_value > '0'
    AND wposts.post_status = 'publish'
    ORDER BY wposts.post_date DESC
    LIMIT 12", OBJECT);
    
    foreach ($selposts as $post) :
    setup_postdata($post);
    
    echo '<a href="';
    the_permalink();
    echo '">';
    the_title();
    
    endforeach;
    ?>

    This collects posts given any rating with DS-rating, including 0, if I recall correctly, because the value is a text variable, not an integer.

    If you changed the LIMIT to 1, this might work for you, to get just the latest "rated" post.

  12. pkoury
    Member
    Posted 5 years ago #

    Could you try explaining that a bit more in depth?

    Which file in the blog directory needs to be edited after installing the DS-rating plugin?

  13. ericr23
    Member
    Posted 5 years ago #

    Create a new file in the blog root directory, containing the above code. Name it something.php, and include it where desired on your home page (<?php include_once('/blog/something.php'); ?>.

    By running WP code in the blog directory, you save a lot of effort that would otherwise be required to access its database.

  14. pkoury
    Member
    Posted 5 years ago #

    It only posts a link on the page. I am looking to display the full post text and images.

  15. ericr23
    Member
    Posted 5 years ago #

    Then you will have to edit the section

    echo '<a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '</a>';

    to present what you want. It is like "the loop".

    (Note that I had accidentally left off the closing code earlier.)

  16. bittup
    Member
    Posted 5 years ago #

    rss2html php script.

    I am member of this web site. They announced this script realease yesterday. It simply displays my feed on my .shtml pages.

Topic Closed

This topic has been closed to new replies.

About this Topic