doufei0933
2013-01-31 15:39
浏览 34
已采纳

在WordPress页面上运行自定义功能

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.

图片转代码服务由CSDN问答提供 功能建议

我的WordPress主题 functions.php 文件中有一个函数,代码如下:

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

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

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

完美的世界,我想在我的< code> footer.php script。

非常感谢任何帮助。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzuo7166 2013-01-31 15:45
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题