How to create posts (not post template) to be displayed on projects page?

The best way is to register a custom post type then loop through that type within your projects page template.

CPT from GenerateWP.

if ( ! function_exists('projects') ) {

// Register Custom Post Type
function projects() {

    $labels = array(
        'name'                  => _x( 'Projects', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Project', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Projects', 'text_domain' ),
        'name_admin_bar'        => __( 'Projects', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Project:', 'text_domain' ),
        'all_items'             => __( 'All Projects', 'text_domain' ),
        'add_new_item'          => __( 'Add New Project', 'text_domain' ),
        'add_new'               => __( 'Add New', 'text_domain' ),
        'new_item'              => __( 'New Project', 'text_domain' ),
        'edit_item'             => __( 'Edit Project', 'text_domain' ),
        'update_item'           => __( 'Update Project', 'text_domain' ),
        'view_item'             => __( 'View Project', 'text_domain' ),
        'search_items'          => __( 'Search Projects', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'items_list'            => __( 'Projects list', 'text_domain' ),
        'items_list_navigation' => __( 'Projects list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter project list', 'text_domain' ),
    );
    $rewrite = array(
        'slug'                  => 'project',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
    $args = array(
        'label'                 => __( 'Project', 'text_domain' ),
        'description'           => __( 'A Single Project', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-media-document',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
    );
    register_post_type( 'projects', $args );

}
add_action( 'init', 'projects', 0 );

}

In your Projects Template run your custom loop.

<?php
/*
 * Template Name: Projects Page Template
 * Description: Page template to display projects custom post types
 */

 get_header(); ?>

   <div id="primary" class="content-area">
     <main id="main" class="site-main" role="main">

       <?php
       // WP_Query arguments
        $args = array (
            'post_type' => 'projects',
        );

        // The Query
        $query = new WP_Query( $args );

        // The Loop
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {

            // PROJECT
                $query->the_post(); ?>
            <h3><a href="https://wordpress.stackexchange.com/questions/211354/<?php the_permalink();?>"><?php the_title(); ?></a></h3>
            <?php the_field('description'); ?>
            <hr><?php

            }
        } else { ?>
            <p>There are no posts or pages here</p>
        <?php }

        // Restore original Post Data
        wp_reset_postdata();

        // The Content
        echo apply_filters( 'the_content', get_the_content() );
     ?>

     </main><!-- .site-main -->
   </div><!-- .content-area -->

 <?php get_sidebar(); ?>
 <?php get_footer(); ?>

You can create a page /projects and set the template to Projects Template which will list all your projects as /project/project-title.

But if you’re looking to just add extra fields to a post and tag them as a project then that’s more of a loop question.

Leave a Comment