Advertisement
MimiDev

CPT plus code for post grid

Aug 2nd, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2. function major_trips_cpt() {
  3.   $args = array(
  4.     'labels' => array(
  5.         'name'          => 'Major Trips',
  6.         'singular_name' => 'Major Trip',
  7.        'all_items'     => 'All Major Trips',
  8.     ),
  9.     'supports'      => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
  10.     'menu_position' => 4,
  11.     'show_ui'             => true,
  12.     'show_in_menu'        => true,
  13.     'show_in_admin_bar'   => true,
  14.     'description'   => ( 'New Major Trip Entry'),
  15.     'public'        => true,
  16.     'has_archive'   => true,
  17.     'menu_icon'     => get_stylesheet_directory_uri() . '/images/fwdaus-icon.png',
  18.     'rewrite'        => array('slug' => '4wdaus-major_trips')
  19.   );
  20.   register_post_type( 'major_trip', $args );
  21. }
  22. add_action( 'init', 'major_trips_cpt' );
  23. ?>
  24.  
  25. //Code for displaying grid of blog posts for the relevant category
  26. <!-- Search all posts for those with the category for this major_trip -->
  27.   <?php
  28.    $args = array(
  29.        'post_type' => 'post',
  30.        'posts_per_page' => 6,
  31.        'order' => 'desc',
  32.        'orderby' => 'date',
  33.        'tax_query' => array(
  34.            array(
  35.                'taxonomy' => 'category',
  36.                'field'    => 'slug',
  37.                'terms'    => get_field('category_to_display'), // category from the Advanced Custom Field variable
  38.            ),
  39.        ),
  40.    );
  41.    
  42.     if ( get_query_var('paged') ) {
  43.         $args['paged'] = get_query_var('paged');
  44.     } elseif ( get_query_var('page') ) {
  45.         $args['paged'] = get_query_var('page');
  46.     } else {
  47.         $args['paged'] = 1;
  48.     }
  49.    
  50.     $the_query = new WP_Query( $args );
  51.    
  52.    // Pagination fix
  53.    $temp_query = $wp_query;
  54.     $wp_query   = NULL;
  55.     $wp_query   = $the_query;
  56.   ?>
  57.  
  58.  <div id="fwd-blog-grid">
  59.  <?php if ( $the_query->have_posts() ) : ?>
  60.      <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  61.          <div class="fwd-grid-item">
  62.              <?php if ( has_post_thumbnail() ) {
  63.                  the_post_thumbnail();
  64.              } ?>
  65.              <h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
  66.              <div class="entry-content">
  67.                  <a href='<?php the_permalink(); ?>'><?php the_excerpt(); ?></a>
  68.              </div>
  69.          </div>
  70.      <?php endwhile; ?>
  71.  
  72.      <?php
  73.          wp_reset_postdata();
  74.          
  75.          echo "<div style='clear: both;'>";
  76.             //wp_pagenavi( array( 'query' => $the_query ) );
  77.             echo "<div style='float: left;'>";
  78.                 next_posts_link( '&laquo; Older Posts' );
  79.             echo "</div>";
  80.             echo "<div style='float: right;'>";
  81.                     previous_posts_link( 'Newer Posts &raquo;' );
  82.                 echo "</div>";
  83.          echo "</div>";
  84.          
  85.          // Reset main query object
  86.          $wp_query = NULL;
  87.          $wp_query = $temp_query;
  88.      ?>
  89. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement