How to insert latest news slider into category in wordpress

To insert a latest news slider into a category in WordPress, you'll need to follow these steps:

Method 1: Using a Plugin

  1. Install and activate a plugin like "WPBakeryPageBuilder" or "Elementor" which has a slider module.
  2. Create a new page or edit an existing one where you want to add the slider.
  3. Add a new section or row to the page and choose the slider module.
  4. Configure the slider settings, such as the number of posts to display, category, and slider layout.
  5. Save the changes and publish the page.

Method 2: Using a Theme's Built-in Slider

  1. Check if your WordPress theme has a built-in slider feature. If it does, you can use it to add a latest news slider to a category.
  2. Go to the theme's customizer or settings page and look for the slider option.
  3. Configure the slider settings, such as the number of posts to display, category, and slider layout.
  4. Save the changes and refresh the page to see the slider in action.

Method 3: Using Shortcode

  1. Install and activate a plugin like "Shortcode Ultimate" or "WP Shortcode".

  2. Create a new page or edit an existing one where you want to add the slider.

  3. Add a new shortcode to the page using the following format:

    [slider category="your-category-slug" posts_per_page="5"]

    Replace "your-category-slug" with the actual slug of the category you want to display the latest news from.

  4. Configure the shortcode settings, such as the number of posts to display, and save the changes.

  5. Publish the page and the slider should appear.

Method 4: Using PHP Code

  1. Create a new PHP file in your theme's directory (e.g., latest-news-slider.php) and add the following code:
    
    <?php
    $args = array(
     'post_type' => 'post',
     'posts_per_page' => 5,
     'category' => 'your-category-slug'
    );
    $query = new WP_Query($args);
    ?>
posts as $post) :?>

post_title;?>

post_excerpt;?>

<?php wp_reset_postdata();?>

Replace "your-category-slug" with the actual slug of the category you want to display the latest news from.

2. Add the following code to your theme's `functions.php` file to enqueue the slider script:
```php
function latest_news_slider_script() {
    wp_enqueue_script('latest-news-slider', get_template_directory_uri(). '/js/latest-news-slider.js', array('jquery'));
}
add_action('wp_enqueue_scripts', 'latest_news_slider_script');
  1. Create a new JavaScript file in your theme's directory (e.g., latest-news-slider.js) and add the following code:
    jQuery(document).ready(function($) {
     $('.slider').slick({
         dots: true,
         infinite: true,
         speed: 300,
         slidesToShow: 3,
         slidesToScroll: 1
     });
    });
  2. Save the changes and refresh the page to see the slider in action.

Remember to customize the code and settings according to your needs and theme requirements.