Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1318028
    rekluse
    Participant

    Hello, I am trying to figure out if it’s possible to exclude specific categories from the Post Slider on the blog page. Here is the issue I am having:

    I have two different sections on my site to display two different Post types.

    Page A (the “blog” page) has a Post Slider set to randomly display recent posts. (http://everyoneisright.com/perspectives/)

    Page B does not have a Post Slider at all. (http://everyoneisright.com/practice/)

    I have Page A set up to only display posts with certain categories, which I manage with the Index Filter. Page B contains posts in other categories.

    However, the Post Slider on Page A is displaying results for ALL categories, whether they belong on Page A or Page B. I would like it to exclude items that I’m designating for Page B.

    So, is it possible to either:

    a) Limit the Page A Post Slider to only draw from posts that are being displayed via the filterable index, or

    b) Exclude specific categories (even if it’s just one specific category) from the Page A Post Slider, without also affecting the Post Sliders used in our archives?

    I hope this is clear!

    #1318435
    Rue Nel
    Moderator

    Hello There,

    Thanks for writing in! What you are trying to accomplish requires a template customization, we would like to suggest that you use 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.

    Once you have your child theme active and ready, please follow the following steps below:
    1] Using Notepad or TextEdit or Sublime Text or any text editor, please create a new file in your local machine.
    2] Insert the following code into that new file

    <?php
    
    // =============================================================================
    // VIEWS/ETHOS/_POST-SLIDER.PHP
    // -----------------------------------------------------------------------------
    // Outputs the post slider that appears at the top of the blog.
    // =============================================================================
    
    $is_blog    = is_home();
    $is_archive = is_category() || is_tag();
    
    if ( $is_blog || $is_archive ) :
    
      if ( $is_blog ) {
        $info = array( 'blog', NULL, NULL, '_x_ethos_post_slider_blog_display' );
      } elseif ( $is_archive ) {
        $type = ( is_category() ) ? 'cat' : 'tag_id';
        $info = array( 'archive', $type, get_queried_object_id(), '_x_ethos_post_slider_archives_display' );
      }
    
      $slider_enabled = x_get_option( 'x_ethos_post_slider_' . $info[0] . '_enable' ) == '1';
      $count          = x_get_option( 'x_ethos_post_slider_' . $info[0] . '_count' );
      $display        = x_get_option( 'x_ethos_post_slider_' . $info[0] . '_display' );
    
      $blog_slider_is_enabled    = $slider_enabled && $is_blog;
      $archive_slider_is_enabled = $slider_enabled && $is_archive;
      $is_enabled                = $blog_slider_is_enabled || $archive_slider_is_enabled;
    
      switch ( $display ) {
        case 'most-commented' :
          $args = array(
            'post_type'      => 'post',
            'posts_per_page' => $count,
            'orderby'        => 'comment_count',
            'order'          => 'DESC',
            $info[1]         => $info[2]
          );
          break;
        case 'random' :
          $args = array(
            'post_type'      => 'post',
            'posts_per_page' => $count,
            'orderby'        => 'rand',
            $info[1]         => $info[2]
          );
          break;
        case 'featured' :
          $args = array(
            'post_type'      => 'post',
            'posts_per_page' => $count,
            'orderby'        => 'date',
            'meta_key'       => $info[3],
            'meta_value'     => 'on',
            'ignore_sticky_posts'	=> true
          );
          break;
      }
    
      ?>
    
      <?php if ( $is_enabled ) : ?>
    
        <div class="x-flexslider x-post-slider">
          <ul class="x-slides">
    
            <?php $args['cat'] = x_get_option( 'x_ethos_filterable_index_categories' ); ?>
    
            <?php $wp_query = new WP_Query( $args ); ?>
    
            <?php if ( $wp_query->have_posts() ) : ?>
              <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    
                <li class="x-slide">
                  <article <?php post_class( 'x-post-slider-entry' ); ?> style="<?php echo x_ethos_entry_cover_background_image_style(); ?>">
                    <a href="<?php the_permalink(); ?>">
                      <div class="cover">
                        <div class="middle">
                          <span class="featured-meta"><?php echo x_ethos_post_categories(); ?> / <?php echo get_the_date( 'F j, Y' ); ?></span>
                          <h2 class="h-featured"><span><?php x_the_alternate_title(); ?></span></h2>
                          <span class="featured-view"><?php _e( 'View Post', '__x__' ); ?></span>
                        </div>
                      </div>
                    </a>
                  </article>
                </li>
    
              <?php endwhile; ?>
            <?php endif; ?>
    
            <?php wp_reset_query(); ?>
    
          </ul>
        </div>
    
        <script>
          jQuery(window).load(function() {
            jQuery('.x-post-slider').flexslider({
              controlNav   : false,
              selector     : '.x-slides > li',
              prevText     : '<i class="x-icon-chevron-left" data-x-icon=""></i>',
              nextText     : '<i class="x-icon-chevron-right" data-x-icon=""></i>',
              animation    : 'fade',
              smoothHeight : true,
              slideshow    : true
            });
          });
        </script>
    
      <?php endif; ?>
    
    <?php endif; ?>

    3] Save the file named as _post-slider.php
    4] Upload this file to your server in the child theme’s folder
    wp-content/themes/x-child/framework/views/ethos/

    This code should limit the Page A Post Slider to only draw from posts that are being displayed via the filterable index.

    We would loved to know if this has work for you. Thank you.

    #1318597
    rekluse
    Participant

    Yep, totally worked. Thank you so much!

    Followup question — can I easily modify this code so the slider displays tags instead of categories, and maybe even removing the publishing date?

    Again, thank you for the support!

    #1318606
    rekluse
    Participant

    Ah, wait, actually there is an issue — it seems that this code has deactivated the random mode in both Firefox and Chrome. It is now showing the same selections with every page refresh. Seems to work in Safari though. Is there an modification I should make?

    #1318767
    Rad
    Moderator

    Hi there,

    It shouldn’t be browser specific since the query runs on the server and not on the browser. Would you mind providing your site’s admin and FTP login credentials in private reply? I’ll have to check it first πŸ™‚

    Thanks!

    #1319872
    rekluse
    Participant

    You’re right, the problem isn’t with this code at all — I actually seem to be having a conflict with Autoptimize, and it only seems to be affecting logged-out users. When I reset the Autoptimize cache it offers a new selection of random slides, but it keeps the same slides across all browsers until I reset the cache again. Again, logged-out view works totally fine, which is strange. Still looking into it.

    Let me know if you have any advice here, otherwise we will keep trying to figure out what’s going on.

    Thanks again!

    #1320062
    Rad
    Moderator

    Hi there,

    Make sure your hosting doesn’t have any active cache feature too. Multiple cache is a common issue, it’s like caching the already cached resources.

    Thanks!

    #1323095
    rekluse
    Participant

    Thanks Rad, we are looking into that.

    In the meantime I had another followup question.

    The code you gave me seems to work perfectly for the main Perspectives page.

    However, it is causing our archive pages to behave differently.

    It used to be that when looking at a category page (e.g. https://everyoneisright.com/category/topics/education/) the slider would only display posts that were actually in that category.

    But since I modified the code with the new code you gave me, the archive category pages are now showing the same results they would on the main blog page ((e.g. https://everyoneisright.com/perspectives/)

    Tag-based index pages, however, are still working perfectly (https://everyoneisright.com/tag/art-gallery/)

    Any idea how I might tweak the code further so the category index pages behave as they should?

    Thanks again!

    #1324958
    Lely
    Moderator

    Hi There,

    Please look for this line of code on line 66:

    <?php $args['cat'] = x_get_option( 'x_ethos_filterable_index_categories' ); ?>
    Update that code into this:

    
    <?php if ( $is_blog ) { $args['cat'] = x_get_option( 'x_ethos_filterable_index_categories' ); }?>

    Hope this helps.

    #1325087
    rekluse
    Participant

    Nailed it. Works perfectly now. Thank you so much, you guys are awesome as always.

    #1325247
    Paul R
    Moderator

    You’re welcome! πŸ™‚

    #1364018
    Sinesy
    Participant

    Hi There,
    I followed all the previous posts but differently I would like to be able to exclude a specific category by the Post Slider for Blog. It’s possible?

    Thank you for the support

    #1364042
    Rue Nel
    Moderator

    Hello There,

    Thanks for updating this thread. You will be able to follow the instructions above which should work for you. And if you would like to exclude a particular category, you will just have to add an exclude category parameter like: 'category__not_in' => array( 2, 6 ) ) wherein the 2 and 6 is the category ID. To find out how to get the category ID, please check it here: https://community.theme.co/kb/how-to-locate-category-ids/

    A snippet taken from the code above with the exclude category parameter:

    $args = array(
            'post_type'      => 'post',
            'posts_per_page' => $count,
            'category__not_in' => array( 2, 6 ) ),
            'orderby'        => 'comment_count',
            'order'          => 'DESC',
            $info[1]         => $info[2]
          );

    To know more about the category parameter in the WP query, please check it here: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    Hope this make sense.

    #1365653
    Sinesy
    Participant

    Thanks, I tried to add yours code but it generated error. I resolved the problem in this way:

    'category__not_in' => array( 2, 6 ),

    Now works fine.
    Thank you so much.

    #1365731
    Paul R
    Moderator

    Glad you were able to figure it out. Have a nice day! πŸ™‚

  • <script> jQuery(function($){ $("#no-reply-1318028 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>