• Resolved mumumu

    (@imamura4108)


    Hi, thanks for the nice plugin.
    I am having trouble with single.php being applied.

    I have turned on the “Page Attributes” in the “Support” section of the settings page.
    However, the template “single.php” is applied.
    (Checked with plugin QueryMonitor)

    I would like to use page.php.
    What settings do I need to change?

    function cptui_register_my_cpts_contents() {
    
    	/**
    	 * Post Type: MyContent.
    	 */
    
    	$labels = [
    		"name" => __( "MyContents", "astra" ),
    		"singular_name" => __( "MyContent", "astra" ),
    	];
    
    	$args = [
    		"label" => __( "MyContent", "astra" ),
    		"labels" => $labels,
    		"description" => "For Members",
    		"public" => true,
    		"publicly_queryable" => true,
    		"show_ui" => true,
    		"show_in_rest" => true,
    		"rest_base" => "",
    		"rest_controller_class" => "WP_REST_Posts_Controller",
    		"has_archive" => false,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => false,
    		"delete_with_user" => false,
    		"exclude_from_search" => true,
    		"capability_type" => "post",
    		"map_meta_cap" => true,
    		"hierarchical" => true,
    		"rewrite" => [ "slug" => "contents", "with_front" => true ],
    		"query_var" => true,
    		"supports" => [ "title", "editor", "thumbnail", "page-attributes" ],
    		"show_in_graphql" => false,
    	];
    
    	register_post_type( "contents", $args );
    }
    
    add_action( 'init', 'cptui_register_my_cpts_contents' );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If you’re looking for having some of the file content/code from page.php get used for your single posts in this post type, it’d be best to just duplicate that page.php file and rename it to “single-contents.php` so that it gets picked up and used only by that post type, but still behaves as I’m assuming you’re wanting.

    Alternatively, I could see the template_include filter hook being potentially used here to load a different template file than what WordPress would natively pick up.

    Thread Starter mumumu

    (@imamura4108)

    Thank you very much.
    I was able to apply the template.
    Thank you for your quick and accurate answer.

    Thread Starter mumumu

    (@imamura4108)

    I’m sorry.
    I just marked it as “resolved”.

    However, I get “is_page() = false”, which is not the intended behavior.

    Is there any way to make “is_page() = true” for the post type added in the CPU UI?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Nope, you’ll want to change any usage of is_page() to something like is_singular()

    https://developer.ww.wp.xz.cn/reference/functions/is_singular/

    Most specifically, is_singular('contents') since this function allows you to pass a specific post type slug. It’ll return true any time you’re on a single contents URL, and false otherwise.

    Thread Starter mumumu

    (@imamura4108)

    Oh, I understand.
    I had made a basic misunderstanding.

    All “custom post types” in WordPress are “post types”.
    And “page type” is a special one.
    I thought I could choose one or the other.

    As you pointed out, is_page() is always false, is_single() is always true,
    and is_singular(‘contents’) which needs to be looked up in the custom name.

    Thank you for teaching me this.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How can I use page.php?’ is closed to new replies.