0

I been modifying a WordPress theme, and for some specific pages that need a specific design I placed a php file named page-specific-url.php where specific-url is the page I want to show.

The problem I am facing is the location of the specific page I need to design is inside of of a category so the url is domain.com/category1/specific-page. How can I add the extra category1 so when I placed the specific-page.php in the root of the theme I can access it through domain.com/category1/specific-page?

2

There is a filter called theme_page_templates which you can use to force the use of a certain template under certain circumstances. Like this:

add_filter ('theme_page_templates', 'wpse263827_filter_theme_page_templates', 20, 3 );
wpse263827_filter_theme_page_templates ($page_templates, $this, $post) {
  if (condition based on $post) $page_templates = 'path-to-template.php';
  return $page_templates;
  }

So, you can place the template file wherever you want and assign it when the page is in that certain category. No need to touch the url structure.

0

This is where body tags are your best friend. I would add the category as a custom body tag using this function:

/*Add custom body tag for categories*/
add_filter( 'body_class', 'cat_body_class' );
function cat_body_class( $classes ) {
if (is_single()) {
 $category = get_the_category();
 $classes[] = ‘category-‘.$category[0]->slug;
}
 return $classes;
}

Then simply use that body tag to style the page :) You won't even have to add any new page template (unless those categories have WAY different design/layout, or completely different logic.

1
  • Thanks for the quick answer Mayers. The page actually has a different layout, that is why I was trying to create a new page using the page-.....php that wordpress provide. How should apply the function in that case? Thanks
    – Gonzalo
    Apr 17 '17 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.