1

I'm confused how to properly tackle this and the amount of information on the subject isn't very direct. I am trying to develop a custom slider for my homepage and needed to create a custom taxonomy 'slider' to tag which posts could be included in the slideshow.

Slider Example

Here's the code I used:

add_action( 'init', 'create_slider_taxonomies', 0 );

function create_slider_taxonomies() {
    $labels = array(
        'name'              => _x( 'Sliders', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Slider', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Sliders', 'textdomain' ),
        'all_items'         => __( 'All Sliders', 'textdomain' ),
        'parent_item'       => __( 'Parent Slider', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Slider:', 'textdomain' ),
        'edit_item'         => __( 'Edit Slider', 'textdomain' ),
        'update_item'       => __( 'Update Slider', 'textdomain' ),
        'add_new_item'      => __( 'Add New Slider', 'textdomain' ),
        'new_item_name'     => __( 'New Slider Name', 'textdomain' ),
        'menu_name'         => __( 'Slider', 'textdomain' ),
    );


    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'slider' ),
        'show_in_rest'      => true, // need to show this in order for option to display under the 'Document' panel in a post
    );

    register_taxonomy( 'slider', array( 'post', 'slider' ), $args );
}

And it showed up in the post just fine:

Working sidebar example

Now, I'm unsure how to query a list of the posts so that I can assemble the content onto a page.

I thought that I could do some variation of my $wp_query that I use for the archive page, but that didn't seem to work:

<?php 
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); 
$wp_query->query(array(
  'posts_per_page' => -1,
  'taxonomy'       => slider,
  'field'          => slug,
  'terms'          => homepage
));
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

Maybe this isn't the best way to do this. But I don't want to rely on yet another third party plugin especially when the majority of slider plugins don't feature content the way that I want them to.

Any suggestions will be appreciated. Thank you!

1 Answer 1

0

Remember to wrap your strings in quotes. I think that's the main issue. This works for me:

<?php
// https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
// Set up a new WP_Query object for posts that have the 'homepage' term
// set for the 'slider' taxonomy:
$slide_query = new WP_Query( array(
    'posts_per_page' => 100, // Set a reasonable limit just in case.
    'post_type'      => 'post',
    'tax_query'  => array(
        array(
            'taxonomy' => 'slider', // Strings should have quotes!
            'field'    => 'slug',
            'terms'    => 'homepage',
        ),
    ),
));
?>

Then output the HTML:

<?php if ( $slide_query->have_posts() ) : ?>
    <div class="slider">
        <?php while ( $slide_query->have_posts() ) : $slide_query->the_post(); ?>
            <div class="slide">
                <h2 class="slide-title">><?php the_title(); ?></h2>
                <div class="slide-content">
                    <?php the_content(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    </div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

You don't need to do the $temp = $wp_query; $wp_query= null; thing. Just create a new variable like I did in the example, and call wp_reset_postdata() as I did in the example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.