前提・実現したいこと
PHPの知識がなく、説明がうまくできてなかったらすみません。
ACFproを利用して、カスタム投稿タイプで作成した組合一覧(archive-union.php)から、詳細ページ(single-member.php)へリンクしたいです。
通常はarchive-〇〇.php、single-〇〇.phpの〇〇は同じ名前が入るかと思いますが、
一覧と詳細でそれぞれ異なるカスタム投稿タイプの情報を取得したいため、
カスタム投稿タイプはunion(組合)とmember(会員)の2種類あり、unionが親でmemberが子になる関係です。
一覧にはunionの内容となり、詳細にはmemberの内容ということで、memberとunionが混ざってしまうのですが、これを統一して組合の持つ会員へのリンクは可能なのでしょうか。
現状は、リンクを取得できていません。
archive-union.phpとsingle-union.phpも用意しており、組合名を読み込む関係上、
■カスタムフィールドの登録内容
カスタムフィールドのフィールドグループ
組合(archive-union.php)
├フィールド名:union_info
├フィールドタイプ:Group
├サブフィールド
│ └ 組合名:union_name(テキスト)
│ └ 組合住所:union_address(テキスト)
│ └ 組合TEL:union_tel(テキスト)
│ └ 組合HP:union_hp(テキスト)
│ └ 会員情報:member_info_list(繰り返しフィールド)
│ └会員名:member_name(テキスト)
│ └会員住所:member_address(テキスト)
│ └会員TEL:member_address(テキスト)
会員(single-member.php)
上記繰り返しフィールドの3件の情報を含む、
担当者名やその他多くの項目
該当のソースコード
カスタム投稿タイプの定義 functions.php
//組合(カテゴリ) function register_union_custom_post() { register_post_type( 'union', array( 'label' => '組合', 'description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'capability_type' => 'post', 'hierarchical' => true, 'rewrite' => true, 'query_var' => true, 'has_archive' => true, 'exclude_from_search' => true, 'supports' => array('title','comments','author'), 'taxonomies' => array('union_cat'), 'labels' => array( 'name' => '組合', 'singular_name' => '組合', 'menu_name' => '組合', 'add_new' => '新規追加', 'add_new_item' => '組合の新規追加', 'edit' => '編集', 'edit_item' => '組合の編集', 'new_item' => '新しい組合', 'view' => '表示', 'view_item' => '組合の組合', 'search_items' => '組合の検索', 'not_found' => '見つかりません', 'not_found_in_trash' => 'ゴミ箱にはありません。', 'parent' => '親', ) ) ); register_taxonomy( 'union_cat', 'union', array( 'hierarchical' => true, 'label' => '組合のカテゴリ', 'supports' => array('title', 'editor', 'thumbnail','page-attributes'), 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'singular_label' => '組合のカテゴリ' ) ); } add_action('init', 'register_union_custom_post'); //会員(カテゴリ) function register_member_custom_post() { register_post_type( 'member', array( 'label' => '会員', 'description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 4, 'capability_type' => 'post', 'hierarchical' => true, 'rewrite' => true, 'query_var' => true, 'has_archive' => true, 'exclude_from_search' => true, 'supports' => array('title','comments','author'), 'taxonomies' => array('member_cat'), 'labels' => array( 'name' => '会員', 'singular_name' => '会員', 'menu_name' => '会員', "all_items" => "すべての会員", 'add_new' => '新規追加', 'add_new_item' => '会員の新規追加', 'edit' => '編集', 'edit_item' => '会員の編集', 'new_item' => '新しい会員', 'view' => '表示', 'view_item' => '会員の会員', 'search_items' => '会員の検索', 'not_found' => '見つかりません', 'not_found_in_trash' => 'ゴミ箱にはありません。', 'parent' => '親(会員)', ) ) ); register_taxonomy( 'member_cat', 'member', array( 'hierarchical' => true, 'label' => '会員のカテゴリ', 'supports' => array('title', 'editor', 'thumbnail','page-attributes'), 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'singular_label' => '会員のカテゴリ' ) ); } add_action('init', 'register_member_custom_post');
archive-union.php
<?php $post_type = 'union'; $taxonomy_name = 'union_cat'; $args = array( 'orderby' => 'name', 'hierarchical' => false ); $taxonomys = get_terms( $taxonomy_name, $args); if(!is_wp_error($taxonomys) && count($taxonomys)): foreach($taxonomys as $taxonomy): $url = get_term_link($taxonomy->slug, $taxonomy_name); $tax_posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => 5, 'tax_query' => array( array( 'taxonomy' => $taxonomy_name, 'terms' => array( $taxonomy->slug ), 'field' => 'slug', 'include_children' => true, 'operator' => 'IN' ) ) )); if( $tax_posts ) { ?> <h3 id="<?php echo $taxonomy->slug ?>"><?php echo $taxonomy->name; ?></h3> <?php foreach($tax_posts as $tax_post): ?> <?php $group = get_field('union_info',$tax_post->ID); if($group){ ?> <div class="unBox"> <h4 class="bluBdrBg_ttl"><?php echo $group['union_name']; ?></h4> <div class="flex"> <p><?php echo $group['union_address']; ?><br /> <?php echo $group['union_tel']; ?></p> <p><a href="<?php echo $group['union_hp']; ?>" target="_blank" rel="noopener noreferrer">HP</a></p> </div> <?php $repeater = $group['member_info_list']; $r = '0'; $r_count = count($repeater); while($r_count > $r){ $repeater_content = $repeater[$r]; if(get_field('member_info_list')): ?> <dl> <dt><a href="<?php echo get_permalink($tax_post->ID); ?>"><?php echo $repeater_content['member_name']; ?></a></dt> <dd><?php echo $repeater_content['member_address']; ?></dd> <dd><a href="tel:<?php echo $repeater_content['member_tel']; ?>"><?php echo $repeater_content['member_tel']; ?></a></dd> </dl> <?php else: ?> <p>現在、会員は登録されていません</p> <?php endif; ?> <?php $r++; } ?> </div> <?php } ?> <?php endforeach; wp_reset_postdata(); ?> <?php if ($r == 0): ?> <p>現在、会員は登録されていません</p> <?php endif; ?> <?php } endforeach; endif; ?>
single-member.php
<?php the_title(); ?> <?php the_field('XXXX') ?> ・ ・ ・
※XXXXには担当者名などarchive-union.phpに含まれない情報が入ります。
試したこと
これで会員のカスタム投稿画面にunionという枠が追加されましたが、パーマリンクがunion/member/となるわけでもなく、ページが表示されることもありませんでした。
//親子関係
add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'member', 'normal'); }); add_action('add_meta_boxes', function() { add_meta_box('member-parent', 'union', 'member_attributes_meta_box', 'member', 'side', 'default'); }); function member_attributes_meta_box($post) { $post_type_object = get_post_type_object($post->post_type); if ( $post_type_object->hierarchical ) { $pages = wp_dropdown_pages(array('post_type' => 'union', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0)); if ( ! empty($pages) ) { echo $pages; } } }
親子関係を持たせたいのでpage-〇〇.phpでも試してみましたが、タクソノミーを使って親子関係ができるならarchiveとsingleで作成したいです。
よろしくお願いいたします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/18 08:30
2020/08/18 08:56
2020/08/20 02:20
2020/08/20 02:41
2020/08/20 03:48