Tagged: x
-
AuthorPosts
-
January 5, 2017 at 6:06 pm #1318028
rekluseParticipantHello, 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!
January 6, 2017 at 12:02 am #1318435
Rue NelModeratorHello 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.
January 6, 2017 at 3:30 am #1318597
rekluseParticipantYep, 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!
January 6, 2017 at 3:38 am #1318606
rekluseParticipantAh, 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?
January 6, 2017 at 7:05 am #1318767
RadModeratorHi 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!
January 7, 2017 at 12:34 am #1319872
rekluseParticipantYou’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!
January 7, 2017 at 6:12 am #1320062
RadModeratorHi 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!
January 9, 2017 at 5:42 pm #1323095
rekluseParticipantThanks 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!
January 10, 2017 at 9:25 pm #1324958
LelyModeratorHi 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.
January 11, 2017 at 12:20 am #1325087
rekluseParticipantNailed it. Works perfectly now. Thank you so much, you guys are awesome as always.
January 11, 2017 at 4:31 am #1325247
Paul RModeratorYou’re welcome! π
February 9, 2017 at 3:06 am #1364018
SinesyParticipantHi 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
February 9, 2017 at 3:27 am #1364042
Rue NelModeratorHello 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.
February 10, 2017 at 3:08 am #1365653
SinesyParticipantThanks, 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.February 10, 2017 at 5:05 am #1365731
Paul RModeratorGlad you were able to figure it out. Have a nice day! π
-
AuthorPosts
- <script> jQuery(function($){ $("#no-reply-1318028 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>
