前提・実現したいこと
Wordpressで、カテゴリーアーカイブと、個別記事で
現在いるカテゴリーの最新記事5件をサイドバーに表示させたいです。
トップページは必要ありません。
これ↓が一番イメージに近いので、こちらを試してみました。
(今回の場合サムネイルは必要ないですが。)
【WordPress】カテゴリを判別して、最新記事をサムネイル付きで一覧表示する方法
https://kumiko-jp.com/archives/9366.html
functions.phpに書き込むことで、現在のカテゴリを判別して
最新記事を表示できるウィジェットを生成できるものです。
該当のソースコード
/*************************************** サイドバーに最新記事を表示するWordPressウィジェット ***************************************/ class MyWidgetRecentPost extends WP_Widget { function MyWidgetRecentPost() { parent::WP_Widget(false, $name = 'RecentPost'); } function widget($args, $instance) { /* 一度に表示する記事数 */ $num = 10; extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); $body = apply_filters( 'widget_body', $instance['body'] ); /* 個別記事の時は、カテゴリ別の最新記事を表示する */ if (is_single()){ $cat = get_the_category(); $cat = $cat[0]; query_posts($query_string . '&category_name='.$cat->slug.'&posts_per_page='.$num.'&offset=0'); echo '<h3 class="widget-title">「'.$cat->name.'」の最新記事</h3>'; /* カテゴリ別アーカイブの時は、カテゴリ別の最新記事を表示する */ }else if(is_category()){ $cat = get_query_var('cat'); $cat = get_category($cat); query_posts($query_string . '&category_name='.$cat->slug.'&posts_per_page='.$num.'&offset=0'); echo '<h3 class="widget-title">「'.$cat->name.'」の最新記事</h3>'; /* それ以外の時は、全カテゴリの最新記事を表示する */ }else{ query_posts('posts_per_page='.$num.'&offset=0'); echo '<h3 class="widget-title">最新記事</h3>'; } if(have_posts()):while(have_posts()):the_post();?> <div style="float:left;width:135px;margin:0 10px 5px 0;text-align:center;"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(array(135,135)); ?></a></div> <div style="float:left;width:135px;margin-bottom:5px;"><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></div><div style="clear:both;"></div> <?php endwhile; endif; wp_reset_query(); } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['body'] = trim($new_instance['body']); return $instance; } function form($instance) { $title = esc_attr($instance['title']); $body = esc_attr($instance['body']); } } add_action('widgets_init', create_function('', 'return register_widget("MyWidgetRecentPost");')); ?>
発生している問題・エラーメッセージ
が、こちらは2013年の古い記事のようで、
このままfunctions.phpに追加したところ、
Notice:MyWidgetRecentPostで呼び出された WP_Widgetのコンストラクターメソッドはバージョン4.3.0から非推奨になっています!代わりに
_construct()
を使ってください。
というエラーが出たので、
修正前
class MyWidgetRecentPost extends WP_Widget { function MyWidgetRecentPost() { parent::WP_Widget(false, $name = 'RecentPost'); }
修正後
class MyWidgetRecentPost extends WP_Widget { function MyWidgetRecentPost() { parent::__construct(false, $name = 'RecentPost'); }
試したこと
WP_Widgetを__constructに変更しました。
発生している問題・エラーメッセージ2
その後、__constructのエラーメッセージは出なくなりましたが、
ウィジェットを入れると、編集画面のウィジェット部分に
Notice: Undefined index: title in /home/.../functions.php on line 334
Notice: Undefined index: body in /home/.../functions.php on line 335
が出てきてしまうのと、
※該当部分
function form($instance) {
$title = esc_attr($instance['title']);
$body = esc_attr($instance['body']);
}
サイトにエラーが
Notice: Undefined variable:
query_string in
/home/.../functions.php on line 312
と出てくるようになってしまいました。
該当部分は
個別記事の時はこのif以下のquery_postsの行、
カテゴリ別アーカイブの時はカテゴリ別のelse if以下の
やはりquery_postsの行です。
/* 個別記事の時は、カテゴリ別の最新記事を表示する */ if (is_single()){ $cat = get_the_category(); $cat = $cat[0]; query_posts($query_string . '&category_name='.$cat->slug.'&posts_per_page='.$num.'&offset=0'); echo '<h3 class="widget-title">「'.$cat->name.'」の最新記事</h3>'; /* カテゴリ別アーカイブの時は、カテゴリ別の最新記事を表示する */ }else if(is_category()){ $cat = get_query_var('cat'); $cat = get_category($cat); query_posts($query_string . '&category_name='.$cat->slug.'&posts_per_page='.$num.'&offset=0'); echo '<h3 class="widget-title">「'.$cat->name.'」の最新記事</h3>';
エラーは出るものの一応、カテゴリーごとの最新記事は表示されます。
でもできれば、エラーが出ず正しく記述したいと思っています。
お力を貸していただけるとうれしいです。
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/10/18 09:40