0

I have the following post types:

Post - wordpress built in Documents - custom post type Videos - custom post type

I am very new to wordpress and I do not understand the template structure. I am creating template from scratch. I have created static front-page that lists recent "posts", recent "documents" and recent "videos". SO I do not want to use front-page to display all posts. I am using %postype% permalink structure and I am also able to display single item from each custom post type.

What I cannot do is:

  1. I need to have link like mypage.com/posts (or very similar) to display a list of posts with paging (10 posts per page)
  2. I need to have link like mypage.com/videos (or very similar) to display a list of videos with paging (10 posts per page)
  3. I need to have link like mypage.com/documents (or very similar) to display a list of documents with paging (10 posts per page)

I understand how to retrieve 10 posts and I will probably be able to create paging, but I do not understand template structure and heararchy and routing.

Questions How to name the file(s) where I should add my php code (based on wordpress traditional naming)?

Should I create some dummy pages?

How to make routing work and take code from my newly created php file(s)?


Also I am not sure if this would be correct to call this archive or not taking into account that later I need to filter also posts by date combining all post types in the list.

1

You don't have to create any dummy page for custom post types, as those archive pages are just an automated process and named using the post type object name.

To make sure your Custom Registered Post Type uses/support archive page, set the parameter 'has_archive' => 'documents' for Document & 'has_archive' => 'videos' for Video.

register_post_type( 'video', array(
    'labels'      => '....',
    'has_archive' => 'videos'
) );
register_post_type( 'document', array(
    'labels'      => '....',
    'has_archive' => 'documents'
) );

Now, you can create two files in your theme folder to display both archive or you can use the default one archive.php.

For documents archive, the filename would be archive-document.php For videos archive, the filename would be archive-video.php

The archive page template file for the default post type post is home.php. If you want to display posts at site.com/posts, just simply create a dummy page using slug 'posts', and set the "Front page displays" (wp admin -> settings -> reading) option to "A Static page", and set the "Posts page:" to the newly created posts page.

However, common practices are different. What most of the people does is to make the index.php file adoptable. So regardless of post type, it display posts simply. A simple index.php file looks like the following -

<?php get_header(); // this gets the header.php file ?>
<?php if( have_posts() ) : // this checks if current query have posts ?>
    <?php while( have_posts() ): // this trigger looping through each of the post ?>
        <?php the_post(); ?>

        <?php the_title(); ?>
        <?php the_content(); ?>

    <?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); // this gets the footer.php file ?>

If your theme directory don't have a archive.php or archive-{$post_type}.php file, then for any kind of post type archive page, index.php file will be called. the_title() function displays the title, the_content() displays the content of that post.

You really don't have to care about routing for archive page as WordPress does it within core. Including the pagination - site.com/videos/page/2/.

The big reference for naming or anything about Template: Template_Hierarchy

  • Can you, please, explain this "However, common practices are different. What most of the people does is to make the index.php file adoptable. So regardless of post type, it display posts simply." How you mean this? Currently my index.php is never called at all. – renathy Jul 2 '14 at 8:43
  • Another question- for default archive page for post type "post" is home.php, what link would be used to rout to it? For testing purposes, I have created simple home.php and I cannot navigate there. – renathy Jul 2 '14 at 8:44
  • First question explained in the answer. For post type "post", the default link is the front page (site.com) unless you have changed the "Front page displays". – Shazzad Jul 2 '14 at 14:01
  • I have changed it to front page displays. And it has custom logic, displaying recent posts for different custom post types. However, I now need seperate page that will show all posts. And I do not understand how to navigate it (no problems with creating php loop, showing content etc., bet I have problem with navigating to the correct file). – renathy Jul 3 '14 at 9:08

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.