質問編集履歴
1
エラーメッセージの解消方法について追加質問しています
title
CHANGED
File without changes
|
body
CHANGED
@@ -111,4 +111,116 @@
|
|
111
111
|
|
112
112
|
WordPress・PHPともに初心者のため、かなり回り道をしているかもしれません。
|
113
113
|
お手数をお掛けしますが、修正点を教えていただけますでしょうか。
|
114
|
+
以上、よろしくお願いいたします。
|
115
|
+
|
116
|
+
# 追加の質問(2020/11/27)
|
117
|
+
### 困っていること
|
118
|
+
「実現したいこと」は実現できましたが、下記エラーメッセージが出ます。
|
119
|
+
Warning: Creating default object from empty value in functions.php on line 583
|
120
|
+
エラーメッセージを解消するために必要な情報を教えていただきたく質問しています。
|
121
|
+
|
122
|
+
### ソースコード
|
123
|
+
date関連データでソートするために、下記、二つの独自関数を記述しました。
|
124
|
+
```PHP
|
125
|
+
function return_latest_id($cat_id=null) {
|
126
|
+
global $wpdb;
|
127
|
+
|
128
|
+
if(empty($cat_id)) {
|
129
|
+
// 最新記事idの取得
|
130
|
+
$row = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_modified DESC");
|
131
|
+
} else {
|
132
|
+
// カテゴリを指定した最新記事idの取得
|
133
|
+
$cat_id = intval($cat_id);
|
134
|
+
$row = $wpdb->get_row("SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->term_relationships r ON p.ID=r.object_id WHERE p.post_type = 'post' AND p.post_status = 'publish' AND r.term_taxonomy_id = '$cat_id' ORDER BY p.post_modified DESC");
|
135
|
+
}
|
136
|
+
return !empty( $row ) ? $row->ID : '0';
|
137
|
+
}
|
138
|
+
|
139
|
+
function sort_by_postdate($a, $b) {
|
140
|
+
// 2つのカテゴリの最新の投稿(newest_postメンバ)の日付の大小関係を返す
|
141
|
+
return ($a->newest_post->post_modified == $b->newest_post->post_modified) ? 0 :
|
142
|
+
($a->newest_post->post_modified < $b->newest_post->post_modified) ? 1 : -1;
|
143
|
+
}
|
144
|
+
```
|
145
|
+
|
146
|
+
以上の独自関数の後に、先日カテゴリIDの取得で躓いていた関数を大幅に追記しました。
|
147
|
+
```PHP
|
148
|
+
function special_nav_class($classes, $item,$args){
|
149
|
+
if ( $args->theme_location !== 'primary' )
|
150
|
+
return $classes;
|
151
|
+
if(($item->object == 'category') or ($item->object == 'page') or ($item->object == 'post' )){
|
152
|
+
if($item->object =='page'){
|
153
|
+
if ( ( $post = get_post( $item->object_id ) ) ) {
|
154
|
+
$now_date = date( 'U' );
|
155
|
+
$post_date = mysql2date( 'U', $post->post_modified ); // 投稿日なら $post->post_date
|
156
|
+
$diff_date = date( 'U', ( $now_date - $post_date )) / 86400;
|
157
|
+
if ( (int)$diff_date <= 30 )
|
158
|
+
$classes[] = 'new';
|
159
|
+
}
|
160
|
+
}
|
161
|
+
if(($item->object == 'category')){
|
162
|
+
if( $cat_id = $item->object_id ){
|
163
|
+
$cat_data = get_category( $cat_id );
|
164
|
+
$cat_data->category_parent;
|
165
|
+
$parent_id = $cat_data->category_parent;
|
166
|
+
if( empty($parent_id)==false ){
|
167
|
+
if( $post_id = return_latest_id( $cat_id ) ){
|
168
|
+
if ( ( $post = get_post( $post_id ) ) ) {
|
169
|
+
$now_date = date( 'U' );
|
170
|
+
$post_date = mysql2date( 'U', $post->post_modified ); // 投稿日なら $post->post_date
|
171
|
+
$diff_date = date( 'U', ( $now_date - $post_date )) / 86400;
|
172
|
+
if ( (int)$diff_date <= 30 )
|
173
|
+
$classes[] = 'new';
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
if( empty($parent_id)==true ){
|
178
|
+
// 投稿があるカテゴリをすべて読み込む
|
179
|
+
$cats = get_categories($parent_id);
|
180
|
+
// カテゴリの数を得る
|
181
|
+
$count = count($cats);
|
182
|
+
// カテゴリの数だけ繰り返す
|
183
|
+
for ($i = 0; $i < $count; $i++) {
|
184
|
+
// 各カテゴリの最も新しい投稿を読み込む
|
185
|
+
$where = array('category' => $cats[$i]->term_id, 'orderby' => 'post_modified', 'order' => 'desc', 'numberposts' => 1);
|
186
|
+
$newest_posts = get_posts($where);
|
187
|
+
// カテゴリのオブジェクトに「newest_post」というメンバを追加して、最新の投稿を代入する
|
188
|
+
$cats[$i]->newest_post = $newest_posts[0]; //エラーが出ている「line 583」はこの行です
|
189
|
+
// 各カテゴリの最も古い投稿を読み込む
|
190
|
+
$where['order'] = 'asc';
|
191
|
+
$oldest_posts = get_posts($where);
|
192
|
+
// カテゴリのオブジェクトに「oldest_post」というメンバを追加して、最新の投稿を代入する
|
193
|
+
$cats[$i]->oldest_post = $oldest_posts[0];
|
194
|
+
}
|
195
|
+
// カテゴリを、最新の投稿の日付が新しい順に並べ替える
|
196
|
+
usort($cats, 'sort_by_postdate');
|
197
|
+
// 結果を返す
|
198
|
+
$newest_child_cat_id = $cats[0]->object_id;
|
199
|
+
}
|
200
|
+
if( $post_id = return_latest_id( $newest_child_cat_id ) ){
|
201
|
+
if ( ( $post = get_post( $post_id ) ) ) {
|
202
|
+
$now_date = date( 'U' );
|
203
|
+
$post_date = mysql2date( 'U', $post->post_modified ); // 投稿日なら $post->post_date
|
204
|
+
$diff_date = date( 'U', ( $now_date - $post_date )) / 86400;
|
205
|
+
if ( (int)$diff_date <= 30 )
|
206
|
+
$classes[] = 'new';
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
}
|
211
|
+
return $classes;
|
212
|
+
}
|
213
|
+
}
|
214
|
+
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 3);
|
215
|
+
```
|
216
|
+
|
217
|
+
###試したこと
|
218
|
+
エラーメッセージで検索したところ、「new stdClassで初期化してあげると解消します。」という記事を見付けたのですが、
|
219
|
+
|
220
|
+
```PHP
|
221
|
+
$cats = new stdClass;
|
222
|
+
```
|
223
|
+
を追記してみたのですが、解消できませんでした。
|
224
|
+
|
225
|
+
恐れ入りますが、修正方法を教えていただけましたら幸いです。
|
114
226
|
以上、よろしくお願いいたします。
|