OnPoint Plugins

  • Plugins
    • Advanced Sidebar Menu
    • Go Live Update Urls
  • About
    • Services
    • Showcase
  • Blog
  • Contact
  • Support
  • Log In
← Back to support

Display Posts Under Categories in Categories Widget

Resolved

Requested: 4 years and 10 months ago

So I bought your Sidebar Menu Pro earlier today though I ran into an issue as I need the category posts under each category… Now, I see you wrote in one reply that this was not supported at this time, but I wonder, wouldn’t that be possible using taxonomies as a “dirty” workaround somehow?

Plugin: Advanced Sidebar Menu

  1. OnPoint Plugins

    4 years and 10 months ago

    There has now officially been  enough interest in this feature to add it to the upcoming features list. It is slated for version 3.2.0 of PRO.

    If the meantime, I have created a template-override to get this up and running.

     

    1. Create a new folder in your active theme called ‘advanced-sidebar-menu’
    2. Create a new file in this folder called ‘category_list.php’
    3. Add the following code to this file
    <?php
    
    /**
     * The Output of the Advanced Sidebar Categories Widget
     *
     * This edit list all of a categories posts under the category which displays in the menu
     * by default. It also support marking the current post as "current category" if on a single post page.
     */
    
    $menu        = Advanced_Sidebar_Menu_Menus_Category::get_current();
    $child_terms = $menu->get_child_terms();
    $content     = '';
    
    if ( ! class_exists( 'ASM_Posts_Walker' ) ) {
       class ASM_Posts_Walker extends Walker_Category {
          public function end_el( &$output, $page, $depth = 0, $args = array() ) {
             $current_post = get_post();
             $children     = get_posts( array(
                'orderby'       => 'title',
                'order'         => 'ASC',
                'numberposts'   => 100,
                $page->taxonomy => $page->slug,
             ) );
             if ( ! empty( $children ) ) {
                ob_start();
                ?>
                <ul class="children">
                   <?php
                   foreach ( $children as $_post ) {
                      $class = 'child-post cat-item';
                      if ( is_single( $current_post ) && ! empty( $current_post ) && $current_post->ID === $_post->ID ) {
                         $class .= ' current-cat';
                      }
                      ?>
                      <li class="<?php echo esc_attr( $class ); ?>">
                         <a
                            href="<?php echo esc_url( get_permalink( $_post ) ); ?>"
                            title="<?php echo esc_attr( $_post->post_title ); ?>">
                            <?php echo esc_html( $_post->post_title ) ?>
                         </a>
                      </li>
                      <?php
                   }
                   ?>
                </ul>
                <?php
                $output .= ob_get_clean();
             }
    
             parent::end_el( $output, $page, $depth, $args );
          }
       }
    }
    
    //Display parent category
    if ( $menu->include_parent() ) {
       $content .= '<ul class="parent-sidebar-menu">';
    
       $list_args = $menu->get_list_categories_args( 'parent' );
       $content   .= $menu->openListItem( wp_list_categories( $list_args ) );
    }
    
    if ( ! empty( $child_terms ) ) {
       $content .= '<ul class="child-sidebar-menu">';
    
       //Always display child categories
       if ( $menu->display_all() ) {
          $list_args           = $menu->get_list_categories_args( 'display-all' );
          $list_args['walker'] = new ASM_Posts_Walker();
          $content             .= wp_list_categories( $list_args );
    
       } else {
          foreach ( $child_terms as $_term ) {
             //Child terms
             if ( $menu->is_first_level_term( $_term ) ) {
                $list_args = $menu->get_list_categories_args( 'child', $_term );
                $content   .= $menu->openListItem( wp_list_categories( $list_args ) );
    
                //Grandchild terms
                if ( $menu->is_current_term_ancestor( $_term ) && $menu->has_children( $_term ) ) {
                   $content .= '<ul class="grandchild-sidebar-menu children">';
    
                   $list_args           = $menu->get_list_categories_args( 'grandchild', $_term );
                   $list_args['walker'] = new ASM_Posts_Walker();
                   $content             .= wp_list_categories( $list_args );
    
                   $content .= '</ul>';
                }
    
                $content .= '</li>';
             }
          }
       }
    
       $content .= '</ul><!-- End .child-sidebar-menu -->';
    }
    
    if ( $menu->include_parent() ) {
       $content .= '</li></ul><!-- End .parent-sidebar-menu -->';
    }
    
    return $content;
    
  2. erica

    4 years and 9 months ago

    Is there a way to make the plugin work for Portfolio type posts? Since Portfolio is built on a similar structure, I assume that with minor edits the same php should work. Is that accurate?

  3. OnPoint Plugins

    4 years and 9 months ago

    If you change the following lines:

    $children     = get_posts( array(
       'orderby'       => 'title',
       'order'         => 'ASC',
       'numberposts'   => 100,
       $page->taxonomy => $page->slug,
    ) );

    To:

    $children     = get_posts( array(
       'post_type'     => 'portfolio', //custom post type slug
       'orderby'       => 'title',
       'order'         => 'ASC',
       'numberposts'   => 100,
       $page->taxonomy => $page->slug,
    ) );

    It will display the custom post type’s posts which are assigned to category instead of “posts”.

    I also wanted to mention that the ability to select a post type will be part of version 3.2.0 which is slated to release this quarter.

     

  4. OnPoint Plugins

    4 years and 8 months ago

    Hello,

    I wanted to stop in to let everyone know that version 3.2.0 has officially been released with widget option to allow for displaying assigned posts or custom post types under categories. There are now options in the Category Widget to enable this functionality so filters are no longer required.

    Enjoy!

New Response Cancel reply

You must be logged in to respond. Only PRO members are allowed to respond to support requests.

© 2023 · WordPress Development · Terms Of Service · Privacy Policy · Cookie Policy · Log in

  • Plugins
  • Blog
  • Contact