Q&A
前提・実現したいこと
固定ページ(アーカイブページ)で、管理者が自分で選択できるおすすめ記事を作りたいです。
・ カスタム投稿で構築しています。
・ Advanced Custom Fieldsを使っています。
・ Advanced Custom Fieldsの「関連」機能を使って表示したいです。
↓Advanced Custom Fields管理画面
↓固定ページ、社長ブログ編集ページ
右側に表示されてる記事だけ表示させたいです。
↓現在固定ページに表示されている内容
全て表示されてしまっています。
発生している問題・エラーメッセージ
エラーは現状出ていません。
該当のソースコード
php
1 2//function.php 3 4add_action( 'init', 'create_post_type_ceo' ); 5function create_post_type_ceo() { 6register_post_type( 'ceo', //カスタム投稿タイプ名 7array( 8'labels' => array( 9'name' => __( '社長のブログ' ), 10'all_items' => __( '社長のブログ一覧' ), 11 12), 13'public' => true, 14'has_archive' => true, //アーカイブページを持つ 15'menu_position' =>5, //管理画面のメニュー順位 16'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments' ), 17) 18); 19/* カテゴリーの設定 */ 20register_taxonomy( 21'ceo_category', //カテゴリーの名前 22'ceo', //使うカスタム投稿タイプ名 23array( 24'hierarchical' => true, //trueで親子関係使用 25'update_count_callback' => '_update_post_term_count', 26'label' => 'カテゴリー', 27'singular_label' => 'カテゴリー', 28'public' => true, 29'show_ui' => true 30) 31); 32/* タグを設定 */ 33register_taxonomy( 34'ceo_tag', //タグの名前 35'ceo', //使うカスタム投稿タイプ名 36array( 37'hierarchical' => false, 38'update_count_callback' => '_update_post_term_count', 39'label' => 'タグ', 40'singular_label' => 'タグ', 41'public' => true, 42'show_ui' => true 43) 44); 45} 46
php
1 2<?php 3 $related_posts = get_field( 'test' ); // 関連記事IDの取得 4 $args = array( 5 'post_type' => 'ceo', 6 'posts_per_page' => -1, 7 'post__in' => $related_posts, //queryに含む投稿IDを指定 8 'orderby' => 'post__in' //表示順(配列に入っている順) 9 ); 10 ?> 11 <?php $query = new WP_Query( $args ); ?> 12 <?php if($query -> have_posts()): ?></p> 13 <h2>関連記事の出力(オプションページ)</h2> 14 <ul> 15 <?php while($query -> have_posts()): $query->the_post();?></p> 16 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 17 <p> <?php endwhile; ?> 18 </ul> 19 <p><?php endif; ?><br /> 20 <?php wp_reset_postdata(); ?> 21
試したこと
php
1<?php 2 $page_id = get_page_by_path('ceo'); 3 $page_id = $page_id->ID; 4 echo get_post_meta($page_id, 'test', true); 5?>
どうしても表示されず、プラグインの問題かなと思いましたが、このコードでテキストを試しに表示してみたところ、固定ページで正常に表示されました。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2019/08/16 04:16
2019/08/16 04:30
2019/08/16 04:46
2019/08/16 05:04
2019/08/16 05:51
2019/08/16 11:25
2019/08/16 13:48