How do I add a body class to specific pages?

add_filter(‘body_class’,’cp_new_body_classes’); function cp_new_body_classes($classes) { if( !is_page_template() ) $classes[] = ‘reg-page’; return $classes; } The problem was that you are only returning the classes if !is_page_template(), but you want to return the classes always as it contains the other classes you didn’t add yourself in an array, you just want to add some then return them … Read more

page-slug goes to not found

A template is not a page you can load directly. A template is used for the formatting (‘building’) of a ‘page’ (created via Page, Add) or ‘post’ (created via Post, Add). On that page/post editing page, there is a place to specify the template that the WP will use when the page is output. There … Read more

Can you call a template file without assigning template to a page in the admin panel?

You can hook into template_redirect like this: function custom_template_redirect() { global $wp; if ($wp->query_vars[‘pagename’] == ‘my-page-slug’) { // check the page slug status_header(200); // a 404 code will not be returned in the HTTP headers if the page does not exists include(TEMPLATEPATH . “/test.php”); // include the corresponding template die(); } } add_action( ‘template_redirect’, ‘custom_template_redirect’ … Read more

wp_title() handling of author page

I see my name in the title when viewing author pages. wp_title() runs this code during execution.. // If there’s an author if ( is_author() ) { $author = get_queried_object(); $title = $author->display_name; } Perhaps your author(s) don’t have a display name set? Additionally be sure to try disabling plugins that may be hooking a … Read more

Use is_category(), is_tag(), … in functions.php

Your problem is that you didn’t wrap it in a callback, but executed it immediately when functions.php was loaded by core. By then the global $wp_query object isn’t initialized fully and the is_category() etc. wrappers can’t deliver what you are expecting them to do. So your if/else checks should be moved inside the callback like … Read more

Grabbing the page template name?

An alternative to @Ben Cole that’s less intensive (especially if you have several page templates), but not as awesome because it doesn’t use the WP_Theme object 😉 function wpse_184317_get_template_name( $page_id = null ) { if ( ! $template = get_page_template_slug( $page_id ) ) return; if ( ! $file = locate_template( $template ) ) return; $data … Read more