今回の記事では、WordPressテーマ:賢威7サイトの個別記事や固定ページに表示される関連記事をカスタマイズする方法を紹介したいと思います。
賢威7をインストールしたばかりの状態ではサムネイルが記事タイトルと一緒に表示されますが、このサムネイルを非表示にしていきます。画像が無くなる分かなりコンパクトになるので、サムネイルはいらない!というサイトや、バズ部のようにタイトルで勝負!というサイトにオススメな方法です。
カスタマイズ前
↓ インストールしたばかりの状態では、サムネイルとタイトルが5件ずつ表示されます。
カスタマイズ後
↓ サムネイルが非表示になり、タイトルのみに。
1. functions.php を編集
賢威7では、親テーマの functions.php で関連記事のサムネイルを管理しているので、functions.php の803行目付近にある function get_relation_keni()
を編集していきます。
↓ 【編集前】
PHP
コピーする
function get_relation_keni() {$relation = "";$link_count = 0;if(!is_home() && !is_front_page()) {$relation_data = get_post_meta(get_the_ID(),'relation', true);if ($relation_data != "") {$relation_lies = explode("n", $relation_data); // 改行で区切るforeach ($relation_lies as $no => $relation_line) {$line_array = explode("t", $relation_line);// タブで区切るif (trim($line_array[0]) != "") {$image = ($line_array[3] != "") ? "<div class="related-thumb"><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."" target="_blank"><img src="".$line_array[3]."" class="relation-image"></a></div>" : "";if (isset($line_array[2]) && $line_array[2] == "y") {$relation .= "<li>".$image."<p><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."" target="_blank">".esc_attr($line_array[0])."</a></p></li>n";} else {$relation .= "<li>".$image."<p><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."">".esc_attr($line_array[0])."</a></p></li>n";}$link_count++;}}}}if ($link_count < 5) {// カテゴリから取得する$category_relation = get_post_meta(get_the_ID(), 'category_relation', true);if (isset($category_relation) && $category_relation == "y") {$target_category = get_the_category(get_the_ID());if (isset($target_category) && is_array($target_category) && count($target_category) > 0) {foreach ($target_category as $cat_val) {$cat_list[] = $cat_val->cat_ID;}}if (isset($cat_list) && count($cat_list) > 0) {$args = array( 'posts_per_page' => 5,'category' => implode(",", $cat_list), 'orderby' => 'rand', 'exclude' => get_the_ID());$rand_posts = get_posts( $args );if (count($rand_posts) > 0) {foreach ($rand_posts as $cat_posts) {if ($link_count < 5) {$image = (get_the_post_thumbnail($cat_posts->ID)) ? "<div class="related-thumb"><a href="".get_permalink($cat_posts->ID)."" title="".esc_attr($cat_posts->post_title)."" target="_blank">".get_the_post_thumbnail($cat_posts->ID, 'ss_thumb', array('class' => 'relation-image', 'alt' => esc_attr($cat_posts->post_title)))."</a></div>" : "";$relation .= "<li>".$image."<p><a href="".get_permalink($cat_posts->ID)."" title="".esc_attr($cat_posts->post_title)."">".esc_attr($cat_posts->post_title)."</a></p></li>n";$link_count++;}}}}}}if ($link_count < 5) {// タグから取得する$tag_relation = get_post_meta(get_the_ID(), 'tag_relation', true);if (isset($tag_relation) && $tag_relation == "y") {$target_tags= get_the_tags();if (isset($target_tags) && is_array($target_tags) && count($target_tags) > 0) {foreach ($target_tags as $tag_val) {$tag_list[] = $tag_val->term_id;}}if (isset($tag_list) && count($tag_list) > 0) {query_posts(array('tag__in' => $tag_list, 'showposts' => 5, 'post__not_in' => array(get_the_ID())));if (have_posts()) : while(have_posts()) : the_post();if ($link_count < 5) {$image = (get_the_post_thumbnail(get_the_ID())) ? "<div class="related-thumb"><a href="".get_permalink(get_the_ID())."" title="".esc_html(get_the_title())."" target="_blank">".get_the_post_thumbnail(get_the_ID(), 'ss_thumb', array('class' => 'relation-image', 'alt' => esc_html(get_the_title())))."</a></div>" : "";$relation .= "<li>".$image."<a href="".get_permalink()."" title="".esc_html(get_the_title())."">".esc_html(get_the_title())."</a></p></li>n";$link_count++;}endwhile;endif;}}wp_reset_query();}return (!empty($relation)) ? "<div class="contents related-articles related-articles-thumbs01">n<h2 id="keni-relatedposts">".sprintf( __('Related Posts','keni'))."</h2>n<ul class="keni-relatedposts-list">n".$relation."</ul>n</div>n" : "";}
↓ 【編集後】
PHP
コピーする
function get_relation_keni() {$relation = "";$link_count = 0;if(!is_home() && !is_front_page()) {$relation_data = get_post_meta(get_the_ID(),'relation', true);if ($relation_data != "") {$relation_lies = explode("n", $relation_data); // 改行で区切るforeach ($relation_lies as $no => $relation_line) {$line_array = explode("t", $relation_line);// タブで区切るif (trim($line_array[0]) != "") {// $image = ($line_array[3] != "") ? "<div class="related-thumb"><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."" target="_blank"><img src="".$line_array[3]."" class="relation-image"></a></div>" : "";if (isset($line_array[2]) && $line_array[2] == "y") {// $relation .= "<li>".$image."<p><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."" target="_blank">".esc_attr($line_array[0])."</a></p></li>n";$relation .= "<li><p><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."" target="_blank">".esc_attr($line_array[0])."</a></p></li>n";} else {// $relation .= "<li>".$image."<p><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."">".esc_attr($line_array[0])."</a></p></li>n";$relation .= "<li><p><a href="".$line_array[1]."" title="".esc_attr($line_array[0])."">".esc_attr($line_array[0])."</a></p></li>n";}$link_count++;}}}}if ($link_count < 5) {// カテゴリから取得する$category_relation = get_post_meta(get_the_ID(), 'category_relation', true);if (isset($category_relation) && $category_relation == "y") {$target_category = get_the_category(get_the_ID());if (isset($target_category) && is_array($target_category) && count($target_category) > 0) {foreach ($target_category as $cat_val) {$cat_list[] = $cat_val->cat_ID;}}if (isset($cat_list) && count($cat_list) > 0) {$args = array( 'posts_per_page' => 5,'category' => implode(",", $cat_list), 'orderby' => 'rand', 'exclude' => get_the_ID());$rand_posts = get_posts( $args );if (count($rand_posts) > 0) {foreach ($rand_posts as $cat_posts) {if ($link_count < 5) {// $image = (get_the_post_thumbnail($cat_posts->ID)) ? "<div class="related-thumb"><a href="".get_permalink($cat_posts->ID)."" title="".esc_attr($cat_posts->post_title)."" target="_blank">".get_the_post_thumbnail($cat_posts->ID, 'ss_thumb', array('class' => 'relation-image', 'alt' => esc_attr($cat_posts->post_title)))."</a></div>" : "";// $relation .= "<li>".$image."<p><a href="".get_permalink($cat_posts->ID)."" title="".esc_attr($cat_posts->post_title)."">".esc_attr($cat_posts->post_title)."</a></p></li>n";$relation .= "<li><p><a href="".get_permalink($cat_posts->ID)."" title="".esc_attr($cat_posts->post_title)."">".esc_attr($cat_posts->post_title)."</a></p></li>n";$link_count++;}}}}}}if ($link_count < 5) {// タグから取得する$tag_relation = get_post_meta(get_the_ID(), 'tag_relation', true);if (isset($tag_relation) && $tag_relation == "y") {$target_tags= get_the_tags();if (isset($target_tags) && is_array($target_tags) && count($target_tags) > 0) {foreach ($target_tags as $tag_val) {$tag_list[] = $tag_val->term_id;}}if (isset($tag_list) && count($tag_list) > 0) {query_posts(array('tag__in' => $tag_list, 'showposts' => 5, 'post__not_in' => array(get_the_ID())));if (have_posts()) : while(have_posts()) : the_post();if ($link_count < 5) {// $image = (get_the_post_thumbnail(get_the_ID())) ? "<div class="related-thumb"><a href="".get_permalink(get_the_ID())."" title="".esc_html(get_the_title())."" target="_blank">".get_the_post_thumbnail(get_the_ID(), 'ss_thumb', array('class' => 'relation-image', 'alt' => esc_html(get_the_title())))."</a></div>" : "";// $relation .= "<li>".$image."<a href="".get_permalink()."" title="".esc_html(get_the_title())."">".esc_html(get_the_title())."</a></p></li>n";$relation .= "<li><a href="".get_permalink()."" title="".esc_html(get_the_title())."">".esc_html(get_the_title())."</a></p></li>n";$link_count++;}endwhile;endif;}}wp_reset_query();}return (!empty($relation)) ? "<div class="contents related-articles related-articles-thumbs01">n<h2 id="keni-relatedposts">".sprintf( __('Related Posts','keni'))."</h2>n<ul class="keni-relatedposts-list">n".$relation."</ul>n</div>n" : "";}
↓ コード編集後
ちなみに上では以下の編集をしています。
$relation=...
にある".$image."
を削除。$image = ...
をコメントアウト。
※ コメントアウトしてある $relation=...
のコードが、編集前の$relation=...
です。
今回のカスタマイズは以上です。
最後まで閲覧ありがとうございました。