【问题标题】:Wordpress: Display all pagesWordpress:显示所有页面
【发布时间】:2013-12-04 19:49:35
【问题描述】:

我想在 Wordpress 中以简码显示所有页面。 我的代码如下所示:

function createTilesFromSites() {
$args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'hierarchical' => 1,
    'exclude' => '',
    'include' => '',
    'meta_key' => '',
    'meta_value' => '',
    'authors' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
); 
$pages = get_pages($args);
foreach ($pages as $page_data) {
    $content = apply_filters('the_content', $page_data->post_content);
    $title = $page_data->post_title;
    $slug = $page_data->post_name;
    echo "$title <br />";
}
}
add_shortcode('createTiles', 'createTilesFromSites');

现在,它向我显示页面(我的页面称为“1”、“2”、“3”、“4”和“博客”)。所以它应该给我类似的东西:“1234Blog”。 它打印给我这个,但不止一次。 2658 TIMES(至少 chrome 是用 cmd+f 说的,反正超长)。所以我的输出看起来像这样:“1234Blog1234Blog1234Blog1234Blog1234Blog1234Blog1234Blog1234Blog1234Blog1234Blog”例如。

代码有问题吗?这可能是我的 Wordpress 设置的问题吗?

感谢您的帮助。

【问题讨论】:

  • 返回而不是回显短代码输出很重要。在函数开始时声明 $output = '';,在循环期间连接项目,例如$output .= $title; 最后返回输出:return $output;
  • 感谢您的提示,我实际上对 php 和 wordpress 还是很陌生:/ 但这并不能解决我的问题!

标签: php wordpress foreach


【解决方案1】:

我认为您的 apply_filters 代码将再次调用您的简码,因此它将处于循环状态。所以你可以尝试只获取内容而不应用过滤器,或者从内容中去除短代码,或者调用 remove_shortcode('createTiles');

【讨论】:

  • 哦,你是对的!但是,显然,我也需要内容:/我该如何解决这个问题?
  • 获取内容 $content = $page_data->post_content;然后搜索并用空字符串替换您的 [createTiles] 短代码,然后调用 $content = apply_filters('the_content', $content);
  • 用空字符串替换 [createTiles] 是什么意思?抱歉,我对这一切真的很陌生:/编辑:Nvm我在测试后理解了它:>谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多