【特定のカテゴリーの場合:単独】
<?php if ( in_category(array('news')) ) : ?>
<h1 class="title_sub">お知らせ</h1>
<?php else://通常ページの場合 ?>
<h1 class="title_sub">その他</h1>
<?php endif; ?>
【特定のカテゴリーの場合:複数】スラッグを指定する
<?php if ( in_category(array('design','life','html')) ) ://特定のカテゴリーの場合 ?>
<link rel="stylesheet" href="****/special.css" type="text/css" />
<?php else://通常ページの場合 ?>
<link rel="stylesheet" href="****/style.css" type="text/css" />
<?php endif; ?>
カンマで区切ってIDを記述して削除
function WP_Widget_Recent_Posts_Exclude_init() {
unregister_widget('WP_Widget_Recent_Posts');
register_widget('WP_Widget_Recent_Posts_Exclude');
}
add_action('widgets_init', 'WP_Widget_Recent_Posts_Exclude_init');
カテゴリ一覧から特定のカテゴリを複数削除する場合
//カテゴリ一覧から削除
function exclude_widget_categories($args){
$exclude = array(9, 13, 14); // The IDs of the excluding categories
$args["exclude"] = $exclude;
return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");
<div id="mobinews">
<div class="flame_leftTOP">
<h2 id="info">お知らせ</h2>
<div class="news">
<ul>
<?php query_posts('posts_per_page=2&cat=48&paged='.$paged); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title();?></a> <span class="fsmall2 gray">(<?php the_time('m月d日') ?>)</span>
<div class="news_info"><?php echo mb_substr(get_the_excerpt(), 0, 70); ?> <a href="<?php the_permalink(); ?>">[...続きを読む]</a></div>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div></div>
<div class="flame_rightTOP">
<h2 id="info">更新情報</h2>
<div class="news">
<ul>
<?php query_posts('posts_per_page=3&cat=-48&paged='.$paged); ?>#ここで特定の記事をマイナスで表示させなくする
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post->ID, 'top-page'); ?></a>
<a href="<?php the_permalink(); ?>"><?php the_title();?></a> <span class="fsmall2 gray">(<?php the_time('m月d日') ?>)</span>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div></div>
<img class="cler" src="http://orbit-ohata.com/wp-content/themes/wp_orbit/img/border.gif" alt="border" />
<div class="flame_leftTOP s20">
<h2 id="info">初めてのお客様</h2>
<div class="news">
<ul>
<?php query_posts('posts_per_page=2&cat=57&paged='.$paged); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title();?></a> <span class="fsmall2 gray">(<?php the_time('m月d日') ?>)</span>
<div class="news_info"><?php echo mb_substr(get_the_excerpt(), 0, 70); ?> <a href="<?php the_permalink(); ?>">[...続きを読む]</a></div>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div></div>
<div class="flame_rightTOP s20">
<h2 id="info">抜け毛が気になるお客様</h2>
<div class="news">
<ul>
<?php query_posts('posts_per_page=4&cat=7&paged='.$paged); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title();?></a> <span class="fsmall2 gray">(<?php the_time('m月d日') ?>)</span>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div></div>
<img class="cler" src="http://orbit-ohata.com/wp-content/themes/wp_orbit/img/border.gif" alt="border" />
</div>
<<CSS>>
/*更新情報*/
.fsmall2{font-size:80%;}
.news{
border:solid 1px #000000;
padding:15px 20px 40px 40px;
margin-top:-10px;
height:130px;
}
.news h2.widgettitle{display:none;}
.news ul li li{list-style:disc;}
.flame_leftTOP{
width:495px;
float:left;
}
.flame_rightTOP{
width:495px;
float:right;
}
<div id="news">
<div class="wid1000">
<h2>お知らせ</h2>
<ul>
<?php query_posts('posts_per_page=5&cat=6&paged='.$paged); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><span class="fsmall gray"><?php the_time('m月d日') ?></span> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
<p class="aligncenter"><a href="https://tricho.jp/archives/category/news" class="btn-flat-border">お知らせをもっと見る</a></p>
</div>
</div>
<?php query_posts("cat=3&showposts=5"); ?>
cat=3 カテゴリID
showposts=5 表示件数
query_posts で特定のカテゴリIDを表示させる事が出来る!大変便利!!
それを踏まえてループで投稿記事を取得する。
<?php if(have_posts()): while(have_posts()): the_post(); ?>
ここにループさせる
<?php endwhile; endif; ?>
これで特定のカテゴリ一覧を取得できる!
<?php query_posts('cat=4&showposts=2'); ?> <!--カテゴリIDと表示件数-->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!--表示する内容が入ります。-->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<p><a href="<?php the_permalink() ?>">続きを見る</a></p>
<!--表示する内容ここまで-->
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
queryを使ったら、最後にデータを閉じてあげる。<?php wp_reset_postdata(); ?>
> 5 で表示件数を変更
> 'DESC' で降順 昇順は > 'ASC'
<?php
$args = array('posts_per_page' => 5, 'offset' => 0, 'order' => 'DESC', 'orderby' => 'date');
$newpost_query = new WP_Query($args);
?>
<?php if ($newpost_query->have_posts()): ?>
<h3>最近の投稿</h3>
<ul>
<?php while ($newpost_query->have_posts()): $newpost_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
特定の投稿記事の内容を固定ページに表示させる方法。これ便利!!!
<?php
$post_id = 319; //数字は記事のIDを入れてください
$post = get_post($post_id, 'OBJECT' , 'raw'); //指定した記事のIDの情報を取得
$post_include = apply_filters( 'the_content',$post->post_content); //記事の本文をフィルターフックで整形してます
echo $post_include; //出力します
?>
cat=カテゴリID、showposts=何件表示させるか、orderby=randでランダム表示。ソートを決める
<?php query_posts('cat=4&showposts=4&orderby=rand');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_post_thumbnail( 'thumbnail' ); ?></a>
<a href="<?php the_permalink();?>"><span><?php the_time('Y.m.d');?></span>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile;endif;?>
<?php wp_reset_query(); ?>
【通常の前後の記事表示とページナビゲーションの切り替え】
<?php if(is_single()): ?>
<div id="pagelink">
<p><?php previous_post_link(); ?></p>
<p><?php next_post_link(); ?></p>
</div>
<?php else: ?>
<?php if (function_exists("pagination")) {pagination($additional_loop->max_num_pages); } ?>
<?php endif; ?>
【前後をtrueにする】
<?php previous_post_link('« %link', '%title', true , ''); ?>
<?php next_post_link('%link »', '%title', true , ''); ?>
| 第1引数 | リンクの前後に「« 」などの追加する文字を設定できます。「%link」にaタグが挿入されます。 |
|---|---|
| 第2引数 | 表示するリンクのテキスト。デフォルトは前の記事のタイトルです。 |
| 第3引数 | true / false 表示している記事と同じカテゴリーの記事を表示するかどうかを設定。true の場合、同じカテゴリーの記事だけが表示されます 。(デフォルト値は false ) |
| 第4引数 | 表示させたくない記事のカテゴリー ID の番号を記述します。複数のカテゴリーを除外する場合は and で区切ります。 |
<?php previous_post_link('<i class="icon-angle-left"></i> %link', '%title', true , ''); ?>
<?php next_post_link('%link <i class="icon-angle-right"></i>'', '%title', true , ''); ?>
複数のカテゴリを除外したい場合には and で結んであげる。
<?php previous_post_link('« %link', '%title', true , '1 and 2'); ?>
<?php next_post_link('%link »', '%title', true , '1 and 2'); ?>
記事を適当な文字数で区切ってくれる。画面が見やすく便利!!
<?php
echo mb_substr(get_the_excerpt(), 0, 100);//抜粋の文字数を設定
$title= mb_substr($post->post_title,0,40);//タイトルの文字数を設定
?>
<p>
<a href=<?php echo $title; ?>…の続きを読む</a>
</p>
<でもこっちカスタマイズ版>
<?php echo mb_substr(get_the_excerpt(), 0, 120); ?><a class="linkleft" href="<?php the_permalink(); ?>">続きを読む</a>]