I want to change my page template automatically when someone selects a specific post parent.
I have the code to update the template but couldn’t find anything for detection of post parent attribute changes. Can you help?
I want to change my page template automatically when someone selects a specific post parent.
I have the code to update the template but couldn’t find anything for detection of post parent attribute changes. Can you help?
Comments are closed.
This is how you do it…
Notes:
To explain what is happening, first we hook onto the
save_post
action which is fired when inserting/updating a post. We then check for the existence of a certainpost_parent
id which is up to you to decide and determine which (you may want to extend this conditional statement to cover multiple cases). If we find a match, we unhook thesave_post
action to avoid creating an infinite loop when we call ourupdate_post_meta
function. Once the page template has been updated, we re-hook thesave_post
action and continue on our merry way.Using MySQL you can do;