Advertisement
salmancreation

A Random Quote in the Sidebar

Oct 30th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. A Random Quote in the Sidebar
  2.  
  3. If you're keen on literature or religious, you might want to have some of your favorite quotes in the sidebar—it's not a waste of space if you use the area with purpose. So, if you're going to list a random quote in your sidebar on each page view, you can use the following code snippet to create the post type and use the following query to create a loop in your sidebar:
  4.  
  5. <?php
  6.  
  7. /*
  8.  * Create new post type called "Quotes"
  9.  * (refer to the `register_post_type` function to
  10.  * learn more about creating custom post types).
  11.  */
  12. function quote_post_type() {
  13.      
  14.     $args = array(
  15.         'label' => 'Quotes',
  16.         'public' => true
  17.     );
  18.      
  19.     register_post_type( 'quotes', $args );
  20. }
  21.  
  22. add_action( 'init', 'quote_post_type' );
  23.  
  24. // Setup arguments.
  25. $args = array(
  26.     // Get the "quotes" psot type.
  27.     'post_type' => 'quotes',
  28.     // Randomize the order.
  29.     'orderby' => 'rand',
  30.     // Get only one item.
  31.     'posts_per_page' => 1,
  32. );
  33.  
  34. // Instantiate new query instance.
  35. $my_query = new WP_Query( $args );
  36.  
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement