I have setup a WordPress blog at www.example.com/blog
and a Codeigniter site at www.example.com
. I am trying to display recent post from blog to my Codeigniter site. To do it, I included the wp-load.php
file using
require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');
// require_once('../app/blog/wp-load.php');
$args = array(
// 'cat' => 3, // Only source posts from a specific category
'posts_per_page' => 4 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<marquee align="top" behavior="scroll" onmouseover="this.stop();" onmouseout="this.start();" direction="up" scrolldelay="200" ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<li>
<?php if ( has_post_thumbnail() ) { ?>
<span class="post_thumbnail"><?php the_post_thumbnail(); ?></span>
<?php } ?>
<span class="post_title"><?php the_title(); ?></span>
</a>
<!--span class="post_time">Posted on <?php the_time('l jS F, Y') ?></span-->
<?php //the_excerpt(); ?>
</li>
</marquee >
<? }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>
but throws an internal server error.
Do I have to change the file permission? Please help. The site is hosted by Godaddy.
Error detail :
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.Please contact the server administrator to inform of the time the
error occurred and of anything you might have done that may have
caused the error.More information about this error may be available in the server error
log.
Never include any WordPress file in your main website as it can lead to many errors. Instead, use Codeigniter’s second database connection to connect to your WordPress database.
Searching for get wordpress posts from database to codeigniter I have found this gist and this article. The following code is taken from this article. Note that this code is for Codeigniter 2 and the code is not tested. But I have dealt with many databases in Codeigniter before, just ask if you find any trouble.
a. add a different settings data in your
application/config/database.php
file along side with your main website database settings:b. build a WordPress model in
application/models/wordpress_model.php
:c. create the view file in
application/views/wordpress_posts_view.php
:d. now, you can call all this in any Codeigniter controller: