【问题标题】:Get custom post type in WordPress to load custom template在 WordPress 中获取自定义帖子类型以加载自定义模板
【发布时间】:2013-11-04 11:42:29
【问题描述】:

我在 WordPress 中为活动创建了一个自定义帖子类型。我的 index.php 页面基于二十个十二主题,因为它设置了循环,然后将渲染传递给部分模板:

<?php get_header();

if (have_posts()) {
    while (have_posts()) {
        the_post();
        get_template_part('content', get_post_format());
    }
}
else {
    get_template_part('content', 'none');
}

get_sidebar();
get_footer();

但是,在查看事件帖子时,get_post_format() 只返回一个空字符串(我希望它返回 event 或类似的),而只返回 content.php 模板部分已加载,而不是 content-event.php 这是我所期待和真正想要的。

如何将我的活动帖子类型报告event 设为其帖子格式?

【问题讨论】:

    标签: wordpress wordpress-theming custom-post-type


    【解决方案1】:

    我猜你混淆了post formatspost types。你需要的是get_post_type()

    【讨论】:

    • 我不确定。我刚刚从二十一十二主题中复制了它。帖子类型和帖子格式有什么区别?帖子格式从何而来?
    • 学习过的帖子格式是帖子、附件等。上述方法不起作用(用get_post_type() 替换get_post_format() 崩溃的WordPress 信不信由你)我将答案标记为已接受它引导我找到解决方案(使用get_post_type()content.php 模板添加条件)。谢谢。
    • WordPress 崩溃是什么意思?将WP_DEBUG 设置为true 以获取实际错误。
    • 如果您想要为您的event 帖子类型指定一个特定模板,您也可以创建一个名为single-event.php 的文件用作template
    【解决方案2】:

    用途:

    if(get_post_type()):
        get_template_part( 'content', get_post_type() );
    else:
        get_template_part( 'content', get_post_format() );
    endif;
    

    【讨论】:

      猜你喜欢
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多