38

Front page logic is one of the most confusing features in WordPress and is exceptionally hard to explain and summarize. As mentioned in comment while back I burnt unholy amount of time to put together my front page logic cheat sheet for it. But since this is a popular thread let me try to answer those very specific questions you had. What's the ...


28

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 ...


18

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 -- ie, ...


16

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 ...


15

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.


14

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. ...


13

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 { ...


13

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 ...


11

It's actually very easy, add follow code to your functions.php add_filter( 'page_template', function ($template) { global $post; if ($post->post_parent) { // get top level parent page $parent = get_post( reset(array_reverse(get_post_ancestors($post->ID))) ); // ...


10

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 ...


10

The WooCommerce template files are different from the WordPress Template files look at this to see how it works and the template file for shop pages is archive-product.php Usually, all themes provide a separate sidebar area for the shop page, did you check if your theme is compatible with WooCommerce? If yes then you should have a sidebar available under ...


9

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 ( $post-...


8

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', '...


8

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(); ...


7

As I answered here, since WordPress 4.7 Post-Type-Templates are enabled in the WordPress core. That means that you can create multiple templates for the single post-type view. You create these templates like you would create a normal page template. But you need to add a little more code to these templates: /* Template Name: Funerals Video Template ...


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

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 - ...


5

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) get_template_part('...


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 the ...


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

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 ...


5

+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 ...


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 ...


5

If you are using custom WooCommerce template overrides in your theme you need to declare WooCommerce support using the add_theme_support function. WooCommerce template overrides are only enabled on themes that declare WooCommerce support. If you do not declare WooCommerce support in your theme, WooCommerce will assume the theme is not designed for ...


5

As you can see in the hierarchy flowchart on the page that you linked to, Site Front Page will choose front-page.php before falling back to behaving like a normal page: So if you want to have a specific template for the front page template of your theme you need to make those changes to front-page.php, or delete front-page.php so that it behaves like a ...


4

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 ) ) { ...


4

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 ...


4

the_content() does not grab the PHP file content.php, it simply displays a Post's content. Likewise, the_excerpt() grabs the excerpt of a post. get_template_part simply finds a file within your theme named whatever you put in, with an optional suffix. get_template_part( 'content' ); // content.php get_template_part( 'content', 'my_page' ); // content-...


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