実現したいこと
- カスタム投稿の投稿ページ下部にナビゲーションを表示したい
- ナビゲーションには同じタームを持つ前後の投稿のリンクを設置したい
- 各投稿にはタームを1つだけ設定している。それを取得し、対応するタームのリンクを生成したい
解決方法分かる方がいましたらご教示お願いいたします。
発生している問題・分からないこと
$prev_post = get_previous_post(true, '', $term->slug);
$next_post = get_next_post(true, '', $term->slug);
上記のslugが読み取れておらず、aタグが表示されない
エラーメッセージ
error
1Attempt to read property "slug" on array in
該当のソースコード
php
1 <div class="nav"> 2 <?php 3 $term = get_the_terms($post->ID, 'taxname'); 4 $prev_post = get_previous_post(true, '', $term[0]->slug); 5 $next_post = get_next_post(true, '', $term[0]->slug); 6 ?> 7 <div class="prev"> 8 <?php if($prev_post): ?> 9 <a href="<?php echo get_permalink($prev_post->ID); ?>" class="link_b"> 10 <img src="ディレクトリ" alt="矢印"> 11 前の投稿へ 12 </a> 13 <?php endif; ?> 14 </div> 15 <div class="next"> 16 <?php if($next_post): ?> 17 <a href="<?php echo get_permalink($next_post->ID); ?>" class="link"> 18 次の投稿へ 19 <img src="ディレクトリ" alt="矢印"> 20 </a> 21 <?php endif; ?> 22 </div> 23 </div>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
コードを書きの通りに変更したが、いずれもエラーメッセージが表示されてしまう
◆ケース1 $termの配列順を指定した
$term = get_the_terms($post->ID, 'taxname');
$prev_post = get_previous_post(true, '', $term[0]->slug);
$next_post = get_next_post(true, '', $term[0]->slug);
◆ケース2 foreachで取得した情報を分割した
$terms = get_the_terms($post->ID, 'taxname');
foreach ($terms as $term) :
$prev_post = get_previous_post(true, '', $term->slug);
$next_post = get_next_post(true, '', $term->slug);
endforeach;
補足
特になし

回答1件
あなたの回答
tips
プレビュー