Added Functions – Shortcode Recent Cars Filtered by Ribbon
-
So my client wanted to be able to display on their front page a short view of vehicles they have classified as “on special”. To start I created new Ribbons as I didn’t care for the default ones. I then had to edit the admin side to make those function correctly. However this will work with the default ones, you just need to change the appropriate ribbon title.
The code below needs to be inserted into your theme’s functions.php file.
function recent_posts_function($atts){ extract(shortcode_atts(array( 'posts' => 1, ), $atts)); $return_string = '<div class="jumponitcars">'; $args = array( 'numberposts' => $posts, 'post_type' => 'cars_for_sale', 'meta_query' => array( array( 'key' => '_vehicle_ribbon', 'value' => 'jumponit', )) ); query_posts( $args); if (have_posts()) : while (have_posts()) : the_post(); $return_string .= '<a href="'.get_permalink().'">'; $return_string .= '<div class="car">'; $return_string .= '<div class="carribbon"><img src="/images/ribbons/ribbon-jumponit.png"></div>'; $return_string .= ''.get_the_post_thumbnail().'<br>'.get_car_title().'</div>'; $return_string .= '</a>'; endwhile; endif; $return_string .= '</div>'; wp_reset_query(); return $return_string; } function register_shortcodes(){ add_shortcode('recent-cars', 'recent_posts_function'); } add_action( 'init', 'register_shortcodes');
So the code above is first running a query on listed vehicles and selecting only those that have a _vehicle_ribbon meta value of “jumponit”. Jumponit is the value I edited my core files to use for a specific banner I created. You can insert any of the default values there.
The code is then displaying the thumbnail and title for any vehicles it returns along with displaying the appropriate ribbon.
I then used the following CSS entries to make the thumbs consistent with how they appear on the single pages.
div.jumponitcars {width:100%;} div.car { width: 18%; min-width: 200px; float: left; padding: 10px; text-align: center;} .carribbon { text-align: left; margin-left: -4px; margin-bottom: -107px; z-index: 99; position: relative; } a div.car .attachment-post-thumbnail.wp-post-image { padding: 3px; margin: 5px; border: none; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; -moz-box-shadow: 0 0 5px rgba(0,0,0, .3); -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, .3); box-shadow: 0 0 5px rgba(0, 0, 0, .3); }
So once this was setup I simply added the shortcode [recent-cars] where I wanted them to display.
Have fun!
- The topic ‘Added Functions – Shortcode Recent Cars Filtered by Ribbon’ is closed to new replies.