我有这个网站我刚刚完成,我想将我现有的wordpress与它集成。 在主页上注意我想从wordpress数据库中提取帖子并以简短形式显示如下
图像 标题 摘录
此外,我希望完整的帖子内容显示在另一个页面博客页面中。 即
从数据库中提取帖子内容 显示内容
我想知道的是,有没有办法从数据库中获取这些帖子内容并在我的自定义页面上使用它?
答案 0 :(得分:1)
您可以在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
}
}
快速修改以显示一个帖子:
<?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
}