最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I find out which (page) template file my custom child post is looking for?

programmeradmin2浏览0评论

I am trying to make something like a web-app with WP. One of the issues I'm facing now is that I cannot find out which page template file my post is looking for. I have looked at template file hierarchy etc. but to no avail. Don't really know which steps to follow from here so hoping there's someone that can help me.

I have made a custom post type called "week". When a week post is published, 7 days of the week, called "palaver" are also generated. These palaver posts are children of the week post and also inherit the custom taxonomy of the published week post. See the code below in my functions.php.

function create_days_week($new_status, $old_status, $post){
if ('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'week'){
    $week = array(
    "0" => 'Saturday',
    "1" => 'Sunday',
    "2" => 'Monday',
    "3" => 'Tuesday',
    "4" => 'Wednesday',
    "5" => 'Thursday',
    "6" => 'Friday'
);

    $latestposts = new wp_query(array(
        'post_type' => 'week',
        'orderby'   => 'post_date',
        'order'     => 'desc',
        ));
    $latestpost = $latestposts->posts;
    $CategoryName = get_the_terms($latestpost[0]->ID, 'area');
    $CatTermID = array($CategoryName[0]->term_taxonomy_id, $CategoryName[1]->term_taxonomy_id);

     foreach ($week as $weekday) {
        $PostID = wp_insert_post(array(
            'post_title'    => $latestpost[0]->post_title . ': ' . $weekday,
            'post_type'     => 'palaver',
            'post_parent'   => $latestpost[0]->ID,
            'post_content'  => ''
        ));
        wp_set_object_terms($PostID, $CatTermID, 'area');
        update_post_meta($PostID, '_wp_page_template', 'single-palaver.php' );      
     }
}} add_action('transition_post_status', 'create_days_week', 10, 3);

So this works fine (took a decent while to figure everything out though haha). When I look at palaver posts in the admin the posts are displayed as "— Week 1: Tuesday — Draft | Parent Page: Week 1". I'm not sure where the leading dash comes from, but since it does not show in the title or anything I have not really bothered to figure it out.

When the status is still draft the URL shows like this "/?post_type=palaver&p=1264&preview=true". The preview page is powered by my single-palaver.php template.

However when I publish the palaver post the url changes to "/palavers/week-1/week-1-thursday/". Which is

[rewrite of slug -> palavers]/[week post]/[palaver post] or

[palaver post]/[parent page/post]/[child page/post].

Post type & taxonomy registration

    register_post_type('palaver', array(
    'supports' => array(
        'title',
        'editor'),
    'rewrite' => array('slug' => 'palavers'),
    'has_archive' => true,
    'taxonomies' => array('area'),
    'public'=> true,
    'menu_icon' => 'dashicons-calendar-alt',
    'hierarchical' => 'true',
    'labels'=> array(
        'name' => 'Palavers',
        'add_new_item' => 'Add new Palaver',
        'edit_item' => 'Edit Palaver',
        'all_items' => 'All Palavers',
        'Singular name' => 'Palaver'
    )
));

Both Week and Palaver are exactly the same apart from name and labels.

register_taxonomy('area', array('week', 'palaver'), array('hierarchical' => true));

What I tried

1. I tried the following template name files:

  • single-palaver-week-palaver.php
  • single.php
  • page.php
  • taxonomy-palaver.php
  • palaver.php

And several variations which seemed plausible.

2. I also tried to set the template file when generating the post:

update_post_meta($PostID, '_wp_page_template', 'single-palaver.php' );  

3. I made a single palaver post which was recognized and after publication loaded from the single-palaver.php file. URL was /palavers/test.

4. I tried to add a filter in functions.php:

add_filter('page_template', function ($template) {
    global $post;
    if ($post->post_parent) {
        $template = locate_template(['single-palaver.php']);
    }
    return $template;
} );

If there is a parent page it could only be the palaver post so this should have worked, but did not.

5. I tried to get the page template slug:

<?php echo esc_html( get_page_template_slug( $post->ID ) ); ?>

However it seems that there are no posts at all to load so this also did not help.. :(


What is shown?

(1) In the "normal published palaver post" with the URL:/palavers/week-1/week-1-thursday/. My header and footer are loaded and it works of the index.php file. However, The Loop does not recognize the current (or any for that matter) post, so nothing is shown beyond that.

(2) When I go to the URL:/palavers something completely strange happens. The Loop recognizes the posts of type "palaver" but works off of 2(!) template files. First it goes through the complete single-palaver.php template file (inc. header and footer), after which it goes through the complete index.php file (again inc. header and footer).

Ideal situation

I'm not sure yet whether I'm going to need an archive for the palaver post type so (2) is not really a priority.

For (1) though I would like all my palaver posts (independent of whether they are children or not) to load the template file single-palaver.php.

I have the feeling that I tried everything but am missing something trivial. If anyone knows an answer or solution to this, or another avenue to explore, it would be greatly appreciated!

Thanks in advance :)

I am trying to make something like a web-app with WP. One of the issues I'm facing now is that I cannot find out which page template file my post is looking for. I have looked at template file hierarchy etc. but to no avail. Don't really know which steps to follow from here so hoping there's someone that can help me.

I have made a custom post type called "week". When a week post is published, 7 days of the week, called "palaver" are also generated. These palaver posts are children of the week post and also inherit the custom taxonomy of the published week post. See the code below in my functions.php.

