0

I have a custom post type with an archive-my-post-type page and now I need a second archive page without header and footer and with a custom content.

So my question how can I create a second page with custom template which queries the posts from my custom post type ?

1 Answer 1

0

Assuming post type is my-post-type.

We can create a page template with custom query using WP_Query.

Let's say we create template-multier.php with the below code in the root directory of the active theme.

<?php
    /*
    Template Name: Custom Post Type Archive
   */

    //Custom header stuff here

    //Custom query for `my-post-type`

    $args = array(
        'post_type' => 'my-post-type',
        'posts_per_page' => '-1' // All posts
    );

    $the_query = new WP_Query( $args );

    // Sample Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }

    /* Restore original Post Data */
    wp_reset_postdata();

    //Custom footer stuff here

Then we can use above page template to display posts with my-post-type

4
  • Thanks I know how I can create the template but my problem is how can I link to this template? I have a init funtion in my functions.php to init my custom post type and so I've created a archive-my-post-type.php which is called automatically... But I don't know how to call another template. Apr 25, 2016 at 10:29
  • Can you clarify what do you mean by linking this template ?
    – bravokeyl
    Apr 25, 2016 at 10:31
  • In my init function in the functions.php I have defined a slug e.g. "mitglieder" with this I can call my custom template mysite.com/mitglieder and now a I need a second page with the same posts but another template, do I need to create a second init function with another slug? Apr 25, 2016 at 10:48
  • No. Create a page with what-ever name you need , let's say mitglieder-two and use the code above for the new template i have mentioned and then set mitglieder-two page template as the above
    – bravokeyl
    Apr 25, 2016 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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