0

I'm new to WordPress (and to code), having just started my classes. I am working on a project with a few students and can't find a way to call my Custom Post Type.

I tried several solutions from StackExchange, but I can't really find a solution for my problems.

Problem 1 : my custom Post type doesn't have the right name in my back office

Here's how I coded my custom post type :

function custom_post_type_bateau() {
    $labels = array(
        'name'               => 'Bateaux',
        'singular_name'      => 'Bateau',
        'menu_name'          => 'Bateaux',
        'name_admin_bar'     => 'Ajouter',
        'add_new'            => 'Nouveau bateau',
        'add_new_item'       => 'Ajouter un bateau',
        'edit_item'          => 'Modifier un bateau',
        'view_item'          => 'Consulter un bateau',
        'search_items'       => 'Chercher un bateau',
        'parent_item_colon'  => 'Bateaux parents',
        'not_found'          => 'Aucun bateau n\'a été trouvé',
        'not_found_in_trash' => 'Aucun bateau n\'a été trouvé dans la corbeille'
    );

    $args = array(
        'labels'             => 'Bateau',
        'description'        => 'Un bateau de SailingLoc',
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'bateau' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'menu_icon'          =>'dashicons-paperclip',
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'bateau', $args );
}

I called it in my functions.php :

add_action('init', 'initialisation');

function initialisation(){
    //activation des menus dans le backoffice
    register_nav_menus();
    //creation d'un custom post type bateau
    custom_post_type_bateau();
    // activation des images à l'affiche
    add_theme_support('post-thumbnails');
}

Problem is that the name displayed in my back-office isn't "Bateaux" but "Articles".

Backoffice screenshot

Problem 2 : I can't call the post-type bateau So I made a page to display my post-types. I called it with this WP Query :

<?php
    $query = new WP_Query([
        'post_type' => 'bateau',
        'posts_per_page'=>15
    ]);
?>
<?php
    $the_query = new WP_Query();
    $the_query->query($query);
; ?>
<div class="gp3">
<?php /*BOUCLE QUI RECUPERE LES DERNIERS POSTS*/
    if ( $the_query->have_posts() ) : 
        $cpt = 0;
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
    <div class="col-4">
        <div class="fiche-bateau">
            <div class="image-bateau">  
                <div class="product-thumbnails image-responsive"><?php the_post_thumbnail(); ?>
                </div>
            </div>
            <div class="product-title"><?php the_title('<h3>', '</h3>'); ?>
            </div>
        </div>
    </div>
<?php 
    $cpt ++;
    if ($cpt%3 == 0) :
?>
</div>
<div class="gp3">
<?php endif; ?>
<?php endwhile; else : ?>
    <p>Rien à afficher</p>
<?php endif; ?>
</div>

The variable $query should link to the custom post-type 'bateau' but doesn't. Instead I get the blog posts.

Could you help me pretty please ?

0

The issue with the labels in problem 1 is that you haven't passed the labels to register_post_type(). You've defined the labels in the $labels variable, but this is what you passed to register_post_type():

$args = array(
    'labels' => 'Bateau',
    // ...
);

register_post_type( 'bateau', $args );

You need to pass the $labels array:

$args = array(
    'labels' => $labels,
    // ...
);

register_post_type( 'bateau', $args );

For problem 2, your problem is that you're trying to pass a WP_Query instance to another WP_Query. $query needs to just be the array of arguments, but you've passed an entire WP_Query object:

<?php
    $query = new WP_Query([
        'post_type' => 'bateau',
        'posts_per_page'=>15
    ]);
?>
<?php
    $the_query = new WP_Query();
    $the_query->query($query);
; ?>

That needs to be just:

<?php
$the_query = new WP_Query ( [
    'post_type'      => 'bateau',
    'posts_per_page' =>15
] );
?>

But, you shouldn't need to create a page or template like this at all. In your post type registration you have:

'has_archive' => true,

This means that an archive (list) page for your post type is created automatically at http://example.com/bateau/. This is what the "Voir les articles" link in the admin bar in your screenshot is linked to. If this isn't working, make sure to go to Settings > Permalinks to flush your permalinks (visiting the settings page is enough), and make sure that your theme's templates are properly using the main loop:

if ( have_posts() ) : 
    while ( have_posts() ) : the_post();

    endwhile;
endif;

Note the lack of $the_query-> and any new WP_Query().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.