Reputation: 1157
I have this website i just completed and i want to integrate my existing wordpress with it. Note on the homepage i want to pull posts from the wordpress database in and display it in short form as follows
image title excerpt
Also i want the full post content to display in another page blog page. i.e
fetch posts content from DB display content
All i want to know is that is there a way to just fetch this posts content from the Database and use it as i want on my custom pages ?
Upvotes: 0
Views: 308
Reputation: 13344
You can access WordPress outside of WordPress and execute a query like so:
<?php
// Bring in WordPress
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
// Setup your query
$args = array(
'numberposts' => 3, 'orderby' => 'date', 'order' => 'DESC', 'post_status'=>'publish'
// adjust as you need
);
// Execute your query
$posts = new WP_Query( $args );
if( $posts->have_posts() ) {
while( $posts->have_posts() ) {
// Loop through resulting posts
$posts->the_post();
if ( has_post_thumbnail( get_the_ID() ) ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumb-rectangle' );
}
// Now do something with your post
?>
<div class="pod">
<?php if( $thumbnail ) { ?><img src="<?php echo( $thumbnail[0] ); ?>" width="" height="" alt="<?php the_title(); ?>" /><?php } ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
<?php
}
}
Quick modification to display one post:
<?php
// Bring in WordPress
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
// Setup your query
$args = array(
'p' => __post_id_here__
// adjust as you need
);
// Execute your query
$posts = new WP_Query( $args );
if( $posts->have_posts() ) {
$posts->the_post();
if ( has_post_thumbnail( get_the_ID() ) ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumb-rectangle' );
}
// Now do something with your post
?>
<?php if( $thumbnail ) { ?><img src="<?php echo( $thumbnail[0] ); ?>" width="" height="" alt="<?php the_title(); ?>" /><?php } ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php
}
Upvotes: 1