Navigation

This content is old and no longer supported. You may use at your own risk, but no official support will be provided for anything listed here. If you are a Themeco customer, we invite you to check out our new community and forums, Apex.

  • Author
    Posts
  • #149369

    Frank19
    Participant

    I’m attempting to create a new sidebar that shows for all posts within a specific category. I created the sidebar and selected the category taxonomy from the options but for some reason it is not showing within that category on the front end. Am I missing something?

    #149821

    Zeshan
    Member

    Hi there,

    Thanks for writing in! To assist you with this issue, we’ll first need you to provide us with your URL as stated on the forum entrance page. This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you.

    Thanks!

    #149847

    Frank19
    Participant

    Sorry, here is my site URL:

    http://golficity.com/

    And here is an example of a post within the category where I am attempting to create a custom sidebar:

    http://golficity.com/golf-podcast-052-bounce-back-bad-round-of-golf/

    Thanks.

    #150309

    Rad
    Moderator

    Hi Frank,

    You mean each post should have the sidebar assigned from each category?

    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    Add this code at your child theme’s functions.php

    function post_category_add_meta_field() {
      // this will add the custom meta field to the add new term page
      ?>
      <div class="form-field">
        <label for="term_meta[post-category-sidebar]"><?php _e( 'Item\'s Sidebar', '__x__' ); ?></label>
        <select name="term_meta[post-category-sidebar]" id="term_meta[post-category-sidebar]"><?php echo post_category_sidebar_options(); ?></select>
        <p class="description"><?php _e( 'Select Sidebar','__x__' ); ?></p>
      </div>
    <?php
    }
    
    function post_category_edit_meta_field($term) {
     
      // put the term ID into a variable
      $t_id = $term->term_id;
     
      // retrieve the existing value(s) for this meta field. This returns an array
      $term_meta = get_option( "taxonomy_$t_id" ); ?>
      <tr class="form-field">
      <th scope="row" valign="top"><label for="term_meta[post-category-sidebar]"><?php _e( 'Item\'s Sidebar', '__x__' ); ?></label></th>
        <td>
        <select name="term_meta[post-category-sidebar]" id="term_meta[post-category-sidebar]"><?php echo post_category_sidebar_options( $term_meta['post-category-sidebar'] ); ?></select>
          <p class="description"><?php _e( 'Select Sidebar','__x__' ); ?></p>
        </td>
      </tr>
    <?php
    }
    
    add_action( 'post-category_edit_form_fields', 'post_category_edit_meta_field', 10, 2 );
    add_action( 'post-category_add_form_fields', 'post_category_add_meta_field', 10, 2 );
    
    function save_post_category_custom_meta( $term_id ) {
      if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
          if ( isset ( $_POST['term_meta'][$key] ) ) {
            $term_meta[$key] = $_POST['term_meta'][$key];
          }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
      }
    }  
    add_action( 'edited_post-category', 'save_post_category_custom_meta', 10, 2 );  
    add_action( 'create_post-category', 'save_post_category_custom_meta', 10, 2 );
    
    function post_category_sidebar_options ( $default ) {
      $sidebars = get_option( 'ups_sidebars' );
      $option = '<option value="sidebar-main" '.( !isset($default) || empty($default) || $default == 'sidebar-main' ? ' selected ' : '' ).'>Default</option>';
      foreach ( $sidebars as $id => $sidebar ) {
          $option .= '<option value="'.$id.'" '.( $id==$default ? ' selected ' : '' ).'>'.$sidebar['name'].'</option>';
      }
      return $option;
    }

    2. Copy wp-single.php from ▸ wp-content ▸ themes ▸ x ▸ framework ▸ views ▸ {STACK} to your child theme (eg. ▸ wp-content ▸ themes ▸ x-child-{STACK} ▸ framework ▸ views ▸ {STACK} )

    3. Edit your child theme’s wp-single.php and replace this line of code :

    <div class="<?php x_main_content_class(); ?>" role="main">

    with this :

    <div class="x-main right" role="main">

    and this one :

    <?php if ( $fullwidth != 'on' ) : ?>
          <?php get_sidebar(); ?>
        <?php endif; ?>

    with this :

    <aside class="x-sidebar left" role="complementary">
    
    <?php
    
    $terms = get_the_terms( get_the_ID(), 'category' );
    
    if ( !isset( $terms ) && count( $terms ) == 0 ) $sidebar = 'sidebar-main';
    else {
    
        $term = array_pop( $terms );
        $setting = get_option( "taxonomy_".$term->term_id );
        $sidebar = $setting['post-category-sidebar'];
    }
    
    dynamic_sidebar( $sidebar );
    
    ?>
    
    </aside>

    4. Save and upload.

    With this customization, it will allow you to set your custom sidebar per category. You may add or edit your category at Admin > Posts > Categories and set your sidebar. If there is no sidebar, then it will use main sidebar. Please also note that if your items has multiple category, then it will pickup the last category and use its sidebar.

    Hope this helps.

    #150480

    Frank19
    Participant

    Sorry, maybe I’m not fully understanding then. What then is the function of the taxonomies selection in the custom sidebar creation? I was under the impression that was how I could assign a sidebar to a certain category.

    #150939

    Zeshan
    Member

    Hi Frank,

    We’re sorry for the confusion! Selecting a category from “All Taxonomies” section will only enable the sidebar for that specific category page, it won’t be shown on the posts added to that category.

    To show the category on certain posts, you need to select them under All Pages and Posts (see: http://prntscr.com/599be3).

    Thanks!

    #195909

    Nicolas Z
    Participant

    Hi Support,

    I need to do exactly that :

    Hi Frank,

    You mean each post should have the sidebar assigned for each category?

    I tried your codes, but I have unfurtunately no sidebars on the posts, and my post content goes on the right side of the page.

    I am on a ethos theme, but on a local installation for now.

    Thanks for your help,

    Best regards.

    #196190

    Rad
    Moderator

    @Frank,

    As a follow up explanation. Sidebars that can be assigned from Admin > Appearance > Sidebars are displayable to where it’s assigned. The taxonomy selection will let you only assign a sidebar to specific taxonomy, like example, the category page (/category/music/) will have that sidebar, but not at the post that has music category.

    I’m attempting to create a new sidebar that shows for all posts within a specific category.

    As your initial request, you’re requesting for sidebar that shows for all posts within a specific category, then the provided customisation fits that. With that, the assigned sidebar for specific category will be the sidebar that will display to its posts (single post) too. So no need to assign sidebars one by one for each post.

    If you now decided just to assign a sidebar to a certain category, then you can ignore the provided customisation. And you can use Admin > Appearance > Sidebars for that.

    @Nicolaz, hmm I will need to do some testing at your end that. How about setting a live stage for testing? Have you check your layout if it’s allowed to have a sidebar? Have you created a sidebar at Admin > Appearance > Sidebar and assigned it at Admin > Posts > Categories?

    here is another related thread,
    https://theme.co/x/member/forums/topic/portfolio-menu/page/2/#post-134727 but for portfolio

    here is the updated code, with cleaning

    function post_category_add_meta_field() {
      // this will add the custom meta field to the add new term page
      ?>
      <div class="form-field">
        <label for="term_meta[category-sidebar]"><?php _e( 'Item\'s Sidebar', '__x__' ); ?></label>
        <select name="term_meta[category-sidebar]" id="term_meta[category-sidebar]"><?php echo post_category_sidebar_options(); ?></select>
        <p class="description"><?php _e( 'Select Sidebar','__x__' ); ?></p>
      </div>
    <?php
    }
    
    function post_category_edit_meta_field($term) {
     
      // put the term ID into a variable
      $t_id = $term->term_id;
     
      // retrieve the existing value(s) for this meta field. This returns an array
      $term_meta = get_option( "taxonomy_$t_id" ); ?>
      <tr class="form-field">
      <th scope="row" valign="top"><label for="term_meta[category-sidebar]"><?php _e( 'Item\'s Sidebar', '__x__' ); ?></label></th>
        <td>
        <select name="term_meta[category-sidebar]" id="term_meta[category-sidebar]"><?php echo post_category_sidebar_options( $term_meta['category-sidebar'] ); ?></select>
          <p class="description"><?php _e( 'Select Sidebar','__x__' ); ?></p>
        </td>
      </tr>
    <?php
    }
    
    add_action( 'category_edit_form_fields', 'post_category_edit_meta_field', 10, 2 );
    add_action( 'category_add_form_fields', 'post_category_add_meta_field', 10, 2 );
    
    function save_post_category_custom_meta( $term_id ) {
      if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
          if ( isset ( $_POST['term_meta'][$key] ) ) {
            $term_meta[$key] = $_POST['term_meta'][$key];
          }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
      }
    }  
    add_action( 'edited_category', 'save_post_category_custom_meta', 10, 2 );  
    add_action( 'create_category', 'save_post_category_custom_meta', 10, 2 );
    
    function post_category_sidebar_options ( $default ) {
      $sidebars = get_option( 'ups_sidebars' );
      $option = '<option value="sidebar-main" '.( !isset($default) || empty($default) || $default == 'sidebar-main' ? ' selected ' : '' ).'>Default</option>';
      foreach ( $sidebars as $id => $sidebar ) {
          $option .= '<option value="'.$id.'" '.( $id==$default ? ' selected ' : '' ).'>'.$sidebar['name'].'</option>';
      }
      return $option;
    }
    

    Code works on my end 🙂

    Thanks!

    #196581

    Nicolas Z
    Participant

    Yes it’s working great ! Without the wp-single.php modifications !

    Thanks support 🙂 !

    Have a nice day.

    #196815

    Christian
    Moderator

    You’re welcome Nicolas.

    #209250

    Anonymous
    This reply has been marked as private.
    #209454

    John Ezra
    Member

    Hi Paul,

    Thanks for writing in. Regretfully this isn’t a feature offered by X. It could be possible with custom development, but this would be outside the scope of support we can offer.

    You can just use the custom sidebars and leave the main sidebar blank. This will keep the rest of the site without a working sidebar, but the sidebar container will still be present. (it will be blank and the main content will not be centered on page)

    Thanks for understanding. Take care!

    #211024

    Anonymous

    Hi Support,

    That’s something I was a afraid of. Thanks for your help !

    Is there a way to go around this as, for example :
    – set up the default page not to display a sidebar
    – change the layout of the concerned pages so that the sidebar does not show up

    #211459

    Rad
    Moderator

    Hi Paul,

    What do you mean default page? The page, post, or the entire site? How about setting your layout at customizer and choose full-width, then just use page templates to use sidebars or with no sidebars? And you can do that through page template at your current page.

    Though, I’m not really sure and could be wrong from my understanding, please provide more details about this 🙂

    #213788

    Anonymous

    Hi,

    Good day! I hope it is alright to reply to this thread, if not, please let me know and I will create a new one. I searched for existing thread regarding this matter and so far I have not found any.

    How come I cannot assign a specific sidebar for a category?

    The taxonomy selection will let you only assign a sidebar to specific taxonomy, like example, the category page (/category/music/) will have that sidebar, but not at the post that has music category.

    I created a unique sibebar and assigned it to a category using the ‘Taxonomies’ area. When I checked the category/archive page it’s not showing the unique sidebar.

    Now I tried placing the uniques sidebar to a post and page type using the ‘posts and pages’ area, it is successful.

    I also tried placing the unique sidebar to a different category, but it is not showing it too.

    I checked the category editor and there is no option to choose a template. Did I misunderstood the ‘Taxonomies’ thing? I checked the name of the category i am assigning it to and it is on the list.

    Thank you for the help!

Join Apex

This content is old and no longer supported. We invite you to check out our new community and forums, Apex.

Join Apex