go41

single.php ‚post-format‘ template for builtin Post Formats

von Joern am 17. Sep. 2012 | Keine Kommentare

The problem: I need a single.php for one of the built in post formats like ‚aside‘, ‚gallery‘, ‚link‘, ‚video‘, ‚image‘ or ‚quote‘.

My theme was calling the content-single.php Post Format in it’s original single.php like <?php get_template_part( ‚content‘, ’single‘ ); ?>

and I could use

<?php get_template_part( ‚content‘, get_post_format() ); ?>

to load the required post-format like content-aside.php into it to display a modified content.

But like this there is still single.php template loading the default header.php, sidebar.php and footer.php.
But I want to customize just single.php to not load the sidebar in when displaying a post in the format ‚aside‘.
In WordPress Codex for Template Hierarchy is mentioned a template:

‚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‘

So I tried ‚taxonomy-post_format-post-format-aside.php‘ as template for a single.php for the Post Format ‚aside‘ ..

Didn’t work for single post, is made for Custom Taxonomies display of an archive ..
Finally the solution:

There is a filter to add to functions.php that uses your ‚taxonomies‘ or ‚post_format‘ and lets you make a single-‚post_format‘.php file.
Adding the following code to your functions.php will enable you to use single-post-format-‚post_format‘.php templates like single-post-format-aside.php or single-post-format-video.php
add_filter(’single_template‘, ’single_template_terms‘);
function single_template_terms($template) {
foreach( (array) wp_get_object_terms(get_the_ID(), get_taxonomies(array(‚public‘ => true, ‚_builtin‘ => true))) as $term ) {
if ( file_exists(TEMPLATEPATH . "/single-{$term->slug}.php") )
return TEMPLATEPATH . "/single-{$term->slug}.php";
}
return $template;
}
Having the above filter-function in your theme’s functions.php you can use single post templates with a name like stated above.
The filter function is a modified version of a filter you find here:

Single post template for custom taxonomies

The filename for this custom-single.php I found under ‚Custom Taxonomies display‘ in the WordPress Codex:

‚the taxonomy is ‚post_format‘ and the terms are ‚post-format-{format}‘
____________________
you find me on Google+, Twitter and Facebook

(von: Joern)

Sorry, no posts matched your criteria.

Autor:

Du findest mich auch auf Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

weitere forum Beiträge: