【发布时间】: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 确定的页面模板显示的。我希望我的客户能够从模板选择器中选择它的显示方式。