0

I am trying to make a dynamic carousel with CPT. My CPT works well and I added some articles but I can not display it. I am doing it this way:

<section id="carousel">
    <div class="row mb-2"><!-- ROW -->
        <div class="col-md-12">
            <h3 class="mb-3">Mes sites préférés</h3>
        </div>
    </div><!-- ROW -->

    <div class="row mb-5">
        <!-- CAROUSEL BOOTSTRAP -->
        <?php
        $args = array(
            'post_type' => 'carousel',
            'post_status'=>'publish',
            'category_name' => 'realisations',
            'posts_per_page'=>-1
        );
        $the_query = new WP_Query ( $args ); 
        ?>

        <div class="col-lg-12 colCarousel">
            <div id="ExampleCarouselID" class="carousel slide shadow" data-ride="carousel" data-interval="7000">
                <a class="carousel-control-prev" href="#ExampleCarouselID" role="button" data-slide="prev">
                    <span class="arrowCarousel ti-angle-double-left" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="carousel-control-next" href="#ExampleCarouselID" role="button" data-slide="next">
                    <span class="arrowCarousel ti-angle-double-right" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
                <?php rewind_posts(); ?>
                <div class="carousel-inner"> 
                    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
                        $thumbnail_id   = get_post_thumbnail_id();
                        $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attatchment_image_alt', true );
                        ?>
                        <div class="carousel-item <?php if ($the_query->current_post == 0) : ?> active"<?php endif; ?>>
                            <?php if (has_post_thumbnail()) : ?>
                                <a href="" title="<?php the_title_attribute(); ?>">
                                    <?php the_post_thumbnail('taille-carousel', array('class' => 'img-fluid')); ?><!-- Dans functions.php -->
                                </a>
                            <?php endif; ?>
                            <div class="carousel-caption align-middle">
                                <h3><?php the_title();?></h3>
                                <a class="btnCarousel" href="<?php echo get_post_meta( get_the_ID(), 'url', true ); ?>" target="_blank" rel="noopener noreferrer"><span>Visitez le site <i class="icofont-arrow-right"></i></span></a>
                            </div>
                        </div><!-- /.carousel-item -->
                        <!-- end second loop -->
                    <?php endwhile; endif; ?>
                </div><!-- /.carousel-inner -->
            </div><!-- /.carousel-slide -->
        </div><!-- COL -->
    </div><!-- ROW -->
</section>

My CPT:

    function cpt_carousel() {
    $labels = array(
        'name'                => _x( 'Slides', 'Post Type General Name'),
        'singular_name'       => _x( 'Slide', 'Post Type Singular Name'),
        'menu_name'           => __( 'Carousel'),
        'all_items'           => __( 'Tous les slides'),
        'view_item'           => __( 'Voir les slides'),
        'add_new_item'        => __( 'Ajouter un slide'),
        'add_new'             => __( 'Ajouter un slide'),
        'edit_item'           => __( 'Editer le carousel'),
        'update_item'         => __( 'Modifier le carousel'),
        'search_items'        => __( 'Rechercher un slide'),
        'not_found'           => __( 'Non trouvé'),
        'not_found_in_trash'  => __( 'Non trouvé dans la corbeille'),
    );

    $args = array(
        'menu_icon'           => 'dashicons-image-flip-horizontal',
        'label'               => __( 'Sliders'),
        'description'         => __( 'Tous sur les slides'),
        'labels'              => $labels,
        // On fait en sorte de ne pas afficher l'éditeur etc.
        'supports'            => array( 'title', 'author', 'thumbnail','custom-fields', ),
        'show_in_rest'        => true,
        'hierarchical'        => false,
        'public'              => true,
        'has_archive'         => true,
        'rewrite'             => array( 'slug' => 'carousel'),

    );
    register_post_type( 'carousel', $args );
}
add_action( 'init', 'cpt_carousel', 0 );

My taxonomy:

    $labels_categories = array(
    'name'                       => _x( 'Catégories', 'taxonomy general name'),
    'singular_name'              => _x( 'Catégorie', 'taxonomy singular name'),
    'search_items'               => __( 'Rechercher une catégorie'),
    'popular_items'              => __( 'Catégories populaires'),
    'all_items'                  => __( 'Toutes les Catégories'),
    'edit_item'                  => __( 'Editer une catégorie'),
    'update_item'                => __( 'Mettre à jour une catégorie'),
    'add_new_item'               => __( 'Ajouter une nouvelle catégorie'),
    'new_item_name'              => __( 'Nom de la nouvelle catégorie'),
    'separate_items_with_commas' => __( 'Séparer les Catégories avec une virgule'),
    'add_or_remove_items'        => __( 'Ajouter ou supprimer un Catégorie'),
    'choose_from_most_used'      => __( 'Choisir parmi les plus utilisées'),
    'not_found'                  => __( 'Pas de Catégories trouvés'),
    'menu_name'                  => __( 'Catégories'),
);

$args_categories = array(
    'hierarchical'          => false,
    'labels'                => $labels_categories,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var'             => true,
    'rewrite'               => array( 'slug' => 'categories' ),
);

register_taxonomy( 'categories', 'carousel', $args_categories );

URL field for the carousel button:

    add_action('wp_insert_post', 'champ_carousel_url');
function champ_carousel_url($postID) {
    $post = get_post($postID);
    if ($post->post_type == 'carousel') {
        add_post_meta($postID, 'url', '', true);
    }
    return true;
}

Excuse me but the formatting of the code does not work very well

CPT for the carousel: enter image description here

Taxonomy: enter image description here

URL field: enter image description here

7
  • 2
    Can you explain what CPT is and how it is related to the Raspberry Pi?
    – kwasmich
    Commented Oct 29, 2019 at 9:09
  • 1
    Excuse me but the formatting of the code does not work very well. I added the cpt to my question above.
    – TBA
    Commented Oct 29, 2019 at 9:35
  • CPT = Corel Photo Paint file? Commented Oct 29, 2019 at 14:08
  • here are the pictures
    – TBA
    Commented Oct 29, 2019 at 14:26
  • 1
    Please don't post pictures of text. Formatting code blocks is very simple. Just indent it with 4 spaces or start and end the code block with a line just containing three ``` (back ticks).
    – Ingo
    Commented Oct 29, 2019 at 15:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.