0

I want to display the custom post type archive of my plugin inside a normal page template. My approach was:

function my_include_template($template) {
    global $post;

    if ( is_post_type_archive('my_custom_post') ) {

        $new_template = locate_template( array( 'page.php' ) );

        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

add_filter('template_include', 'my_include_template', 99);

This way the page.php template will be taken but the get_template_part() function of the template itself will be called for every post within the archive.

But in fact I don't want to do this, I simply want to output a list of all posts inside this archive by using the normal page template: e.g.

  • post title 1
  • post title 2
  • post title 3...

Note: I don't want to create a custom template within my plugin due to the fact that In my opinion I cannnot rely on all the theme authors:

<?php get_header(); ?>
    <div class="my-content">
        ...
    </div>  
<?php get_footer(); ?>

Because a custom template file like above, could break the layout if the theme has a non standard structure.

So this is the reason why it would be best to output my archive inside a normal page template of the theme itself.

Any suggestion how to handle this?


Update 1

I have to specify: Of course I can replace the content of a user selected "my-archive" page. This will work for the normal custom post archive so far.

My problem is that I additionally have a taxonomy as category for the posts. So my basic slugs look like:

  • Single post: /article/postname/
  • CPT archive: /articles/
  • Category Taxonomy: /articles/category1/

Would be okay to use the same page for both archives, but of course when visiting /articles/category1/ only the posts of this category should be shown and the url should be kept. So I can't simply redirect to the /articles/ page.

2
  • You can do what WooCommerce does and supply generic default templats with possible overrides.
    – Howdy_McGee
    Nov 26, 2015 at 22:35
  • Took a look into but their handling, is okay so far but doesn't solve my problem.
    – flowdee
    Nov 27, 2015 at 11:34

1 Answer 1

0

If you don't want to supply your own page templates and using your current code is not a possibility (because of how the loop loops through all the posts) I would recommend prompting the plugin users to select a page on which they want your plugin's content displayed (as an option on your plugin's setting page).

Once you have the ID of the page selected by the user, you would use the_content filter and modify the page's content, like this:

add_filter( 'the_content', 'wpse210003_render_archive' );
function wpse210003_render_archive( $content ) {
    // $selected_page_id is the page ID that the user has selected for displaying your plugin's content
    if ( is_page( $selected_page_id ) ) {
        // Get your post loop here and either add it to the existing page's content or throw away the content and just render your loop
        $content = 'My custom loop goes here';

        // Return your custom content here
        return $content;
    }
}

You will also be able to modify the title via the_title filter if you want to.

This way you can make sure that the layout of the page selected by user will always be correct and you will keep all the theme features associated with creating pages (sidebars, ad spaces etc.).

2
  • Hey Mateusz, thanks for the inspiration, this is a really good idea. So in fact for the single posts I can use the single or page template. And for the archives I always redirect to the "user selected page" and display my filtered content there. I would like to vote your answer up but unfortunately I cannot do it yet :)
    – flowdee
    Nov 27, 2015 at 8:36
  • Any hint how to forward the archive content to the user selected page? So for example the user clicks on the "mysite/articles/" archive page, how would you get your content on the selected for example "my-archive" page?
    – flowdee
    Nov 27, 2015 at 8:46

Your Answer

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

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