Hide Category and display random order in Slider

Good Morning,

I would like to both hide one category (called Latest) in my Activello slider and also have the posts that do appear in the slider show up in a random order (I believe the default is descending beginning with the latest post). I found code to add both options to my functions.php file by searching other posts and replies in this forum. However, when I add the combined code to my functions.php file, the slider does not work. I attached a copy of my child theme functions.php file.
My website is www.dawsongang.com

I love this theme and thanks for your help.

I see that the functions.php did not load. Here is a copy of the file that I am using in my activello-child theme:

<?php

// Queue parent style followed by child/customized style
add_action( 'wp_enqueue_scripts', 'func_enqueue_child_styles', 99);

function func_enqueue_child_styles() 
{
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_dequeue_style('activello-style');
    wp_enqueue_style( 'activello-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('parent-style')
    );
}
function custom_the_category_list($categories){
    if ( !empty( $categories ) ) {

        foreach ( $categories as $key => $category ) 
{
            if( $category->name == 'latest' ){
                unset($categories[$key]);
            }
        }
    }
    return   $categories;
}
function activello_featured_slider() {
  if ( ( is_home() || is_front_page() ) && get_theme_mod( 'activello_featured_hide' ) == 1 ) 
{
		wp_enqueue_style( 'flexslider-css' );
		wp_enqueue_script( 'flexslider-js' );
		
    echo '<div class="flexslider">';
      echo '<ul class="slides">';

        $count = 6;
        $slidecat = get_theme_mod( 'activello_featured_cat' );

        $query = new WP_Query( array( 'cat' => $slidecat,'posts_per_page' => $count, 'orderby' => 'rand' ) );
        if ($query->have_posts()) :
          while ($query->have_posts()) : $query->the_post();
                
            if ( (function_exists( 'has_post_thumbnail' )) && ( has_post_thumbnail() ) ) :

                echo '<li>';
                      echo get_the_post_thumbnail( get_the_ID(), 'activello-slider' );

                    echo '<div class="flex-caption">';
                      echo get_the_category_list();
                        if ( get_the_title() != '' ) echo '<a href="' . get_permalink() . '"><h2 class="entry-title">'. get_the_title().'</h2></a>';
                        echo '<div class="read-more"><a href="' . get_permalink() . '">' . __( 'Read More', 'activello' ) .'</a></div>';
                    echo '</div>';

                echo '</li>';
            endif;

        endwhile; 
        wp_reset_query();
        endif;

      echo '</ul>';
    echo ' </div>';
  }
}

Hi @sunnyw,

I hope you are well today and thank you for your question.

Please use the below modified code in the functions.php file of your child theme instead of your shared code.


<?php

// Queue parent style followed by child/customized style
add_action( 'wp_enqueue_scripts', 'func_enqueue_child_styles', 99);

function func_enqueue_child_styles() 
{
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_dequeue_style('activello-style');
    wp_enqueue_style( 'activello-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('parent-style')
    );
}
function custom_the_category_list($categories){
    if ( !empty( $categories ) ) {

        foreach ( $categories as $key => $category ) 
{
            if( $category->name == 'Latest' ){
                unset($categories[$key]);
            }
        }
    }
    return   $categories;
}
function activello_featured_slider() {
  if ( ( is_home() || is_front_page() ) && get_theme_mod( 'activello_featured_hide' ) == 1 ) 
{
		wp_enqueue_style( 'flexslider-css' );
		wp_enqueue_script( 'flexslider-js' );
		
    echo '<div class="flexslider">';
      echo '<ul class="slides">';

        $count = 6;
        $slidecat = get_theme_mod( 'activello_featured_cat' );

        $query = new WP_Query( array( 'cat' => $slidecat,'posts_per_page' => $count, 'orderby' => 'rand' ) );
        if ($query->have_posts()) :
          while ($query->have_posts()) : $query->the_post();
                
            if ( (function_exists( 'has_post_thumbnail' )) && ( has_post_thumbnail() ) ) :

                echo '<li>';
                      echo get_the_post_thumbnail( get_the_ID(), 'activello-slider' );

                    echo '<div class="flex-caption">';
                        add_filter('the_category_list', 'custom_the_category_list');
                      echo get_the_category_list();
                      remove_filter('the_category_list', 'custom_the_category_list');
                        if ( get_the_title() != '' ) echo '<a href="' . get_permalink() . '"><h2 class="entry-title">'. get_the_title().'</h2></a>';
                        echo '<div class="read-more"><a href="' . get_permalink() . '">' . __( 'Read More', 'activello' ) .'</a></div>';
                    echo '</div>';

                echo '</li>';
            endif;

        endwhile; 
        wp_reset_query();
        endif;

      echo '</ul>';
    echo ' </div>';
  }
}

Best Regards,
Movin

Hi Movin,

Thanks for your help. After deleting my functions.php and replacing it with a new functions.php with your code from above, the slider re-appeared briefly. The “Latest” Category did not come up which was good, but only one random slide appeared and there was no navigation to change slides. The page loaded very slowly, and after a couple of times loading the slider disappeared again. I have some customizations in the .css options file, could they be affecting the slider loading?

Again, I really appreciate your help.

sunnyw

It is working fine on my test site.

I visited your shared site to troubleshoot the issue but i didn’t see any slider on it.

Please advise.

Hi Movin,

I have been testing this with your code and with my original basic functions.php file and have attached a Word file with my results including pictures of the website on different browsers with the new code. I set the site back to the normal slider configuration showing all categories and all posts with a featured image. I decided I will be fine without excluding the “Latest” category if I can get the slider to display random posts from all posts that have featured pics. I have tried some of the code for that found on this forum, but so far nothing works for me. I very much appreciate your assistance.

Thanks!
sunnyw

Sorry should have compressed photos in file. My attachment showing tests is with this response.

I can’t troubleshoot the issue like that.

Could you please reproduce the issue on your site or would you mind if i log in to your site and do some troubleshooting? If this is ok then could you please share me your site log in details privately by checking the option “Set as private reply” when replying to this topic?

Thanks, Movin. I have decided not to modify the slider, but to just occasionally change the categories shown and control the display by adding featured photos to older posts that I want to add to the slider. I really appreciate your efforts!

sunnyw

You are most welcome here :slight_smile: