WordPress

niestandardowe typy postów – Zmień dynamicznie wartość listy rozwijanej

  • 17 maja, 2021
  • 3 min read
niestandardowe typy postów – Zmień dynamicznie wartość listy rozwijanej


Mam formularz, w którym wyświetlam powiązany niestandardowy typ postu według identyfikatora nadrzędnego niestandardowego typu postu na liście pól wyboru, takiej jak obrazek.

więc kiedy zaznaczam opcję 4 opcji, chcę mieć listę terminów, które mają niestandardowe posty, które sprawdziłem na liście rozwijanej.

formularz jest zbudowany przy użyciu Elementora Pro, ale aby mieć listę postów i terminy taksonomii, robię to za pomocą kodu (wykonuję swoją funkcję i dołączam ją z krótkim kodem do funkcji.php).

/*
 * Display list of  relationship field
 *
*/

function list_formation_caces(){
echo "<div class="golistgo">";
$args = array(
    'post_type' => 'categories',
    'posts_per_page' => 20,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    $formations = get_posts(array(
        'post_type' => 'formations',
        'post__in' => array(16061),
        'meta_query' => array(
            array(
                'key' => 'categories', // name of custom field
                'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
        )
    ));
    if($formations){
        foreach ($formations as $categorie){
            $image = get_field('image_form');
            if($image){
                // Image variables.
                $url = $image['url'];
                $title = $image['title'];
                $alt = $image['alt'];
                $caption = $image['caption'];

                // Thumbnail size attributes.
                $size="thumbnail";
                $thumb = $image['sizes'][ $size ];
                $width = $image['sizes'][ $size . '-width' ];
                $height = $image['sizes'][ $size . '-height' ];
            }
            echo "<input type=\"checkbox\" class=\"btn-check\" name=\"Description\" value =\"". get_the_title() .
                "\" id=\"". get_the_ID()  ."\">";
            echo "<label class=\"options-form\" for="". get_the_ID() ."">";
                               echo "<div class=\"image-cat\">";
                                    echo get_the_post_thumbnail();
                               echo"</div>";
                               echo "<div class=\"body-cat\">";
                                    echo "<div  class=\"title-formation\">" ;
                                    echo "</div>";
                                    echo "<div  class=\"title-cat\">" ;
                                    echo the_title() . ":" . " <span  class=\"describe-cat\">" . get_field('type_machine') . "</span>";
                                    echo "</div>";
                               echo "</div>";
            echo "</label>";
        }
    }
endwhile;
wp_reset_postdata();
echo "</div>";
}

function shortcode_list_categ_caces(){
            return (list_formation_caces()) ;
}
add_shortcode('list_formation_caces', 'shortcode_list_categ_caces');

function shortcode_total_count_checked(){

return ("<div class="count-checkboxes-wrapper">" . "<h2 class="title-count">" . "Vous avez choisis" .
    "<span id='count-checked-checkboxes'> " . "  0" ."</span> catégorie(s)."." </h2>" .
    "</div>");
}
add_shortcode('count_checked', 'shortcode_total_count_checked');

function list_sessions_caces()
{
    $terms = get_terms( array(
        'taxonomy' => 'sessions',
        'hide_empty' => false,
    ) );
    echo'<label for="form-field-centre" class="elementor-field-label">Où se déroulera la formation ?</label>';
    echo '<div class="elementor-field elementor-select-wrapper ">';
    echo '<select name="CENTRE"  id="form-field-field_centre" class="elementor-field-textual elementor-size-sm">';
    echo '<option value="">Choisir une session de formation </option> ';
    foreach ($terms as $term) {
        echo '<option value="' . $term->name . '">' . $term->name . '</option>';
    }
    echo '</select>';
    echo'</div>';
}
function shortcode_list_sessions(){
    return (list_sessions_caces()) ;
}
add_shortcode('list_sessions_caces', 'shortcode_list_sessions');

Proszę o jakąkolwiek pomoc!
wprowadź tutaj opis obrazu

Warto przeczytać!  Wybierz listę rozwijaną ze zdarzeniem onChange i funkcją wyszukiwania — za pomocą Selectize


Źródło