0

完成したばかりのこのウェブサイトがあり、既存のワードプレスをそれに統合したいと考えています。ホームページで、WordPressデータベースから投稿を取得し、次のように短い形式で表示したいことに注意してください

画像タイトル抜粋

また、完全な投稿コンテンツを別のページのブログ ページに表示したいと考えています。すなわち

DB表示コンテンツから投稿コンテンツを取得

私が知りたいのは、この投稿のコンテンツをデータベースから取得して、カスタムページで必要に応じて使用する方法があるということだけですか?

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           
        }
    }

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