WordPress の外部で WordPress にアクセスし、次のようにクエリを実行できます。
<?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
}
}
1つの投稿を表示するための簡単な変更:
<?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
}