2013-01-31 19 views
0

我有一个函数在我的WordPress主题functions.php文件用下面的代码:一个WordPress页面上运行自定义功能

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; 
} 

的想法是,我可以在我的网站的任何地方运行这个功能,它将存储我的所有帖子都在数组中。

但是,我不知道我是如何在公共端运行此:“(

完美的世界,我想在我的footer.php脚本运行此

任何帮助将不胜感激。 。

回答

2

完美的世界,我想在我的footer.php脚本运行此。

然后只需运行在foo的功能ter.php模板从你的主题。

此外,请避免将query_posts()用于自定义查询,因为该功能会改变主查询。使用WP_Query类,而不是直接:

$query = new WP_Query(array(...)); 
while($query->have_posts()) { 
    $query->the_post(); 
    // $locations... 
} 

FYI the_title()打印文章标题在屏幕上,所以你并不需要它,因为它没有返回值赋值给一个变量。

+0

我会给它一个旋转。不会是片刻:-) – michaelmcgurk

+0

工作完美 - 非常感谢! – michaelmcgurk

相关问题