0

In my plugin (my first one), I inserted 2 posts with a custom post_type ('foo' for this example). This kind of post is exactly like a page but which is unedited (so hidden in the admin. interface). I would like to display this post on the front-end as a page (so with the template: page.php) when the user click on the button with the url : - mysiteurl.com?post_type=foo&p=140 where 140 is the post ID.

I get this error

Trying to get property of non-object in ...\wordpress\wp-includes\canonical.php on line 120

<?php

namespace MyNameSpace\FrontEnd;

class FrontEnd {

    public function __construct(){

        // Include dependencies
        $this->include_dependencies();

        // Initialize the components
        $this->init();

    }

    private function include_dependencies(){
         ...
    }

    private function init(){


        add_filter( 'template_include', array( $this, 'foo_subs_form_page_template'), 99 );
        ...


    }

    public function foo_subs_form_page_template( $template ) {

        if ( is_page( 'foo' )  ) {
            $new_template = locate_template( array( 'page.php' ) );
            if ( '' != $new_template ) {
                return $new_template;
            }
        }

        return $template;
    }
}

What do I need for that ?

0

To use page.php specifically, you can use the template_include filter.

You could also copy page.php in your theme, saving it as single-foo.php, and it would then be used for your "foo" CPT's individual posts. You would want to do this in a custom or child theme rather than editing a parent theme directly, so that whenever your theme is updated, you don't lose this edit.

5
  • It's a plugin development, it's completely separate of the theme. This post is exactly a page so I would like to avoid to make a template (which will be the copy of page.php)
    – J.BizMai
    Sep 18 '17 at 13:20
  • So, using the template_include filter will serve your purposes.
    – WebElaine
    Sep 18 '17 at 13:22
  • Sorry but maybe is a part of the solution but that does not work.
    – J.BizMai
    Sep 18 '17 at 13:35
  • To check something in template_include, I already need to get post information... I do not magae to get it :/
    – J.BizMai
    Sep 18 '17 at 14:17
  • Here are a couple of other options specifically for plugins: wpexplorer.com/wordpress-page-templates-plugin -- jeroensormani.com/how-to-add-template-files-in-your-plugin
    – WebElaine
    Sep 18 '17 at 14:49
0

I found the solution ! To display the post as a page, I needed to do these steps :

1) Register the post type

private function init(){
    add_action( 'init', array( $this, 'create_post_type') );
}

public function create_post_type() {
    register_post_type( 'foo',
        array(
            'labels' => array(
                'name' => __( 'Foos' ),
                'singular_name' => __( 'Foo' )
            ),
            'public' => true,
            'exclude_from_search' => true,
            'has_archive' => true,
            'show_in_menu' => false,
            'show_in_nav_menus' => false,
            'show_in_admin_bar' => false,
            'rewrite' => true //<-- Important !
        )
    );
}

2) Choose the template

 private function init(){
    add_action( 'init', array( $this, 'create_post_type') );
    add_filter( 'template_include', array( $this, 'foo_page_template'), 99 );
}

public function foo_page_template( $template ) {
    if ( get_post_type() === 'foo' ) {
        $new_template = locate_template( array( 'page.php' ) );
        if ( '' != $new_template ) {
            return $new_template;
        }
    }

    return $template;
}

3) (Optional) Add body classes

add_action( 'body_class', array($this , 'add_body_classes' ));

public function add_body_classes( $classes ) {

    // Add custom class
    $classes[] = 'page';
    $classes[] = 'page-two-column';

    return $classes;
}

The href was changed by mysiteurl.com/foo/my-post-name and that works if you do not forget 'rewrite' => true when you use register_post_type() !

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.