wptouchのタイトル横にカテゴリー別で指定したサムネイルを表示させる

記事ごとにサムネイルをつけて表示させてもごちゃごちゃしそうだし、毎回つけるとなると面倒なので、
カテゴリーで判別させて、Aカテゴリーの記事ならAの画像、Bカテゴリーの記事ならBの画像、という風に表示させるようにしました。



1
2
3
4
5
6
7
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
<?php elseif (in_category('カテゴリーID')) : ?>
<a href="<?php the_permalink(); ?>"><img src="画像URL" title="<?php the_title_attribute(); ?>" /></a>
<?php else : ?>
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/image/common_image.jpg" title="<?php the_title_attribute(); ?>" /></a>
<?php endif; ?>

カテゴリーの数だけ3行目と4行目を増やしてください。
これをindex.phpの94行目
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
$total = '24'; $file_type = '.jpg'; 
 
// Change to the location of the folder containing the images 
$image_folder = '' . compat_get_plugin_url( 'wptouch' ) . '/themes/core/core-images/thumbs/'; 
$start = '1'; $random = mt_rand($start, $total); $image_name = $random . $file_type; 
 
if ($wptouch_settings['post-cal-thumb'] == 'post-thumbnails-random') {
echo "<img src="$image_folder/$image_name" alt="$image_name" />";
} else {
echo '<img src="' . compat_get_plugin_url( 'wptouch' ) . '/themes/core/core-images/thumbs/thumb-empty.jpg" alt="thumbnail" />';
}
?>

のところで上書きします。

さらに、個別記事のタイトル横でも表示させたい時はsingle.phpの6行目と7行目の間に
6
7
<div class="post">
<a class="sh2" href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e( "Permanent Link to ", "wptouch" ); ?><?php if (function_exists('the_title_attribute')) the_title_attribute(); else the_title(); ?>"><?php the_title(); ?></a>
1
2
3
4
5
6
7
8
9
10
11
<div class="wptouch-post-thumb-wrap">
<div class="wptouch-post-thumb">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
<?php elseif (in_category('カテゴリーID')) : ?>
<a href="<?php the_permalink(); ?>"><img src="画像URL" title="<?php the_title_attribute(); ?>" /></a>
<?php else : ?>
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/image/common_image.jpg" title="<?php the_title_attribute(); ?>" /></a>
<?php endif; ?>
</div>
</div>

を挿入します。
タイトルが短い(一行)と若干めりこんでますね。




リンクさせずにカテゴリー名だけを表示させるには
<?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?>


ちなみにプラグインを使わずに記事の読了時間を表示させるコードはこちら。
1分400文字換算で、秒は切り上げています。
<?php
$mycontent = $post->post_content;
$word = mb_strlen(strip_tags($mycontent));
$m = floor($word / 400) + 1 ;
$est = $m . '分' ;
?>読了時間: 約<?php echo $est; ?>



02/08 人間の愚かさを教えてくれる『フランケンシュタインの誘惑』が好きすぎる

はじめてこのサイトに来た方への案内所はこちら

類似記事

Copy&Paste


本文のコピペやスクショはしないでください