Last active
December 15, 2015 09:09
-
-
Save hissy/5235923 to your computer and use it in GitHub Desktop.
#WordPress use page template for archive with WP_Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: Blog Archive | |
*/ | |
get_header(); ?> | |
<div id="primary" class="site-content"> | |
<div id="content" role="main"> | |
<?php | |
$paged = (int) get_query_var('paged'); | |
$args = array( | |
'posts_per_page' => 10, | |
'paged' => $paged, | |
'orderby' => 'post_date', | |
'order' => 'DESC', | |
'post_type' => 'post', | |
'post_status' => 'publish' | |
); | |
$myquery = new WP_Query($args); | |
if ( $myquery->have_posts() ) : | |
?> | |
<header class="archive-header"> | |
<h1 class="archive-title">ブログアーカイブ</h1> | |
</header><!-- .archive-header --> | |
<?php | |
while ( $myquery->have_posts() ) : | |
$myquery->the_post(); | |
get_template_part( 'content', get_post_format() ); | |
endwhile; | |
if ( $myquery->max_num_pages > 1 ) : | |
?> | |
<nav class="navigation" role="navigation"> | |
<div class="nav-previous alignleft"><?php echo get_next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentytwelve' ), $myquery->max_num_pages ); ?></div> | |
<div class="nav-next alignright"><?php echo get_previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?></div> | |
</nav> | |
<?php | |
endif; | |
?> | |
<?php else : ?> | |
<?php get_template_part( 'content', 'none' ); ?> | |
<?php endif; ?> | |
<?php | |
// reset post data | |
wp_reset_postdata(); | |
?> | |
</div><!-- #content --> | |
</div><!-- #primary --> | |
<?php get_sidebar(); ?> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment