Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Shortcode to print posts inside a page
- * Example: [query category_name="wordpress-code" count=3]
- * Code grabbed from: http://goo.gl/my24k
- */
- function queryposts($atts){
- extract( shortcode_atts( array(
- 'category_id' => '',
- 'category_name' => '',
- 'tag' => '',
- 'day' => '',
- 'month' => '',
- 'year' => '',
- 'count' => '5',
- 'author_id' => '',
- 'author_name' => '',
- 'order_by' => 'date',
- ), $atts));
- $output = '';
- $query = array();
- // create the query string
- if ($category_id != '') $query[] = 'cat=' .$category_id;
- if ($category_name != '') $query[] = 'category_name=' .$category_name;
- if ($tag != '') $query[] = 'tag=' . $tag;
- if ($day != '') $query[] = 'day=' . $day;
- if ($month != '') $query[] = 'monthnum=' . $month;
- if ($year != '') $query[] = 'year=' . $year;
- if ($count) $query[] = 'posts_per_page=' .$count;
- if ($author_id != '') $query[] = 'author=' . $author_id;
- if ($author_name != '') $query[] = 'author_name=' . $author_name;
- if ($order_by) $query[] = 'orderby=' . $order_by;
- ob_start(); // output to buffer
- // backup the $post variable so it doesn't mess it up if called again
- $backup = $post;
- $posts = new WP_Query(implode('&',$query));
- while ($posts->have_posts()): $posts->the_post();
- // copy here the post layout from your theme's index.php file -- this is just a example ?>
- <h2> <?php the_title(); ?></h2>
- <?php the_content(); ?>
- <?php
- endwhile;
- $post = $backup; // restore the original $post
- wp_reset_query();
- // store the buffer output in the $output variable
- $output = ob_get_contents();
- ob_end_clean();
- return $output;
- }
- add_shortcode('query', 'queryposts');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement