Skip Navigation

[Closed] Use different template with header depending on post category

This support ticket is created 8 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.14 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Beda 8 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#342987

I am trying to: use a different template depending on post category. I have 4 different venues within one website, all having their own template and header. But when I view a post I want it to pick up the specific venue header i.e template.

I tried using the following code, but didn't work, Is there an easier way to do this with shortcodes?

add_action('template_include', 'load_single_template');
  function load_single_template($template) {
    $new_template = '';

    // single post template
    if( is_single() ) {
      global $post;
      // 'cat-1' and 'cat-2' are category slugs

      if( has_term('cat-1', 'category', $post) ) {
        // use template file single-template-cat-1.php
        $new_template = locate_template(array('single-template-cat-1.php' ));
      }

      if( has_term('cat-2', 'category', $post) ) {
        // use template file single-template-cat-2.php
        $new_template = locate_template(array('single-template-cat-2.php' ));
      }

    }
    return ('' != $new_template) ? $new_template : $template;
  }

I visited this URL:

I expected to see:

Instead, I got: the default template

#343038

Thank you for contacting us here in the Support Forum

This your code and goal dose not use Toolset API, GUI or features.

I can help you do this as example with Layouts Plugin.
Or by using CRED forms, and performing a similar action but assigning a certain Layout to the Post on CRED submit.

The Code you use is using only WP API and the right place to get help for it would be the WP Support Forum:
https://wordpress.org/support/

If you want to use the Layouts Plugin and have a certain Layout of your choice assigned to a certain Post depending on Taxonomy, you can use a Code similar to the Below:

function assign_layout_by_post_status( $id, $layout ){
 
        global $post;
 
        $preferred_layout = 12345; // my preferred layout ID
 
        $control_layout = 0; //just set to 0 if you don't have any layout set for your post yet

        if( $id === $control_layout && $post->post_type === 'your_type'){
            if (has_term( 'cat-1', 'category', $post)) {
                return $preferred_layout;
            }
        }
        return $id;
    }
 
add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

The topic ‘[Closed] Use different template with header depending on post category’ is closed to new replies.