Created
September 26, 2016 18:23
-
-
Save brunomonteiro3/5aa78d2abb4fe8c459c34f841b6b45c4 to your computer and use it in GitHub Desktop.
Query posts with specific template
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: List | |
*/ | |
?> | |
<style> | |
td, tr{ | |
border: 1px solid grey; | |
padding: 10px; | |
} | |
</style> | |
<h1> | |
Lista: | |
</h1> | |
<table> | |
<tbody> | |
<thead> | |
<tr> | |
<td>Title</td> | |
<td>Link</td> | |
</tr> | |
</thead> | |
<?php | |
// WP_Query arguments | |
$args = array ( | |
'post_type' => 'page', | |
'posts_per_page' => 1000, | |
'meta_key' => '_wp_page_template', | |
'meta_value' => 'template_name.php' | |
); | |
// The Query | |
$query = new WP_Query( $args ); | |
// The Loop | |
if ( $query->have_posts() ) { | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
?> | |
<tr> | |
<td><?php the_title(); ?></td> | |
<td><a href="<?php the_permalink(); ?>">Link</a></td> | |
</tr> | |
<?php | |
} | |
} else { | |
// no posts found | |
} | |
// Restore original Post Data | |
wp_reset_postdata(); | |
?> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment