このコードだと、ID2の場合とそれ以外の場合にdivのclass指定を変えるというコードのように見受けられますが?記事そのものを表示・非表示切替えたいのですよね?
だったら、まあ通常はquery posts
でauthor=2
とかやるのですけど、あえて今のコードを生かすとすれば、
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( get_the_author_ID() == 2 ) { ?>
<div class="post-cat-three">
<small><?php the_time('F jS, Y'); ?></small>
<!-- 記事の本文をdiv内に表示 -->
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php } ?>
<!-- “else”部分を除いたWordPressループ終了 -->
<?php endwhile; else: ?>
<p>現在お知らせはありません。</p>
<?php endif; ?>
という感じ?まあ、単純にID2の場合だけ表示するという記述にしただけですが。
ただ、この場合問題が多く、ループの数は変更されないので、実際に表示される記事の件数が変動します。最悪の場合記事表示0もありえますので、良いコードだとは言えません。
やはり、query posts
で制御したほうが良いと思いますね;)
素早い返信ありがとうございます。
本当は下のコードで簡単に表示できるのかと思ったら表示できませんで、
記事もマックス5つだけ表示しようと
< ?php $recent = new WP_Query(”page_id=2&showposts=1“); while($recent->have_posts()) : $recent->the_post();?>
< ?php the_excerpt(’Read the rest of this entry »’); ?>
< ?php endwhile; ?>
などを流用しようとしましたが結局できませんでした。
<?php
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
..
endwhile;
else:
..
endif;
//Reset Query
wp_reset_query();
?>
query_posts('posts_per_page=5&author=2');
とかじゃないですかね?
下記のコードでエラーがでるんですが、やはり根本的に文法が間違っているんでしょうか?
<?php
//The Query
query_posts('posts_per_page=5&author=2');
//The Loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
<div class="post-cat-three">
<small><?php the_time('F jS, Y'); ?></small>
<!-- 記事の本文をdiv内に表示 -->
<div class="entry">
<?php the_content(); ?>
</div>
</div>
endwhile;
else:
<p>現在お知らせはありません。</p>
endif;
//Reset Query
wp_reset_query();
?>
shokun0803さん
<?php the_time('F jS, Y'); ?>
<?php the_content(); ?>
がだめということですか?
ではどのように記述すればよいのでしょうか?すんません。。。
<?php
//The Query
query_posts('posts_per_page=5&author=2');
//The Loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<div class="post-cat-three">
<small><?php the_time('F jS, Y'); ?></small>
<!-- 記事の本文をdiv内に表示 -->
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
else: ?>
<p>現在お知らせはありません。</p>
<?php endif;
//Reset Query
wp_reset_query();
?>
現在のコードをそのまま利用したとしたらこうかな?ちょっと強引(あまりきれいなコードではない)ですが。
なるほど・・・かこみの位置ですね、ありがとうございます。