1

In Wordpress I have a page with the slug logged-user with a parent page ecadmin, making it accessible under the URL /ecadmin/logged-user/.

Since it has a parent page, I (or any other user) can create a page with the same slug logged-user as long as it isn't nested under the same parent page.

The problem now is that I cannot create a single page template page-logged-user.php in theme's folder, as this template can be potentially applied to any other page named logged user no matter where it belongs hierarchly.

Is there a way to name the template file in such way that is referencing to its parent page(s) as well? E.g. page-ecadmin-logged-user.php

2 Answers2

2

The feature you are hoping for doesn't exist, but there is a quite simple way to accomplish what you want to do:

You can create a custom page template [whatever-you-want].php and place this comment in the header

/*
Template Name: Logged In User
*/

That template will be available in the Page Attributes meta box for any "Page" in Wordpress, but will only be applied to a given page if selected in that meta box.

Note:

All content creators have access to the page attributes meta box by default. If you are worried about users applying that template inappropriately you could hide the Page Attributes meta box from everyone who isn't an admin:

function remove_post_custom_fields() {

    if (!current_user_can( 'create_users' )){
         remove_meta_box( 'pageparentdiv' , 'post' , 'normal' ); 
    }
}
add_action( 'admin_menu' , 'remove_post_custom_fields' );

http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/

vlasits
  • 2,215
  • 1
  • 15
  • 27
-2

You could try using using page-$id.php which would be unique to that page.

Paul
  • 1,412
  • 1
  • 9
  • 19
  • thanks for your reply, I already thought of that, but I am working on a development environment and when I deploy the ids are never the same in production. – Chris Athanasiadis Apr 05 '17 at 13:16
  • That's a pain alright. The only other solution I can think of offhand is to write some conditional code in your page-logged-user.php file. If the post_parent slug === ecadmin then output logged in code else just show regular page code. – Paul Apr 05 '17 at 13:20
  • 1
    Using this method is generally pretty brittle; I'd recommend avoiding it as much as possible. @ChrisAthanasiadis has a perfect example of why this is bad. Also if you delete and remake the page it's very difficult to re-apply the same ID. You either end up in the database or changing your theme files which is not ideal. – Jake Feb 12 '20 at 04:12