2
votes

I've got a blog and a website; the two are separate things all together. For example, I have www.domainname.com as the main site, and the blog would be at www.domainname.com/blog/

It's a WordPress blog, however I don't want people looking at the WordPress front-end, so I would like to write a php function that would pull the posts from WordPress to a separate page on the main site.

The function I've got so far is as follows:

<div id="blogPosts">
<?php
    require('../path1/path2/wp-blog-header.php');
?>    
<?php
    $posts = get_posts('numberposts=10&order=DESC&orderby=post_title');
    foreach ($posts as $post) : start_wp();
?>  
    <h4 class="blogDate"> <? the_date(); ?> </h4>
    <hr />
    <h5 class="blogTitle"> <? the_title(); ?> </h5>
    <p class="blogText"> <? the_excerpt() ?> </p>
    <br />
<?php
    endforeach;
?>
</div>

It will display the page fine, but it's not posting the posts to the page at all. Any ideas why it won't work?

3
You're missing a semicolon the_excerpt(); - Milo LaMar

3 Answers

1
votes

first, put this before u include wp-blog-header.php

define('WP_USE_THEMES', false);

and i think you are looking wp_query not get_posts, if you want to use the loop functions

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();
    the_title();
endwhile;
wp_reset_postdata();
1
votes

Are you sure you are getting the posts? Try dumping your posts after you fetched them, like this:

var_dump($posts)

Does the $posts-variable contain anything? If not, you probably won't get the posts, because $wpdb (the database connection class) is still undefined.

Secondly, what happens when you try

<?php echo $post->post_date ?>

instead of

<? the_date(); ?>

It could be that wordpress doesn't realise you are in a loop and therefor the loop-functions (e.g. the_date, the_title, ...) won't work.

let me know if one of these things works.

0
votes

Just from a quick search I see that usage of get_posts( $args ) expects $args to be an array with these optional values

$args = array(
'numberposts'     => 5,
'offset'          => 0,
'category'        => ,
'orderby'         => 'post_date',
'order'           => 'DESC',
'include'         => ,
'exclude'         => ,
'meta_key'        => ,
'meta_value'      => ,
'post_type'       => 'post',
'post_mime_type'  => ,
'post_parent'     => ,
'post_status'     => 'publish' );