Community Forums › Forums › Archived Forums › General Discussion › Custom layout for posts in a category.
Tagged: Category post, Custom post, Site layout
- This topic has 6 replies, 3 voices, and was last updated 11 years, 3 months ago by Jen Baumann.
-
AuthorPosts
-
December 13, 2012 at 11:07 pm #5167robskidmoreMember
I've spent two days trying to figure this out.
What I'm trying to do
When I put posts in a certain category I want them to be displayed differently.
The Method
This is what I have so far. I created my custom template and added it as writing.php. I added the following code to my functions.php. This successfully brought out my custom template but is also added the code from the single.php file. I know because I added code to the single.php and was able to make changes to the post.
/** Add the custom writing template */add_action( 'genesis_after_header', 'minimum_writing_category', 15 );
function minimum_writing_category() {
if (in_category ( 'writing')) {
require_once( get_stylesheet_directory() . '/writing.php' );
}
}
My Question
How do I remove the call to the single.php? I've scoured the interwebz and these forums for hours to no avail.
If someone can help I would greatly appreciate it.
Thanks Rob.
December 14, 2012 at 9:36 am #5211JohnParticipantRob,
Here's how I would do that:
1. Put your custom template inside a function instead of in writing.php, and remove writing.php all together. In this example I'm calling that function writing_content_template
2. Add that new function to functions.php, or better yet, to a custom-functions plugin in your mu-plugins directory, which you can create in wp-content if it's not there already. The benefit of mu-plugins is that it stays with the site even if the theme changes.
3. In single.php add a conditional for that category that removes the post content and replaces it with your custom template. Something like this:
if( in_category ( 'writing' ) ) {
remove_action( 'genesis_post_content', 'genesis_do_post_content' );
add_action( 'genesis_post_content', 'writing_content_template' );
}I haven't tested this specifically with a category, but I have done something very similar with displaying different custom post types with different content templates. And by putting that template in a function you can call it wherever you need it, in multiple page templates, for example.
John
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google 😉December 14, 2012 at 9:49 am #5214Jen BaumannParticipantThat assumes that you copy single.php to the child theme. Don't edit Genesis single.php. You could also try it in functions.php and use
if( is_single() && in_category( 'writing') )
note that should be && not the html...grrr.
December 14, 2012 at 10:48 am #5224JohnParticipantJen,
Right. I was assuming that the modified single.php would be in the child theme directory, but thanks for mentioning that.
RE the code posting frustration, I just started a thread in Forum Bugs and Suggestions with a possible solution. I know it's up to StudioPress to implement it, but more voices could help the cause.
John
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google 😉December 14, 2012 at 10:55 am #5225robskidmoreMemberHey guys thanks for the responses.
I probably should have made it clear what code I was trying to add to my template. This is what I am trying to do.
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
remove_action( 'genesis_after_post', 'genesis_do_author_box_single' );
remove_action ( 'genesis_after_header', 'minimum_page_title');
remove_action( 'genesis_after_header', 'genesis_do_nav' );
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
remove_action( 'genesis_after_header', 'genesis_do_breadcrumbs' );
remove_action( 'genesis_before_footer', 'genesis_footer_widget_areas' );
remove_action( 'genesis_footer', 'minimum_custom_footer', 6 );
remove_action( 'genesis_footer', 'genesis_footer_markup_open', 5 );
remove_action( 'genesis_footer', 'genesis_do_footer' );
remove_action( 'genesis_footer', 'genesis_footer_markup_close', 15 );
I've tried variations of both of your suggestions.First
I added this to the funtions.php still trying to use my writing.php template.
/** Add the custom writing template */add_action( ‘genesis_after_header’, ‘minimum_writing_category’, 15 );
function minimum_writing_category() {
if (is_single() && in_category ( ‘writing’)) {
require_once( get_stylesheet_directory() . ‘/writing.php’ );Â }}
Did not work. It removed all the changes that I had made with my original code.
Second
I tried adding the whole thing to my functions.php
/** Add the custom writing template */add_action( ‘genesis_after_header’, ‘minimum_writing_category’, 15 );
function minimum_writing_category() {
if (is_single() && in_category ( ‘writing’)) {
...My code... Â }}
No luck.
Third
I removed the the first two attempts and added this to single.php (   in my child theme 🙂   )
if (in_category ( ‘writing’)) {remove_action( 'genesis_post_content', 'genesis_do_post_content');
add_action( 'genesis_post_content', 'writing_content_template');function writing_content_template () { ...My code... }}
Still nothing
Not sure where I'm going wrong. I got the best results from my original code it was just calling two templates and so I had duplicate content on the page.
Appreciate your help guys.
Any other suggestions.
December 14, 2012 at 11:56 am #5235robskidmoreMemberSolved!!!!!!
Like most things the answer was rather simple.
I removed the writing.php and added this code to my single.php.
if( is_singular() && in_category('writing')) {add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
remove_action( 'genesis_after_post', 'genesis_do_author_box_single' );
remove_action ( 'genesis_after_header', 'minimum_page_title');
remove_action( 'genesis_after_header', 'genesis_do_nav' );
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
remove_action( 'genesis_after_header', 'genesis_do_breadcrumbs' );
remove_action( 'genesis_before_footer', 'genesis_footer_widget_areas' );
remove_action( 'genesis_footer', 'minimum_custom_footer', 6 );
remove_action( 'genesis_footer', 'genesis_footer_markup_open', 5 );
remove_action( 'genesis_footer', 'genesis_do_footer' );
remove_action( 'genesis_footer', 'genesis_footer_markup_close', 15 );}
Thanks friends. Couldn't have done it without you.
🙂
December 14, 2012 at 11:59 am #5236Jen BaumannParticipantGlad you solved it.
-
AuthorPosts
- The topic ‘Custom layout for posts in a category.’ is closed to new replies.