前提・実現したいこと
WordPressのテンプレート「LION Media」を使ってブログを作成中です。
LION Media デモサイト → http://lionmedia.fit-jp.com/
こちらのサイドバーにある「最近の投稿」などの項目をトップページでは非表示に、記事ページでは表示するよう変更したいです。
WordPressもPHPも初心者なので、分かる方がいましたら教えていただけると嬉しいです。よろしくお願い致します。
該当のソースコード
↓↓↓新着記事ウィジットのPHP(親テーマ内のfunctions.php)↓↓↓
php
1////////////////////////////////////////////////// 2//新着記事ウィジェットアイテムのフォーマット変更(サムネイル追加) 3////////////////////////////////////////////////// 4class fit_recent_posts_widget extends wp_widget_recent_posts { 5 function widget($args, $instance) { 6 extract( $args ); 7 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base); 8 9 if( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) 10 $number = 10; 11 12 $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) ); 13 if( $r->have_posts() ) : 14 15 echo $before_widget; 16 if( $title ) echo $before_title . $title . $after_title; ?> 17 <ol class="imgListWidget"> 18 <?php while( $r->have_posts() ) : $r->the_post(); ?> 19 <li class="imgListWidget__item"> 20 <a class="imgListWidget__borderBox" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><span> 21 <?php if ( has_post_thumbnail()): ?> 22 <?php the_post_thumbnail('thumbnail'); ?> 23 <?php else: ?> 24 <img src="<?php echo get_template_directory_uri(); ?>/img/img_no_thumbnail.gif" alt="NO IMAGE"> 25 <?php endif; ?> 26 </span></a> 27 <h3 class="imgListWidget__title"> 28 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 29 <?php if( !empty( $instance['show_date'] )): ?><span class="post-date"><?php the_time('Y.m.d'); ?></span><?php endif; ?> 30 </h3> 31 </li> 32 <?php endwhile; ?> 33 </ol> 34 <?php 35 echo $after_widget; 36 37 wp_reset_postdata(); 38 endif; 39 } 40} 41function fit_recent_widget_registration() { 42 unregister_widget('wp_widget_recent_posts'); register_widget('fit_recent_posts_widget'); 43} 44add_action('widgets_init', 'fit_recent_widget_registration');
試したこと
参考にしている記事 → https://www.studiobrain.net/10958
こちらの「★トップページ(ホームとフロントページ)にだけ表示したくない場合」のコードの中に上記のコードをそのまま入れてみましたがエラーが起こります。
php
1<?php if ( !is_home() && !is_front_page() ) : ?> 2ここに上記の「新着記事ウィジェットアイテムのフォーマット変更(サムネイル追加)」の中身をコピー 3<?php endif; ?>
補足情報(FW/ツールのバージョンなど)
LION Media デモサイト → http://lionmedia.fit-jp.com/
参考にしている記事 → https://www.studiobrain.net/10958
追記
CHERRYさんの教えていただいた通りにコードを変えてみましたができませんでした。
php
1function fit_recent_widget_registration() { 2 unregister_widget('wp_widget_recent_posts'); //ここを↓に持ってきてもダメでした 3 if ( is_single() ){ 4 register_widget('fit_recent_posts_widget'); 5 } 6}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。