最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Taxonomie cpt with acf

programmeradmin2浏览0评论

I have a CPT 'formations'. In the page of one of my formations (single.php) there is a section "other articles". I would like from my editor of each "formation" page, select categories to show other "formation" desired. The list of my taxonomies is created. For the moment they are in the same archive.php appearance structure (they all appear) with a loop.

Conclude: on my "formation" page I want to be able to check the categories of articles that must appear in the block "other formation" on my page.

how should i place and or my taxonomy ACF loop for my post to appear. For moment i created the metabox with ACF field type: Taxonomy & name of my taxonomy "Thematiques"

My code: single-formation.php

<ul>
                <?php $args = array('post_type' => 'formation');?>
            <?php $loop = new WP_Query($args);?>
                <?php while ($loop -> have_posts()): $loop->the_post();?>
                <li>
                    <?php the_post_thumbnail('cpt_formation');?>
                    <div class="container-other">
                        <h4><?php the_title();?></h4>
                        <p><?php the_content();?>
                        </p>
                        <a href="<?php the_field('url_formation');?>">En savoir plus</a>
                    </div>  
                </li>
                <?php endwhile;?>
            </ul>

My code: functions.php

    function custom_formation(){
    $labels = array(
        'name' => 'Formation',
        'all_items' => 'Toutes les formations',  // affiché dans le sous menu
        'singular_name' => 'Formation',
        'add_new_item' => 'Ajouter une formation',
        'edit_item' => 'Modifier la formation',
        'menu_name' => 'Formation'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_in_rest' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor','thumbnail' ),
        'menu_position' => 5, 
        'menu_icon' => 'dashicons-format-status',
    );
    register_post_type( 'formation', $args );
    enter register_taxonomy(
        'thematique',
        'formation',
        array(
            'label' => 'Thématiques',
            'labels' => array(
            'name' => 'Thématiques',
            'singular_name' => 'Thématiques',
            'all_items' => 'Toutes les Thématiques',
            'edit_item' => 'Éditer la Thématique',
            'view_item' => 'Voir la Thématiques',
            'update_item' => 'Mettre à jour la Thématiques',
            'add_new_item' => 'Ajouter une Thématiques',
            'new_item_name' => 'Nouvelle Thématiques',
            'search_items' => 'Rechercher parmi les Thématiques',
            'popular_items' => 'Thématiques les plus utilisés',

        ),
            'hierarchical' => true,
            'show_in_rest' => true,
            'rewrite'=>false,
        )
    );
}
add_action('init','custom_formation');

I have a CPT 'formations'. In the page of one of my formations (single.php) there is a section "other articles". I would like from my editor of each "formation" page, select categories to show other "formation" desired. The list of my taxonomies is created. For the moment they are in the same archive.php appearance structure (they all appear) with a loop.

Conclude: on my "formation" page I want to be able to check the categories of articles that must appear in the block "other formation" on my page.

how should i place and or my taxonomy ACF loop for my post to appear. For moment i created the metabox with ACF field type: Taxonomy & name of my taxonomy "Thematiques"

My code: single-formation.php

<ul>
                <?php $args = array('post_type' => 'formation');?>
            <?php $loop = new WP_Query($args);?>
                <?php while ($loop -> have_posts()): $loop->the_post();?>
                <li>
                    <?php the_post_thumbnail('cpt_formation');?>
                    <div class="container-other">
                        <h4><?php the_title();?></h4>
                        <p><?php the_content();?>
                        </p>
                        <a href="<?php the_field('url_formation');?>">En savoir plus</a>
                    </div>  
                </li>
                <?php endwhile;?>
            </ul>

My code: functions.php

    function custom_formation(){
    $labels = array(
        'name' => 'Formation',
        'all_items' => 'Toutes les formations',  // affiché dans le sous menu
        'singular_name' => 'Formation',
        'add_new_item' => 'Ajouter une formation',
        'edit_item' => 'Modifier la formation',
        'menu_name' => 'Formation'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_in_rest' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor','thumbnail' ),
        'menu_position' => 5, 
        'menu_icon' => 'dashicons-format-status',
    );
    register_post_type( 'formation', $args );
    enter register_taxonomy(
        'thematique',
        'formation',
        array(
            'label' => 'Thématiques',
            'labels' => array(
            'name' => 'Thématiques',
            'singular_name' => 'Thématiques',
            'all_items' => 'Toutes les Thématiques',
            'edit_item' => 'Éditer la Thématique',
            'view_item' => 'Voir la Thématiques',
            'update_item' => 'Mettre à jour la Thématiques',
            'add_new_item' => 'Ajouter une Thématiques',
            'new_item_name' => 'Nouvelle Thématiques',
            'search_items' => 'Rechercher parmi les Thématiques',
            'popular_items' => 'Thématiques les plus utilisés',

        ),
            'hierarchical' => true,
            'show_in_rest' => true,
            'rewrite'=>false,
        )
    );
}
add_action('init','custom_formation');
Share Improve this question edited Mar 20, 2019 at 18:26 Tanmay Patel 8111 gold badge7 silver badges11 bronze badges asked Mar 20, 2019 at 16:27 t.webt.web 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You would want to use the value of the ACF Taxonomy Field as an additional argument of your WP_Query. So in 'single-formation.php' replace your WP_Query with something like this:

$term_ids = get_field('taxonomy_field_name'); // make sure the return value of your ACF Taxonomy field is set to "Term ID"

$args = array(
    'post_type' => 'formation',
    'tax_query' => array(
        array(
            'taxonomy' => 'thematique',
            'field'    => 'term_id',
            'terms'    => $term_ids,
        ),
    ),
);
$loop = new WP_Query( $args );

$loop should now contain any CPT 'formations' with the selected terms of the 'thematique' taxonomy.

发布评论

评论列表(0)

  1. 暂无评论