Archived topic
Custom template for Single post
8 replies · Started by Rodrigo on October 9, 2016
Hi Tom,
How can I create a custom single post template for a specific category?
In single posts within a specific category I'd like to change the featured image smaller and to the right and put the title and content next to it.
That specific post is just going to have a paragraph description and a player for podcast.
Is there a plugin or somehow I can do that?
You may not need a custom template.
You would need to add the category class to the single post though with a function like this:
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes, $class) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array
return $classes;
}
Adding PHP: https://generatepress.com/knowledgebase/adding-php-functions/
Then you could target that featured image like this:
.your-category-name .page-header-image-single {
max-width: 300px;
float: right;
}
Adding CSS: https://generatepress.com/knowledgebase/adding-css/
That should get you started :)
Uh WOW, that worked perfectly well...
I might need a little bit of help, if you can.
How can I put the image below the title, so that way the title size will be 100% of the content area?
You should be able to do that in "Customize > Layout > Page Header" by changing "Single Post Page Header" to "Below title".
Let me know :)
The thing is I only need that image position in one unique category posts. On the other categories, image's posts should be above any element, including post title.
That's a bit harder, but you should be able to do this:
add_action( 'after_setup_theme','tu_category_specific_page_header' );
function tu_category_specific_page_header() {
// Only do this if we're in category IDs 1, 2 and 3
if ( in_category( array( 1,2,3 ) ) ) {
remove_action('generate_after_header','generate_page_header_single_above', 10);
add_action('generate_after_entry_header','generate_page_header_single_below_title', 10);
}
}
I guess this is solved with the new Elements module on GP, right?
Which part, specifically?
Hello Tom. I'm just gonna close this thread because I can't even remember what site I was talking about here (and if is still on my hands, which I don't think so) and, honestly, I don't like to see the topic open. You know, OCD issues... n_n