Php 使用页面模板显示自定义文章类型中的内容

Php 使用页面模板显示自定义文章类型中的内容,php,wordpress,Php,Wordpress,因此,我正在为一个客户端开发一个站点,其中主页的构建就像一个单页滚动条,但我还需要在单个主页之外的其他页面的功能。我为这些部分构建了一个自定义的帖子类型,并使用此代码在主页上显示它们 <?php query_posts( array('post_type'=>'homepage', 'posts_per_page' => 1000, 'orderby' => 'menu_order', 'order' => 'ASC') ); ?> <?php if(h

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

<?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'] );
    }
}
add_操作('add_meta_box','add_custom_page_attributes_meta_box');
函数添加\自定义\页面\属性\元\框(){
全球$员额;
如果('page'!=$post->post\u type&&post\u type\u支持($post->post\u type,'page attributes')){
添加元框('custompageparentdiv'、'custompageparentdiv'、'custompageparentdiv('Template')、'custompageparents'meta'box',NULL、'side'、'core');
}
}
函数自定义页面属性元框($post){
$template=get_post_meta($post->ID,'.\u wp_page_template',1);?>

Wordpress实际上只使用
\u wp\u page\u template
元字段作为页面类型的帖子。如果你想更改模板,你可以使用过滤器
单个模板
。我建议你在你的主题/插件中添加好的注释

顺便说一句,更新你的帖子类型

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' );

所以基本上你只想知道如何在你的博客主页上显示自定义帖子,对你来说,主页?不,我需要知道如何根据模板选择器来显示自定义帖子类型的内容。现在,这会将所有内容拉入主页,但是,内容是根据e slug。我希望我的客户能够从模板选择器中选择它的显示方式。我应该用它替换代码中的整个循环,还是简单地从
全局$post
开始?不只是放在函数文件中,它不是循环的一部分,它会在自定义的post类型和loa上查找meta值\u wp\u page\u模板我的下一个问题是如何把所有的帖子放在一起,并用它们的页面模板显示在页面上。关于这方面有什么想法吗?这真的是一个新问题!如果这个问题完成了,请投票/接受并发布一个新问题。我会亲自尝试,
include()
或wordpress
get\u template\u part()
循环中的页面模板。post变量会有问题,但应该能够解决。