function create_days_week($new_status, $old_status, $post){
if ('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'week'){
    $week = array(
    "0" => 'Saturday',
    "1" => 'Sunday',
    "2" => 'Monday',
    "3" => 'Tuesday',
    "4" => 'Wednesday',
    "5" => 'Thursday',
    "6" => 'Friday'
);

    $latestposts = new wp_query(array(
        'post_type' => 'week',
        'orderby'   => 'post_date',
        'order'     => 'desc',
        ));
    $latestpost = $latestposts->posts;
    $CategoryName = get_the_terms($latestpost[0]->ID, 'area');
    $CatTermID = array($CategoryName[0]->term_taxonomy_id, $CategoryName[1]->term_taxonomy_id);

     foreach ($week as $weekday) {
        $PostID = wp_insert_post(array(
            'post_title'    => $latestpost[0]->post_title . ': ' . $weekday,
            'post_type'     => 'palaver',
            'post_parent'   => $latestpost[0]->ID,
            'post_content'  => ''
        ));
        wp_set_object_terms($PostID, $CatTermID, 'area');
        update_post_meta($PostID, '_wp_page_template', 'single-palaver.php' );      
     }
}} add_action('transition_post_status', 'create_days_week', 10, 3);

So this works fine (took a decent while to figure everything out though haha). When I look at palaver posts in the admin the posts are displayed as "— Week 1: Tuesday — Draft | Parent Page: Week 1". I'm not sure where the leading dash comes from, but since it does not show in the title or anything I have not really bothered to figure it out.

When the status is still draft the URL shows like this "/?post_type=palaver&p=1264&preview=true". The preview page is powered by my single-palaver.php template.

However when I publish the palaver post the url changes to "/palavers/week-1/week-1-thursday/". Which is

[rewrite of slug -> palavers]/[week post]/[palaver post] or

[palaver post]/[parent page/post]/[child page/post].

Post type & taxonomy registration

    register_post_type('palaver', array(
    'supports' => array(
        'title',
        'editor'),
    'rewrite' => array('slug' => 'palavers'),
    'has_archive' => true,
    'taxonomies' => array('area'),
    'public'=> true,
    'menu_icon' => 'dashicons-calendar-alt',
    'hierarchical' => 'true',
    'labels'=> array(
        'name' => 'Palavers',
        'add_new_item' => 'Add new Palaver',
        'edit_item' => 'Edit Palaver',
        'all_items' => 'All Palavers',
        'Singular name' => 'Palaver'
    )
));

Both Week and Palaver are exactly the same apart from name and labels.

register_taxonomy('area', array('week', 'palaver'), array('hierarchical' => true));

What I tried

1. I tried the following template name files:

  • single-palaver-week-palaver.php
  • single.php
  • page.php
  • taxonomy-palaver.php
  • palaver.php

And several variations which seemed plausible.

2. I also tried to set the template file when generating the post:

update_post_meta($PostID, '_wp_page_template', 'single-palaver.php' );  

3. I made a single palaver post which was recognized and after publication loaded from the single-palaver.php file. URL was /palavers/test.

4. I tried to add a filter in functions.php:

add_filter('page_template', function ($template) {
    global $post;
    if ($post->post_parent) {
        $template = locate_template(['single-palaver.php']);
    }
    return $template;
} );

If there is a parent page it could only be the palaver post so this should have worked, but did not.

5. I tried to get the page template slug:

<?php echo esc_html( get_page_template_slug( $post->ID ) ); ?>

However it seems that there are no posts at all to load so this also did not help.. :(


What is shown?

(1) In the "normal published palaver post" with the URL:/palavers/week-1/week-1-thursday/. My header and footer are loaded and it works of the index.php file. However, The Loop does not recognize the current (or any for that matter) post, so nothing is shown beyond that.

(2) When I go to the URL:/palavers something completely strange happens. The Loop recognizes the posts of type "palaver" but works off of 2(!) template files. First it goes through the complete single-palaver.php template file (inc. header and footer), after which it goes through the complete index.php file (again inc. header and footer).

Ideal situation

I'm not sure yet whether I'm going to need an archive for the palaver post type so (2) is not really a priority.

For (1) though I would like all my palaver posts (independent of whether they are children or not) to load the template file single-palaver.php.

I have the feeling that I tried everything but am missing something trivial. If anyone knows an answer or solution to this, or another avenue to explore, it would be greatly appreciated!

Thanks in advance :)

Share Improve this question asked May 16, 2020 at 12:58 bramdekbramdek 1
Add a comment  | 

1 Answer 1

Reset to default 0

If think you were using the wrong filter, please try using 'single_template'.

I assume you have created a plugin to create your custom post type. In the plugin folder you have a "templates" folder where you put the single page template called "palaver_single.php".

function palaver_single_mapping($single) {

global $post;

    if ( $post->post_type == 'palaver' ) {
        if ( file_exists( plugin_dir_path( __FILE__ ) . '/templates/palaver_single.php' ) ) {
            return plugin_dir_path( __FILE__ ) . '/templates/palaver_single.php';
        }
    }

    return $single;
}
add_filter( 'single_template', 'palaver_single_mapping' );

So if a single palaver post is viewed, the template file inside the templates folder will be used.

发布评论

评论列表(0)

  1. 暂无评论