0

我有一个刚刚完成的网站,我想将我现有的 wordpress 与它集成。在主页上注意我想从wordpress数据库中提取帖子并以简短形式显示如下

图片标题摘录

另外我希望完整的帖子内容显示在另一个页面博客页面中。IE

从数据库显示内容中获取帖子内容

我想知道的是有没有办法从数据库中获取这个帖子内容并在我的自定义页面上使用它?

4

1 回答 1

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           
    }
于 2013-07-03T18:01:43.473 回答