Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically select a template for a child page with the same name as another

In a wordpress website I have this page structure:

/posts
/another-page/posts

Basically they are two different pages listing different posts, even though the name (posts) is the same. The two pages should use different templates. For the first "posts" page I just create a file called page-posts.php, and it's automatically chosen. How can make the same thing for the other "posts" page?

Is the only solution to create a template and manually select it from the dropdown in the admin page? Isn't there any way to change the slug withouth affecting the url?

like image 887
Carlo Avatar asked Dec 05 '25 19:12

Carlo


1 Answers

You can create two template files name page-{id}.php for each page. This would handle both pages for you.

Template Hierarchy Page Section

The IDs for each page will be unique. So when you select Edit from the WP-Admin area, look at the URL and you can find the ID.

/wp-admin/post.php?post=6163&action=edit

For this page, my page ID would 6163. So the template name would page-6163.php.

Cheers

like image 95
justindgivens Avatar answered Dec 08 '25 11:12

justindgivens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!