質問編集履歴

1

エラーメッセージの解消方法について追加質問しています

2020/11/27 09:40

投稿

hollyhock
hollyhock

スコア1

test CHANGED
File without changes
test CHANGED
@@ -225,3 +225,227 @@
225
225
  お手数をお掛けしますが、修正点を教えていただけますでしょうか。
226
226
 
227
227
  以上、よろしくお願いいたします。
228
+
229
+
230
+
231
+ # 追加の質問(2020/11/27)
232
+
233
+ ### 困っていること
234
+
235
+ 「実現したいこと」は実現できましたが、下記エラーメッセージが出ます。
236
+
237
+ Warning: Creating default object from empty value in functions.php on line 583
238
+
239
+ エラーメッセージを解消するために必要な情報を教えていただきたく質問しています。
240
+
241
+
242
+
243
+ ### ソースコード
244
+
245
+ date関連データでソートするために、下記、二つの独自関数を記述しました。
246
+
247
+ ```PHP
248
+
249
+ function return_latest_id($cat_id=null) {
250
+
251
+ global $wpdb;
252
+
253
+
254
+
255
+ if(empty($cat_id)) {
256
+
257
+ // 最新記事idの取得
258
+
259
+ $row = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_modified DESC");
260
+
261
+ } else {
262
+
263
+ // カテゴリを指定した最新記事idの取得
264
+
265
+ $cat_id = intval($cat_id);
266
+
267
+ $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");
268
+
269
+ }
270
+
271
+ return !empty( $row ) ? $row->ID : '0';
272
+
273
+ }
274
+
275
+
276
+
277
+ function sort_by_postdate($a, $b) {
278
+
279
+ // 2つのカテゴリの最新の投稿(newest_postメンバ)の日付の大小関係を返す
280
+
281
+ return ($a->newest_post->post_modified == $b->newest_post->post_modified) ? 0 :
282
+
283
+ ($a->newest_post->post_modified < $b->newest_post->post_modified) ? 1 : -1;
284
+
285
+ }
286
+
287
+ ```
288
+
289
+
290
+
291
+ 以上の独自関数の後に、先日カテゴリIDの取得で躓いていた関数を大幅に追記しました。
292
+
293
+ ```PHP
294
+
295
+ function special_nav_class($classes, $item,$args){
296
+
297
+ if ( $args->theme_location !== 'primary' )
298
+
299
+ return $classes;
300
+
301
+ if(($item->object == 'category') or ($item->object == 'page') or ($item->object == 'post' )){
302
+
303
+ if($item->object =='page'){
304
+
305
+ if ( ( $post = get_post( $item->object_id ) ) ) {
306
+
307
+ $now_date = date( 'U' );
308
+
309
+ $post_date = mysql2date( 'U', $post->post_modified ); // 投稿日なら $post->post_date
310
+
311
+ $diff_date = date( 'U', ( $now_date - $post_date )) / 86400;
312
+
313
+ if ( (int)$diff_date <= 30 )
314
+
315
+ $classes[] = 'new';
316
+
317
+ }
318
+
319
+ }
320
+
321
+ if(($item->object == 'category')){
322
+
323
+ if( $cat_id = $item->object_id ){
324
+
325
+ $cat_data = get_category( $cat_id );
326
+
327
+ $cat_data->category_parent;
328
+
329
+ $parent_id = $cat_data->category_parent;
330
+
331
+ if( empty($parent_id)==false ){
332
+
333
+ if( $post_id = return_latest_id( $cat_id ) ){
334
+
335
+ if ( ( $post = get_post( $post_id ) ) ) {
336
+
337
+ $now_date = date( 'U' );
338
+
339
+ $post_date = mysql2date( 'U', $post->post_modified ); // 投稿日なら $post->post_date
340
+
341
+ $diff_date = date( 'U', ( $now_date - $post_date )) / 86400;
342
+
343
+ if ( (int)$diff_date <= 30 )
344
+
345
+ $classes[] = 'new';
346
+
347
+ }
348
+
349
+ }
350
+
351
+ }
352
+
353
+ if( empty($parent_id)==true ){
354
+
355
+ // 投稿があるカテゴリをすべて読み込む
356
+
357
+ $cats = get_categories($parent_id);
358
+
359
+ // カテゴリの数を得る
360
+
361
+ $count = count($cats);
362
+
363
+ // カテゴリの数だけ繰り返す
364
+
365
+ for ($i = 0; $i < $count; $i++) {
366
+
367
+ // 各カテゴリの最も新しい投稿を読み込む
368
+
369
+ $where = array('category' => $cats[$i]->term_id, 'orderby' => 'post_modified', 'order' => 'desc', 'numberposts' => 1);
370
+
371
+ $newest_posts = get_posts($where);
372
+
373
+ // カテゴリのオブジェクトに「newest_post」というメンバを追加して、最新の投稿を代入する
374
+
375
+ $cats[$i]->newest_post = $newest_posts[0];  //エラーが出ている「line 583」はこの行です
376
+
377
+ // 各カテゴリの最も古い投稿を読み込む
378
+
379
+ $where['order'] = 'asc';
380
+
381
+ $oldest_posts = get_posts($where);
382
+
383
+ // カテゴリのオブジェクトに「oldest_post」というメンバを追加して、最新の投稿を代入する
384
+
385
+ $cats[$i]->oldest_post = $oldest_posts[0];
386
+
387
+ }
388
+
389
+ // カテゴリを、最新の投稿の日付が新しい順に並べ替える
390
+
391
+ usort($cats, 'sort_by_postdate');
392
+
393
+ // 結果を返す
394
+
395
+ $newest_child_cat_id = $cats[0]->object_id;
396
+
397
+ }
398
+
399
+ if( $post_id = return_latest_id( $newest_child_cat_id ) ){
400
+
401
+ if ( ( $post = get_post( $post_id ) ) ) {
402
+
403
+ $now_date = date( 'U' );
404
+
405
+ $post_date = mysql2date( 'U', $post->post_modified ); // 投稿日なら $post->post_date
406
+
407
+ $diff_date = date( 'U', ( $now_date - $post_date )) / 86400;
408
+
409
+ if ( (int)$diff_date <= 30 )
410
+
411
+ $classes[] = 'new';
412
+
413
+ }
414
+
415
+ }
416
+
417
+ }
418
+
419
+ }
420
+
421
+ return $classes;
422
+
423
+ }
424
+
425
+ }
426
+
427
+ add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 3);
428
+
429
+ ```
430
+
431
+
432
+
433
+ ###試したこと
434
+
435
+ エラーメッセージで検索したところ、「new stdClassで初期化してあげると解消します。」という記事を見付けたのですが、
436
+
437
+
438
+
439
+ ```PHP
440
+
441
+ $cats = new stdClass;
442
+
443
+ ```
444
+
445
+ を追記してみたのですが、解消できませんでした。
446
+
447
+
448
+
449
+ 恐れ入りますが、修正方法を教えていただけましたら幸いです。
450
+
451
+ 以上、よろしくお願いいたします。