一页wordpress问题与get_template_part和get_footer


One page wordpress issue with get_template_part and get_footer

我想做的是转换我的一页设计在wordpress,我认为这将是很好的,能够编辑,添加和修改页面的不同部分在单独的页面。单页网站将通过菜单(id main)进行排序。

在wp-codex之后,我使用了get_template_part,它应该正常工作,因为它应该:

将模板部分加载到模板中(页眉、侧边栏、页脚除外)

get_header被跳过,但get_footer被执行,网站呈现错误。


front-page.php

$pages = wp_get_nav_menu_items('main');
global $post;
foreach ($pages as $post) {
    $post = get_post($post->object_id);
    if($post->post_type != "page") continue;
    setup_postdata( $post );
    $postName = (locate_template('page-' . $post->post_name . '.php') == '') ? null : $post->post_name;
    get_template_part('page', $postName);
}
wp_reset_postdata();

这是一个bug吗?还是我做错了什么?实现这一目标的最佳方式是什么?

如果您的get_template_part()不工作,那么问题可能是主题中没有名为page.php的文件,请检查该文件

为了它的价值,我经常跳过一些WP帮助器,更直接地工作,但不是流氓。下面是我的库中的一个片段:

/** Get core data for a WP post/page using ID
* @param $id : wp post/page ID
* @return array
*/
function wp_get_single_post_basic($id){
    $post = get_post($id, ARRAY_A);
    $ret = array();
    $content = $post['post_content'];
    // FILTER via WP
    $content = apply_filters('the_content', $post['post_content']);
    $ret['title']= $post['post_title'];;
    $ret['content']= $content;
    $ret['link'] = $post['post_name'];
    $ret['edit_link'] = '<a href="'.get_edit_post_link($id).'" title="edit">edit</a>';
    return $ret;
}

该函数以一种非常易于管理的方式分解内容并生成编辑链接(在调用脚本时为其构建bool)。也可以格式化内容。

因此,抓取并排序您想要的id,并在循环中运行此id为您的一个页面。然后,所有的内容将被页面/帖子/其他内容隔离(自定义分类法FTW)。

如果你已经准备好了id和排序加上html/css准备去,我可以把这个功能和工作你的一个页面在一个小时内完成。对于生产线的工作,这种类型的助手是伟大的,也是完美的网站,你想要一个特定的帖子/页面以一些奇怪的方式放置在一些奇怪的地方以外的循环。

如果你有一个问题,请告诉我,我已经几个月没有使用它了,几年前写的…

我认为您使用全局$post并在菜单对象的foreach循环中存储$post变量有问题。我想这可能是模板部分调用。

时出错的原因。

我建议你删除全局$post声明,reset_postdatasetup_postdata,因为你没有在循环中使用全局,你只是命名它。

你从菜单中获得$post对象,所以看起来你不需要全局或其他函数,当你想使用没有post_id的"循环样式"模板标签时,如the_permalink()the_title()

我最终将默认的page.php模板复制为page-page.php,并像这样加载它:

$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? 'page' : $post->post_name;

然后在page.php中加载page-page.php