• Hi,

    I have a website with a slider (showing imges and text of a custom post type) on several page templates. For every page in the admin area, I created a custom field (with the simple fields plugin), where users can select items from a custom taxonomy to decide, which posts are being displayed in the slider on this particular page.

    If no category is selected, the slider is supposed to display all posts from the custom post type.

    This is the part, that’s not working. Here i my code:
    (the custom post type is “slider_posts”, the custom taxonomy “slider_cat”)

    $slider_category = simple_fields_value("sf_slider_cat");
    
    if (!empty ($slider_category) ) {
    	$cat_list = implode(",", $slider_category);
    	$args = array(
    		'post_type'=> 'slider_posts',
    		'tax_query' => array(
    				array(
    					'taxonomy' => 'slider_cat',
    					'field' => 'ID',
    					'terms' => $cat_list
    					)));
    	}
    	else {
    	$args = array(
    		'post_type'=> 'slider_posts'
    	);
    		}
    
    $postQuery = get_posts($args);
    
    foreach ($postQuery as $post ) : setup_postdata($post);
    // blabla, display post content
    endforeach;

    The thing that’s not working is the “else” condition. When “slider_category” is empty, no posts are being displayed (instead of all of them).

    Where is my mistake?

Viewing 1 replies (of 1 total)
  • Thread Starter lukastillmann

    (@lukastillmann)

    There seems to be an additional problem, when more than one category is selected, suggesting my method to “implode” the slider-category array to a comma-separated string is another problem here.

Viewing 1 replies (of 1 total)
  • The topic ‘Display all posts from custom post type, when no category is selected’ is closed to new replies.