【问题标题】:Run a custom function on a WordPress page在 WordPress 页面上运行自定义函数
【发布时间】:2013-01-31 15:39:27
【问题描述】:

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

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

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: php arrays wordpress custom-fields


    【解决方案1】:

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

    然后只需从您的主题中运行 footer.php 模板中的函数。

    另外,避免将query_posts() 用于自定义查询,因为该函数会更改主查询。直接使用WP_Query 类:

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

    仅供参考 the_title() 在屏幕上打印帖子标题,因此您无需将其分配给变量,因为它什么也不返回。

    【讨论】:

    • 我会试一试。不会是片刻:-)
    猜你喜欢
    • 2010-12-28
    • 2016-06-24
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多