I am building a wordpress theme from scratch. I have this taxonomy:
add_action( 'init', 'create_cat_slider' );
function create_cat_slider() {
register_taxonomy(
'sliderType','post',
array(
'label' => __( 'Slider' ),
'hierarchical' => true,
)
);
}
I have two posts attached to this taxonomy
Now I have this following loop. It should create two buttons for the slider (the number of posts related to the taxonomy should be equal to the number of buttons created). As stated above, I have to posts related to the taxonomy. The issue is that after the WHILE is executed I will end up with 7 buttons. Been trying to fing a solution for this for the past several hours. Even with the codex in front, being my first theme, I find this quite hard.
$args = array(
'tax_query' => array(
'taxonomy' => 'sliderType'
)
);
$custom_query = new WP_Query( $args );
if($custom_query->have_posts()) :
//echo $custom_query->found_posts;
$i = 0;
while ( $custom_query->have_posts() ) : $custom_query->the_post();
if($i == 0)
$active = 'class="active"';
else
$active = '';
echo '<a href="#" data-target="#bigSlider" data-slide-to="' . $i . '" ' . $active . '></a>';
$i++;
endwhile;
endif;
wp_reset_postdata();
LE: The current version of the code:
Taxonomy
add_action( 'init', 'create_cat_slider' );
function create_cat_slider() {
register_taxonomy(
'slider_ype','post',
array(
'label' => __( 'Slider' ),
'hierarchical' => true,
'rewrite' => array( 'slug' => 'slidertype' )
)
);
}
Query
$args = array(
'tax_query' => array
(
array(
'taxonomy' => 'slidertype',
'field' => 'slug',
'terms' => 'slidertype'
)
)
);
$custom_query = new WP_Query( $args );
There is no easy way to query all the posts attached to a specific taxonomy.
The only way to do this is to get all the terms attached to the taxonomy and then pass all the term ID’s to a
tax_query
in your custom queryYou can try the following: (Requires PHP 5.4+ and this is only the important parts. Also note, you have mispelled
sliderType
😉)If you, however, just need to query posts from a specific term in a taxonomy, you can simply just pass the term ID or slug with the relevant field value to the query