【问题标题】:Displaying Content From Custom Post Type with Page Templates使用页面模板显示来自自定义帖子类型的内容
【发布时间】:2016-05-09 22:08:37
【问题描述】:

因此,我正在为客户开发一个网站,其中主页的构建类似于单页滚动条,但我还需要单个主页之外的其他页面的功能。我为这些部分构建了自定义帖子类型,并使用此代码将它们显示在主页上。

<?php query_posts( array('post_type'=>'homepage', 'posts_per_page' => 1000, 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>

    <?php 
        global $post;
        $slug = $post->post_name;
        locate_template(
            array(
                "template-$slug.php",
                'template-main.php'
            ), true
        );
    ?>
<?php endwhile; endif; ?>

因此,如您所见,这是自动提取内容并使用基于 post slug 的页面模板显示它,但是,我需要允许我的客户基于在下拉列表中选择的页面模板显示内容,我'已经使用此代码创建了一个显示页面模板的下拉 UI。

add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' );
function add_custom_page_attributes_meta_box(){
global $post;
    if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) {
        add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core');
    }
}

function custom_page_attributes_meta_box($post) {
    $template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?>
    <select name="page_template" id="page_template">
        <?php $default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' ); ?>
        <option value="default"><?php echo esc_html( $default_title ); ?></option>
        <?php page_template_dropdown($template); ?>
    </select><?php
}

add_action( 'save_post', 'save_custom_page_attributes_meta_box' );
function save_custom_page_attributes_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;
    if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) {
        update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] );
    }
}

所以,我现在面临的问题是如何根据选择的页面模板在我的主页中显示所有自定义帖子。

非常感谢! J

【问题讨论】:

  • 所以基本上你只是想知道如何在你的主博客页面上显示自定义帖子,对于你来说,主页?
  • 不,我需要知道如何根据模板选择器使自定义帖子类型显示内容。现在,这会将所有内容拉到主页中,但是,内容是根据 slug 确定的页面模板显示的。我希望我的客户能够从模板选择器中选择它的显示方式。

标签: php wordpress


【解决方案1】:

Wordpress 实际上只对页面类型的帖子使用_wp_page_template 元字段。如果要更改模板,可以使用过滤器single template。我会推荐的一件事是你在你的主题/插件中写下你正在使用它的好笔记......

顺便说一句,将cpt 更新为您的帖子类型

function load_cpt_template($single_template) {
     global $post;

     if ($post->post_type == 'cpt') {

          $new_template = get_post_meta( $post->ID, '_wp_page_template', true ); 

          // if a blank field or not valid do nothing, load default..
          if( is_file($new_template) )
            $single_template = $new_template;
     }
     return $single_template;
}
add_filter( 'single_template', 'load_cpt_template' );

【讨论】:

  • 我应该用这个替换代码中的整个循环还是直接从global $post开始?
  • 不只是放在你的函数文件中,它不是循环的一部分,它会在你的自定义帖子类型上查找元值 _wp_page_template 并加载模板而不是正常过程。
  • 谢谢。我的下一个问题是如何将所有帖子拉到一起,并使用它们的页面模板将它们显示在页面中。对此有什么想法吗?
  • 这真是一个新问题!如果这个完成了,请投票/接受并为此发布一个新问题。我会自己尝试,include() 或 wordpress get_template_part() 循环中的页面模板。你会遇到 post 变量的问题,但你应该能够克服它。
猜你喜欢
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多