TUTORIAL

Custom type posts – creating a list with custom post type in WordPress

In this article you will learn how to create a list with custom post in WordPress. If we have a set of entries of a given type and would like to create a list view that displays all the posts, we need to create a file that would handle it. In our case we will be based on a previously created article Advanced Custom Fields – custom field types WordPress, where we added our own type of entries: Projekt. On the basis of these projects we would like to create a list of projects, which would properly display: The title of the project, a short description of the project, a distinguishing image.

How do you create a list with custom post type of entries in WordPress ?

Let’s do it

The first step is to edit our Custom Post Type Project. Let’s go to CPT UI tab in WordPress panel. Let’s choose our Post Type Design and look for the Has Archive section. Let’s change the value to true and set the url – ”projekty”.

 width=.

 

After approving the change in our post type Projekt, let’s go to the folder of our theme – in our case wp_tutorial and find the archive.php file. This file is responsible for displaying a set of basic WordPress entries. To create a list view of your own type of entries, follow the same procedure as in Create a view (template) for a custom post in WordPress. Copy the archive.php file and add Custom Post Type Slug: archive-projekt.php.

 width=.

We are going to edit the archive-projekt.php file. By default, the code and view are as follows:


We would like them to appear in our view:

  • a signature that says it’s a collection of projects,
  • the project title,
  • short description,
  • the distinguishing image.

The rest of the items are unnecessarily displayed, so they should be removed. We move on to code editing. Delete the title of the archive and its description and replace it with your own title: Transite of Projects. We also need to change it to take a view from the content-projects.php file, changing the ‘template-part/content’ path to ‘template-part/content-projects’..

 

get_header(); ?> <div id=”primary” class=”content-area”> <main id=”main” class=”site-main”> <?php if ( have_posts() ) : ?> <header class=”page-header”> <h2 class=”tytul-zbioru”>Zbiór Projektów</h2> </header><!– .page-header –> <?php while ( have_posts() ) : the_post(); get_template_part( ‘template-parts/content-projekty’, get_post_type() ); endwhile; the_posts_navigation(); else : get_template_part( ‘template-parts/content’, ‘none’ ); endif; ?> </main><!– #main –> </div><!– #primary –> <?php get_footer();

Go to the folder /template-parts select the file content.php copy and rename the copy to content-projekty.php. Go on to edit the file.

 

We remove unnecessary sections:

  • entry-meta– delete the entire section if displaying a section
  • the_content– delete the entire content display block
  • wp_link_pages– delete display function
  • entry-footer– delete article footer

Our code after reduction:


We proceed to the correct display of the list. We want the image to be displayed at the end of a single article, to do this we move wp_tutorial_post_thumbnail() before end of tag< /article>“. In the entry-content we add a short project description using the_excerpt().

We have received the following project list view.

Summary

Creating a view for a list of custom posts is similar to creating a template for a single custom post. Similarly, we create a archive.php file, and the display is slightly different. Assign the appropriate template-part for displaying a single post in the list. We can use different variations to display the Custom Post set, it can be, as in this case of the list, a more complex and complicated view.