Skip to content

Instantly share code, notes, and snippets.

@amaxwell01
Created July 29, 2012 21:31
Show Gist options
  • Save amaxwell01/3201950 to your computer and use it in GitHub Desktop.
Save amaxwell01/3201950 to your computer and use it in GitHub Desktop.
Simple and clean wordpress custom post query
<ul>
<?php
$wp_query = new WP_Query( array(
'post_type' => 'gallery',
'posts_per_page' => -1
));
if( have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
echo '<li>' . the_title() . '</li>';
}
}
else {
echo '<li>No Title</li>';
}
wp_reset_query();
?>
</ul>
@amaxwell01
Copy link
Author

Change the 'posts_per_page' from 100 to -1 to default to show all posts vs a random number

@amaxwell01
Copy link
Author

Realized that I didn't have a unordered list element for the list item to be contained in.

@robhealy
Copy link

robhealy commented Nov 2, 2018

"the_title()" echoes itself out. Please try "get_the_title()"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment