I have a function in my WordPress theme functions.php
file with the following code:
function locations() {
$locations_list = array();
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'post_type' => 'location'));
while(have_posts()) {
$locations_list[the_slug()] = the_title();
}
wp_reset_query();
return $locations_list;
}
The idea is that I can run this function anywhere on my site and it will store all my posts in an array.
However, I have no idea how I run this at the public end :'(
Perfect world, I'd like to run this in my footer.php
script.
Any help would be greatly appreciated.
Perfect world, I'd like to run this in my footer.php script.
Then simply run the function in the footer.php template from your theme.
Also, avoid using query_posts()
for custom queries because that function alters the main query. Use the WP_Query
class directly instead:
$query = new WP_Query(array(...));
while($query->have_posts()) {
$query->the_post();
// $locations...
}
FYI the_title()
prints the post title on the screen, so you don't need to assign it to a variable because it returns nothing.