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