WordPressテンプレートその2

投稿記事に設定したタグを表示

<?php
$posttags1 = get_the_tags();
if ( $posttags1 ) {
  foreach ( $posttags1 as $tag1 ) {
    echo $tag1->name; 
    echo ' '; 

  }
}
?>

投稿記事に設定したタグのスラッグを表示

<?php $posttags = get_the_tags(); if ( $posttags ) { foreach ( $posttags as $tag ) { echo $tag->slug . ' ';} } ?>

投稿記事のアイキャッチ画像機能を有効にする

投稿記事のアイキャッチ画像機能を有効にする場合「functions.php」に以下を記述する

// すべてのアイキャッチ画像の有効化
add_theme_support('post-thumbnails');

カテゴリー毎にアイキャッチ画像を設定する

//	カテゴリーごとのアイキャッチ画像の設定
function default_category_eyecatch() {

global $post;
$user_eyecatch = has_post_thumbnail($post->ID);            
if (!$user_eyecatch)  {
$preset_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($preset_image) {				  
foreach ($preset_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment);
}
}
else if ( in_category('6') ) {
set_post_thumbnail($post->ID, '27');
}
else if ( in_category('カテゴリーID') ) {
set_post_thumbnail($post->ID, '画像ID');
}
else {
set_post_thumbnail($post->ID, '画像ID');
wp_reset_postdata();
}					   
}                       
}
add_action('the_post', 'default_category_eyecatch');

?>