• Hi,

    I’m looking for a (plugin-free) solution to display a custom column into the page dashboard which would show the page template name of every page created.

    For now I’m only able to display the path of the template used, which is not really elegant.

    /** 
     * Display page template's name column into admin
     */
    //Add the custom column to the post type
    add_filter( 'manage_pages_columns', 'sp_add_custom_column' );
    function sp_add_custom_column( $columns ) {
      $columns['template'] = 'Template';
      return $columns;
    }
    // Add the data to the custom column
    add_action( 'manage_pages_custom_column' , 'sp_add_custom_column_data', 10, 2 );
    function sp_add_custom_column_data( $column, $post_id ) {
      switch ( $column ) {
        case 'template' :
          $post = get_post( $post_id );
          //echo get_post_meta( $post->ID, '_wp_page_template', true );
          echo get_page_template_slug( $post ); 
        break;
      }
    }

    What do you think?

    Thanks by advance for your help !

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You might look at the plugin “show current template”, as it gets all of the templates used in rendering a page. There should be a lot of good code there you could use as a base.

    Thread Starter Obje

    (@harrow)

    Hi Steven,

    I’ve tried to borrow some part of this exact plugin’s code before posting my topic ??

    But it seems to display the striped url of the template file, not the exact page template’s name declared into the template file under :

    /*
      Template Name: Xxxxxxxxxx
      Template Post Type: page
    */

    I will probably end with that if I can’t find a workaround, but I’m be a bit confused to not find something like ‘get_page_template($post->name)’.

    Moderator bcworkz

    (@bcworkz)

    If a custom page template is specified, it’s stored in the post’s postmeta under the key _wp_page_template. If no such record exists, the template used falls to template hierarchy, so page.php with a lot of themes.

    Thread Starter Obje

    (@harrow)

    Thanks for your feedback @bcworkz ,

    I’ve tried something like below
    echo get_post_meta( $post->ID, '_wp_page_template', true );

    Sadly, using _wp_page_template seems to also return a path with the filename

    Moderator bcworkz

    (@bcworkz)

    Yes, that’s necessary if the template is not in the theme folder. If you want just the filename out of the path, use PHP’s pathinfo().

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display page template’s name into the admin’ is closed to new replies.