Skip to content

Instantly share code, notes, and snippets.

@colegeissinger
Last active January 4, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colegeissinger/8628947 to your computer and use it in GitHub Desktop.
Save colegeissinger/8628947 to your computer and use it in GitHub Desktop.
A working WP_Query() script with pagination and some data validation for http://wordpress.org/support/topic/custom-post-type-pagination-problem
<div id="content">
<?php
$args = array(
'post_type' => 'recipes',
'paged' => ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 ),
);
// It's generally a good idea to give a unique name for your new query, so I'll name this $recipes
$recipes = new WP_Query( $args );
?>
<?php while ($recipes->have_posts()) : $recipes->the_post(); ?>
<div class="post clearfix" id="post-<?php the_ID(); ?>">
<?php
if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>"><img class="postimg" src="<?php echo get_stylesheet_directory_uri(); // more performant way of getting the URL to your theme directory than bloginfo('stylesheet_directory') ?>/timthumb.php?src=<?php get_image_url(); ?>&h=200&w=200&zc=1" alt="<?php the_title(); ?> Recipe"/></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><img class="postimg" src="<?php echo get_template_directory_uri(); ?>/images/dummy.jpg" alt="" /></a>
<?php } ?>
<div class="cover">
<div class="title">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php esc_attr( the_title() ); // clean the title for use in the title attribute ?> Recipe"><?php the_title(); ?></a></h2>
</div>
<div class="recipemeta">
<span class="cooktime"> <strong>ready in</strong> <?php $cooktime=get_post_meta($post->ID, 'wtf_cooktime', true); echo esc_html( $cooktime ); // ensure your not outputting any malicous code with esc_html() here ?> mins </span> <span class="serve"> <strong>Serving:</strong> <?php $serving=get_post_meta($post->ID, 'wtf_serving', true); echo esc_html( $serving ); ?> people</span>
</div>
<div class="entry" align="justify">
<?php wpe_excerpt('wpe_excerptlength_recipe', ''); ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php wp_reset_postdata(); ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment