skip to Main Content

Pretty stuck !
I made a custom post type for my WP back office :

register_post_type('realisations',[
'label' => 'Réalisations',
'public' => true,
'menu_position' => 3,
'menu_icon' => 'dashicons-format-gallery',
'supports' => ['title', 'editor', 'thumbnail'],

]);

and I want all the articles to display in my custom template :

<?php/* Template Name: Réalisations */?>

<?php materialis_get_header();?>


<div class="top-page">
    <div class="title">
        <i class="fas fa-camera"></i>
        <h2>Réalisations</h2>
    </div>

    <div class="description-page">
        <p>Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen</p>
    </div>

    <div>
        
        

    </div>
</div>
<?php get_footer(); ?>

So I tried this old code that I found and it’s only showing the title of the post , not the articles inside :

<?php/* Template Name: Réalisations */?>

<?php materialis_get_header();?>

 

<div class="top-page">
    <div class="title">
        <i class="fas fa-camera"></i>
        <h2>Réalisations</h2>
    </div>

    <div class="description-page">
        <p>Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen</p>
    </div>

    <div>

        <?php
        $custom_post_types = get_post_types(
    array(
        // Set to FALSE to return only custom post types
        '_builtin' => false,
        // Set to TRUE to return only public post types
        'public' => true
    ),
    // Set to "objects", so we have the full CPT object
    'objects'
 );

// Add CPT permalinks to CPT objects
foreach ( $custom_post_types as $custom_post_type ) {
    $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );
}

// Output CPT archive index permalinks list
echo '<ul>';
foreach ( $custom_post_types as $custom_post_type ) {
    echo '<li>';
    echo '<a href="' . $custom_post_type->permalink . '">' . $custom_post_type->name . '</a>';
    echo '</li>';
}
echo '</ul>';
?>

    </div>
</div>
<?php get_footer(); ?>

Sorry for being a nooby in wordpress ! And thanks for your upcoming answers !

2

Answers


  1. so – a little mix-up. This code (Example ahead) is used to create a custom post type in WordPress… its similar to a normal post (like the one used in the blog) and not a page template which you tried to apply.

    There are a few differences between ‘custom post types’ and ‘page templates’. Page templates are still pages which mean they have no categories, usually no featured image (thumbnail) and are not queried in chronological order (by default) and also in the way we define them for use in the theme.

    Custom post type are very similar to posts – you could create "categories" structure for them, add tags etc.

    This is how you setup a custom post type
    In your theme functions.php or file included into it.

    add_action('init', 'projects_labels');
    function projects_labels() {
        $labels = array(
            'name'                  => __('Projects', THEME_NAME),
            'singular_name'         => __('Project', THEME_NAME),
            'add_new'               => __('Add Project', THEME_NAME),
            'add_new_item'          => __('Add Project', THEME_NAME),
            'edit_item'             => __('Edit Project', THEME_NAME),
            'new_item'              => __('New Project', THEME_NAME),
            'all_items'             => __('Projects', THEME_NAME),
            'view_item'             => __('View Project', THEME_NAME),
            'search_items'          => __('Find a Project', THEME_NAME),
            'not_found'             => __('No Projects Found', THEME_NAME),
            'not_found_in_trash'    => __('No Projects Found In The Trash', THEME_NAME), 
            'parent_item_colon'     => '',
            'menu_name'             => __('Projects', THEME_NAME),
    
        );
        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'publicly_queryable'    => true,
            'show_ui'               => true, 
            'show_in_rest'          => true, 
            'query_var'             => true,
            'capability_type'       => 'post',
            'has_archive'           => false,       // do u want categories?
            'hierarchical'          => true,
            'menu_position'         => 90,
            'menu_icon'             => get_bloginfo('template_url').'/img/projects.png',        // icon image 
            'supports'              => array('title', 'editor', 'author', 'custom-fields'),     // add support & features
        );
        register_post_type('project', $args);
    }
    

    To use these you need to create a file named

    single-project.php 
    

    The name is structured like this: single-CUSTOM_POSTYPE_NAME.php

    create that file in the root folder of your theme. then you could visit the new postype you created – if you encounter any problem visit the permalinks page (in wp backend) – a visit would refresh the permalink structure.

    example code for single-project.php

    <?php 
    get_header();
    
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            
            echo '
            <main class="project-wrap">
                <div class="container">
                    <div class="page-title"> <h1 class="title-tag">'.get_the_title().'</h1> </div>
                    <div class="page-content">'.apply_filters('the_content', get_the_content()).'</div>
                </div>
            </main>
            ';
        }
    }
    
    get_footer();
    ?>
    

    This is all very basic – I encourage you to use youtube in order to learn more about custom postype because it is hard to explain this in a short answer without sharing a screen.

    Best of luck..

    Login or Signup to reply.
  2. Create .php file and add this code and select your template Réalisations form your admin panel

    <?php
    /* 
    Template Name: Réalisations 
    */
    ?>
    $args = array(
        'post_type' => 'realisations',
        'post_status' => 'publish',
        'posts_per_page' => 10,
    );
      
    $arr_posts = new WP_Query( $args );
    ?>
    <div class="row">  
    <?php
    if ( $arr_posts->have_posts() ) :
      
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <div class="col-sm-3 col-md-3">
            <div class="productbox">
                <div class="product-image">
                <?php
                if ( has_post_thumbnail() ) :
                    the_post_thumbnail("full");
                endif;
                ?>
            </div>    
            <h3 class="entry-title"><?php the_title(); ?></h3>
            </div>
            </div>
            <?php
        endwhile;
    endif;
    wp_reset_query();
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search