◆ これは記事は3件、カテゴリーIDは6と7を表示している <?php $myposts = get_posts('numberposts=3&category=6,7'); foreach($myposts as $post) : ?> ◆アイキャッチ画像 <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(115,80)); ?></a> ◆カテゴリー別にアイコンを表示 <?php $cats = get_the_category(); $cats = $cats[0]; ?> <dt class="<?php echo $cats->category_nicename;?>"></dt> ↑こうするとスラッグ名がclassに入るので、CSSで背景設定する ◆タイトル(文字数制限) <h3><a href="<?php the_permalink(); ?>"><?php if(mb_strlen($post->post_title,'UTF-8')>70) {$title= mb_substr($post->post_title,0,70,'UTF-8');echo $title.…;} else {echo $post->post_title;}?></a></h3> ◆記事内容(文字数制限) <p><?php echo mb_substr(strip_tags($post-> post_content),0,120).'...'; ?></p> <?php endforeach; ?>
実際に表示させた例
<?php $myposts = get_posts('numberposts=3&category=6,7'); foreach($myposts as $post) : ?> <div class="topWn clearfix"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(115,80)); ?></a> <dl> <?php $cats = get_the_category(); $cats = $cats[0]; ?> <dt class="<?php echo $cats->category_nicename;?>"></dt> <dd> <h3><a href="<?php the_permalink(); ?>"><?php if(mb_strlen($post->post_title,'UTF-8')>70) {$title= mb_substr($post->post_title,0,70,'UTF-8');echo $title.…;} else {echo $post->post_title;}?></a></h3> <p><?php echo mb_substr(strip_tags($post-> post_content),0,120).'...'; ?></p> </dd> </dl> <div class="topWn_next"><a href="<?php the_permalink(); ?>">続きを読む</a></div> </div> <?php endforeach; ?>