Hot answers tagged

18

A) The Base in Core As you can see in the Codex Template Hierarchy explanation, single-{$post_type}.php is already supported. B) Extending the core Hierarchy Now there're gladly some filters and hooks inside /wp-includes/template-loader.php. do_action('template_redirect'); apply_filters( 'template_include', $template ) AND: a specific filter inside ...


14

The front-page.php file is the site front page template. It will always be used on your site front page, regardless of whether get_option( 'show_on_front' ) is set to page or posts. The home.php template file is the blog posts index template. It will always be used to display your blog posts index, regardless of whether the blog posts index is displayed on ...


13

All this comes from a thorough reading of the Template Hierarchy. home.php is used if: Your site's front page is set to display a list of posts, or A static front page is set, and A visitor goes to the page that you've defined as your "list of posts" (eg, http://example.com/blog/) And the theme has a home.php file If the last condition isn't met -- ...


11

singular.php is basically an extra fallback for all post types, regardless if built-in or custom. It comes in straight after single.php and page.php, so you can omit the latter two templates and just have a singular.php template which will be used by all post types in single view. How useful it will be and the necessity of it will, just as with any other ...


10

Always flush the rewrite rules when you register a new public post type or taxonomy. Otherwise the internal rewrite rules will not take that into account when an URL is mapped to a query. You can automate that process by hooking into registered_post_type and registered_taxonomy. Below is the updated code, based on feedback from comments and other people. ...


9

It's the nature of the WordPress template hierarchy - point being, if you don't have a page.php, it will use singular.php, same if you don't have single.php, it will fallback to a template lower in the hierarchy. Ideal for themes that have the same layout for posts/pages, instead of having duplicate code in each respective template.


8

There is no specifica template for child pages, but you can do this pretty easily with the get_template_part() function. First create a file called "content-child.php". Second create a file called "content.php". Next, inside of page.php, place this: if( $post->post_parent !== 0 ) { get_template_part('content', 'child'); } else { ...


7

In short: index.php is a fallback template only, in case no appropriate template was found home.php is used for the blog (a listing of recent posts) front-page.php is used for the landing-page The universal index.php template The index.php template file is a fallback template. It is used as a last resort when no other more appropriate template is ...


6

Error pages are served up via .HTACCESS, if you are using Apache you would use the ErrorDocument directive and add the status and URL to it. So it would look like this in your .htaccess file: ErrorDocument 401 http://yourwebsite.com/error-401 ErrorDocument 403 http://yourwebsite.com/error-403 ErrorDocument 500 http://yourwebsite.com/error-500 You could ...


6

Thanks to @Rarst for guiding me to the right direction. Using his direction I googled again and again and found a blog article of WerdsWords with an excellent bit of code snippet filtered to category_template as Rarst suggested me, and the good news is: it worked for my cause: function new_subcategory_hierarchy() { $category = get_queried_object(); ...


6

For the templates WordPress uses, please always refer to Template hierarchy scheme in the Codex. As you can see there, single-{$posttype}-{$slug}.php does not exist, there is only single-{$posttype}.php. To do what you want, have a look at the filter 'single_template': add_filter( 'single_template', function( $template ) { global $post; if ( ...


6

A little preface here. I'm going to assume you know nothing of post types or templates since I'm unfamiliar with Underscores or the Teamtreehouse development track. Hopefully the below helps clear things up if not ask further in the comments. WordPress starts with a few built-in post types that you're already familiar with: Posts ( post type slug: page ) ...


5

WordPress uses a Template Hierarchy to determine which template file to load based on the current context: This diagram is a visual representation of \wp-includes\template-loader.php, that contains the context-based template-selection logic. As to your specific questions: For example, suppose I have two posts by the same author. How do I make WP use ...


5

So what's the best practice here? I would say a combination of letting the theme handle it and providing a default with your plugin. You can use the single_template filter to switch out the template. In your callback, see if the theme provided a template for the post type, if it did, do nothing. <?php add_filter('single_template', ...


5

A category page is an archive page. So is_archive() will return true on a category page. Try to push down is_category() further, or use is_category() before is_archive(). Like This if ( is_search() ) { // Search Result content } elseif( is_category() ) { // Category archive content } // check for tag, taxonomy, date before elseif ( is_archive() ) ...


5

get_header() accepts an argument, using it you can call a different headers. The only thing that get_header() does, is to include in the template where is called the file 'header.php' from child theme (if present) or from theme. If you use the argument $name, like so: get_header( $name), the function will look for a file named 'header-{$name}.php'. An ...


5

I wouldn't recommend using a taxonomy slug that coincides with the public query variables, like title. The title query variable was introduced in 4.4 so I think that could explain your problems. Check out this part of the WP_Query class: if ( '' !== $q['title'] ) { $where .= $wpdb->prepare( " AND $wpdb->posts.post_title = ...


5

The workhorse is WP_Theme::get_page_templates() (wrapped by the helper function get_page_templates()). If you check out the source, you'll see: /** * Filter list of page templates for a theme. * * @since 3.9.0 * @since 4.4.0 Converted to allow complete control over the `$page_templates` array. * * @param array $page_templates Array of page ...


4

There is no such hierarchy in Wordpress, accessing a custom taxonomy directly simply 404. This works for all taxonomies, build-in or not. There is unfortuantely no other work around to this but to create a page You should create a custom page template with a custom query to list your taxonomy terms, something like the following Create a page template ...


4

You have a couple of options here: taxonomy-{$taxonomy}-{$term->parent}-{$term}.php We can create our own hierarchy (or actually extends the existing hierarchy) by creating our own taxonomy-{$taxonomy}-{$term->parent}-{$term}.php template for use when a child term is being viewed. We will also make use of the taxonomy_template filter to add our new ...


4

+1ed Pieter Goosen's answer, but this time I want to be the one who advocate the "make it simple" way. In your taxonomy-earth.php template, you can check if the queried term as a parent, and require another template if so. If you ask this question, I guess you already have (or wish to create) 2 templates, let's call them: taxonomy-earth-continent.php ...


4

You can make use of the frontpage_template filter to adjust where your template should be loaded from add_filter( 'frontpage_template', function ( $template ) { $locate_template = locate_template( 'page-templates/front-page.php' ); if ( !$locate_template ) return $template; return $locate_template; });


3

I suggest creating 3 files 1) regiontemplate-country.php 2) regiontemplate-city.php These 2 will contain the templates for country & city, then 3) taxonomy-region.php In this file, add the code to load the appropriate template <?php $term = get_term_by('slug', get_query_var('term'), 'region'); if((int)$term->parent) ...


3

I'm pretty sure you could also do what you want by filtering template_include. This is super-untested, but maybe this can get you headed in the right direction: function wpse53871( $template ) { global $post; // check if is a Post and in the 'scott' category if( is_single( $post->ID ) && has_category( 'scott', $post ) ) { ...


3

Remove the cat parameter from your query and name your template file category.php From the WordPress Codex on Template Hierarchy; 1. category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php 2. category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php 3. category.php 4. ...


3

Create a file front-page.php with the following content: locate_template( 'category-image-gallery.php', TRUE, TRUE ); That’s all. For the theme’s functions.php If you want to restrict the front page content to posts from that category, filter the front page query: add_action( 'pre_get_posts', 'wpse_74225_frontpage_categories' ); function ...


3

wp-includes/template-loader.php is the file which is the template hierarchy. However, by the time that file executes, all the template decisions are already made since all that file does is check for boolean flags (defined in wp-includes/query.php function init_query_flags). So all the heavy-lifting and interesting code happens before that at ...


3

I'm going to answer my own question for the sake of people who might run in the same issue as me in my setup I had a plugin handling taxonomies; one of these had the rewrite slug set to "year" - well, it turns out this conflicts probably with date based archives (?) and caused my posts and pages not loading but rather redirecting the user to the blog home - ...


3

Following the Template Hierarchy image, I don't see such an option. So heres how i'd go about it: Solution 1 (Best in my opinion) Make a template file and associate it to the review <?php /* Template Name: My Great Review */ ?> Adding the template php file in your theme directory, it would appear as a template option in your post's edit ...


3

index.php is shown whenever wordpress doesn't find an appropriate page template. Generally people use it as the template for their site's blog. If you are absolutely sure that it will never be reached, you can leave it empty. Just make sure you have that file present, otherwise the theme will not work As a personal preference, when coming across this kind ...



Only top voted, non community-wiki answers of a minimum length are eligible