Custom layout for posts in a category.

Community Forums Forums General Discussion Custom layout for posts in a category.

This topic is: resolved

This topic contains 6 replies, has 3 voices, and was last updated by  Jen Baumann 1 year, 7 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #5167

    robskidmore
    Participant
    Post count: 3

    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.

    #5211

    John
    Participant
    Post count: 155

    Rob,

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

    #5214

    Jen Baumann
    Keymaster
    Post count: 323

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

     


    If you need technical support for your theme please file a ticket.

    The forums are community based. Staff only monitors the forum for issues relating to the forum itself and to redirect users to where they need to go.

    • This reply was modified 1 year, 7 months ago by  Jen Baumann.
    • This reply was modified 1 year, 7 months ago by  Jen Baumann.
    #5224

    John
    Participant
    Post count: 155

    Jen,

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

    #5225

    robskidmore
    Participant
    Post count: 3

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

    #5235

    robskidmore
    Participant
    Post count: 3

    Solved!!!!!!

     

    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.

     

    :-)

    #5236

    Jen Baumann
    Keymaster
    Post count: 323

    Glad you solved it.


    If you need technical support for your theme please file a ticket.

    The forums are community based. Staff only monitors the forum for issues relating to the forum itself and to redirect users to where they need to go.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Custom layout for posts in a category.’ is closed to new replies.