Advertisement
salmancreation

Custom post query shortcode / pagination

Oct 10th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. function post_list_shortcode($atts){
  2.     extract( shortcode_atts( array(
  3.         'count' => '',
  4.     ), $atts) );
  5.      
  6.     $q = new WP_Query(
  7.         array('posts_per_page' => $count, 'post_type' => 'posttype', 'orderby' => 'menu_order','order' => 'ASC')
  8.         );      
  9.          
  10.     $list = '<div class="custom_post_list">';
  11.     while($q->have_posts()) : $q->the_post();
  12.         $idd = get_the_ID();
  13.         $custom_field = get_post_meta($idd, 'custom_field', true);
  14.         $post_content = get_the_content();
  15.         $list .= '
  16.         <div class="single_post_item">
  17.             <h2>' .do_shortcode( get_the_title() ). '</h2>
  18.             '.wpautop( $post_content ).'
  19.             <p>'.$custom_field.'</p>
  20.         </div>
  21.         ';        
  22.     endwhile;
  23.     $list.= '</div>';
  24.     wp_reset_query();
  25.     return $list;
  26. }
  27. add_shortcode('post_list', 'post_list_shortcode');  
  28.  
  29.  
  30.  
  31.  
  32. -----------------------------==========================
  33. <?php
  34.   $temp = $wp_query;
  35.   $wp_query = null;
  36.   $wp_query = new WP_Query();
  37.   $wp_query->query('showposts=6&post_type=posttype&orderby=menu_order&order=ASC'.'&paged='.$paged);
  38.  
  39.   while ($wp_query->have_posts()) : $wp_query->the_post();
  40. ?>
  41.  
  42. <?php
  43.    $custom_field= get_post_meta($post->ID, 'custom_field', true);
  44. ?>
  45.  
  46. <h2><?php the_title(); ?></h2>
  47. <?php the_content(); ?>
  48. <p><?php echo $custom_field; ?></p>                          
  49.  
  50.  
  51. <?php endwhile; ?>
  52.  
  53. <?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } else { include('navigation.php'); } ?>
  54.  
  55. <?php
  56.   $wp_query = null;
  57.   $wp_query = $temp;  // Reset
  58. ?>
  59.  
  60.  
  61.  
  62.  
  63. ==============================
  64.  
  65.  
  66.  
  67.  
  68.  
  69. Navigation.php code is
  70.  
  71.  
  72. <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyeleven' ) ); ?></div>
  73. <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement