wordpress:静的なサイトのindex.phpにwordpressの投稿を表示させる方法
静的なサイトのトップページのみにお知らせとして投稿を表示させたい時の方法を紹介します。 index.htmlはindex.phpへ拡張子を変えます。 そして投稿を表示させたい部分は下記のコードを使います。
<table class="table">
<tbody>
<?php require_once('./news/wp-load.php'); ?>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
);
$news_posts = get_posts($args);
foreach ($news_posts as $post):
setup_postdata($post);
?>
<tr>
<td class="date">
<?php the_time('Y/m/d'); ?>
</td>
<td class="title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</td>
</tr>
<?php endforeach; wp_reset_postdata(); ?>
</tbody>
</table>
そして、wordpressは今回の場合お知らせとしてnewsというディレクトリを作成し、その中にwordpressをインストールします。
このnewsディレクトリへは通常通りのインストールで大丈夫です。
さくらサーバーの場合、これにphp.cgiという名前のファイルを作成し、下記のコードを書きトップページのindex.phpと同じ階層にアップする必要があります。
#!/bin/sh exec /usr/local/bin/php-cgi
そして、ファイルのパーミッションを755に設定します。
これで、TOPページにwordpressの投稿を表示することができます。