Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to have a custom post type which contain several sliders for showing in each category page (exact category.php), one for each as planned.

All sliders were assigned to related category.

Currently I am using http://wordpress.org/plugins/custom-post-type-ui/ to create a custom post type called: slider.

And I am having the below code for showing all posts from a category while pressing the cat name in menu. It works fine of course.

<?php $cat = get_the_category(); $cat = $cat[0]; ?>
<?php query_posts($query_string . '&cat='. $cat->cat_ID .''); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php the_content(); ?> 
  <?php endwhile; else: endif; ?>

But the problem is I can't filter the slider (custom post type) by category.

Once I call the wp_query, it rolls out all the sliders and ignores my get cat id arg.

Thought it should show only the slider in certain category, not all category.

So I am asking for a query to call the right slider only.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I have worked out on your problem and have come up with a solution(as far I have understood the problem).

It' works like this:

Registering a post type called : sliders with a taxonomy called slider.

This will create a post type where you can store all your sliders and categories them with this custom taxonomy called slider.

CODE for registering the post type. (this goes in functions.php)

//Custom Post Type Sliders
add_action('init','post_type_slider');
function post_type_slider(){
    register_post_type('sliders',
        array(
            'labels' => array(
                'name'          =>  'Sliders',
                'singular_name' =>  'Sliders',
                'menu_name'     =>  'Sliders',
                'all_items'     =>  'All Sliders',
                'add_new'       =>  'Add A Slider',
                'add_new_item'  =>  'Add New Slider'
                ),
            'public'    => true,
            'supports'  => array(
                            'title',
                            'post-formats',
                            'tags',
                            'editor'
                            ),
            'show_in_admin_bar' =>  true,
            'has_archive'   =>  true
            )
        );

        //Slider Taxanomy Labels
        $labels = array(
            'name'                  => _x( 'Select Slider', 'Taxonomy plural name', 'text-domain' ),
            'singular_name'         => _x( 'Sliders', 'Taxonomy singular name', 'text-domain' ),
            'search_items'          => __( 'slider', 'text-domain' ),
            'all_items'             => __( 'All Sliders', 'text-domain' ),
            'edit_item'             => __( 'Edit Slider', 'text-domain' ),
            'update_item'           => __( 'Update Slider', 'text-domain' ),
            'add_new_item'          => __( 'Add New Slider', 'text-domain' ),
            'new_item_name'         => __( 'New Slider Name', 'text-domain' ),
            'add_or_remove_items'   => __( 'Add or remove Slider', 'text-domain' ),
            'menu_name'             => __( 'Sliders', 'text-domain' ),
        );
        //Slider Taxonomy Arguments
        $slider_args = array(
            'labels'            => $labels,
            'public'            => true,
            'show_in_nav_menus' => true,
            'show_admin_column' => false,
            'hierarchical'      => true,
            'show_tagcloud'     => true,
            'show_ui'           => true,
            'query_var'         => true,
            'rewrite'           => true,
        );

        register_taxonomy( 'slider', array( 'sliders' ), $slider_args );
    }

Now as I have created the post type which will collect all the posts related to different sliders. Now the next step is to create a custom page which can show these posts based on their taxonomy selected. The best way to show is to create a taxonomy-slider.php in theme's directory. This page will automatically catch posts based on slider categories.

Create taxonomy-slider.php and paste this code:

<?php
    while(have_posts()):
        the_post();
        the_title();
    endwhile;
?>

Customize it the way you like.

NOTE: AFTER ALL THE STEPS. Visit Settings > Permalinks page. This will save the changes made by registering custom post type.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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