Tag Info

Hot answers tagged

17

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


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


8

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


5

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


4

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


4

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


4

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


4

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


4

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


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


3

The template hierarchy does not provide for archive index pages. This is an issue that was raised through a trac ticket, but that idea was scrapped and will never be implemented due to different theme stuctures across the board So the following will never exists natively Pages that lists all posts from a given taxonomy Pages that lists posts from all ...


3

First check that there isn't a page set as a front page in "Reading Setting". If that is set, index.php is not used as the home page, but the template of the page you set as front page. In that case, you need to find that template and add you conditional is there. <?php if(is_user_logged_in()) { get_header(); ?> <div id="primary" ...


3

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


3

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


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

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

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

You could hook into template_include and return your plugin file if the request is for your post type: add_filter( 'template_include', 'insert_my_template' ); function insert_my_template( $template ) { if ( 'my_post_type' === get_post_type() ) return dirname( __FILE__ ) . '/template.php'; return $template; } But this will change the look ...


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

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


2

Take a look at the Codex: taxonomy-{taxonomy}-{term}.php - If the taxonomy were sometax, and taxonomy's term were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is 'post_format' and the terms are post-format-{format}. i.e. taxonomy-post_format-post-format-link.php ...


2

Did you save the permalink after creating custom post type Take a look at codex Custom Post Type Templates


2

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


2

One of the easiest (although not the only) of ways to achieve this is by creating a custom options panel in the WP dashboard that will allow your client to create and update information that can be used through out your template files with no technical knowledge being necessary. You can either paste the following directly into your functions.php file or ...


2

There is no default rewrite to a template for a category or taxonomy landing page. You are correct, that this can be done by creating a custom template and applying it to a page.


2

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


2

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' ); // ...


2

When you make a page the page for posts, it's no longer a "page" in the conventional sense. Refer to the Template Hierarchy for home page display- the quickest fix is to rename your blog template home.php, and WordPress will load that before index.php.


2

Got it. I had tried to use flush_rewrite_rules() on plugin activation/deactivation, as so: function creativeworks_activate() { // register taxonomies/post types here flush_rewrite_rules(); } function creativeworks_deactivate() { flush_rewrite_rules(); } register_activation_hook( __FILE__, 'creativeworks_activate' ); register_deactivation_hook( ...


2

add_filter( 'template_include', 'wpsites_cpt_archive_page_template', 99 ); function wpsites_cpt_archive_page_template( $template ) { if ( is_post_type_archive() ) { $new_template = locate_template( array( 'your-cpt-template.php' ) ); if ( '' != $new_template ) { return $new_template ; } } return $template; ...



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