Query post by category using only one custom page template

Is it possible to query a post by category using only one custom page template? Basically I created a custom post called “Vehicle” and each post are assigned to a different a category. Instead of creating a number of custom-page-template in order to query a specific category is there a way to do it in one custom page template? I’m still new to wordpress at the moment.

This is my work in progress

 <ul class="container">
        <?php
            $args = array(
            'post_type' => 'ivecovehicle',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1
            );


            $packages = new WP_Query( array( 'post_type' => 'ivecovehicle', 'category_name' => 'Iveco Astra Articulated Dumptruck' ));  
            if( $packages->have_posts() ) { while( $packages->have_posts() ) {
               $packages->the_post();
                    ?>

                    <li id="post-<?php the_ID(); ?>" class=" two columns iveco-vehicle-post">

                            <?php if ( has_post_thumbnail()) { $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); echo '<img src="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" alt="' . the_title_attribute('echo=0') . '">';  } ?> 
                            <h4><?php the_title(); ?></h4> 


                                <button type="button" class="btn btn-xs btn-info" data-toggle="modal" data-target="#modal-<?php the_ID(); ?>">
                                    <span>Learn More</span>
                                </button>

                    </li>


                            <div class="modal iveco-modal fade" id="modal-<?php the_ID(); ?>"  tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
                                <div class="modal-dialog">
                                    <div class="modal-content">

                                        <div class="modal-header">
                                            <h4 class="modal-title"><?php the_title(); ?></h4>
                                        </div>

                                    <div class="modal-body">

                                        <div class="six columns">
                                            <h5>Quickspec</h5>
                                            <?php the_field('quickspec'); ?>
                                        </div>

                                        <div class="six columns">
                                            <?php the_content(); ?>
                                            <div class="">
                                                <p>To download brochure, please enter email and contact number.</p>
                                                <?php echo do_shortcode('[contact-form-7 id="452" title="[contact-form-7 id="452" title="JCB Download Brochure"]') ?>
                                                <a class="download-button hide-download-brochure" href="<?php the_field('download_link'); ?>" target="_blank">Download Brochure</a>
                                            </div>
                                        </div>



                                        <div class="row"></div>

                                    </div>

                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    </div>

                                    </div>

                                </div>
                            </div>


                    <?php
                        }
                    }
                    else {

                    }
        ?>
    </ul>

As you can see I changed the category name in order to query the specific post based on category.


Source: wordpress

Leave a Reply