我创建了一个名为“project”的自定义帖子类型。但是,如何使自定义帖子类型仅显示在主模板上?这是一个只有一页的网站,我不希望它将访问者引导到第二页。
Wordpress第3.5.1版
<?php
function my_custom_post_project() {
$labels = array(
\'name\' => _x( \'Projects\', \'post type general name\' ),
\'singular_name\' => _x( \'Project\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'project\' ),
\'add_new_item\' => __( \'Add New Project\' ),
\'edit_item\' => __( \'Edit Project\' ),
\'new_item\' => __( \'New Project\' ),
\'all_items\' => __( \'All Projects\' ),
\'view_item\' => __( \'View Project\' ),
\'search_items\' => __( \'Search Projects\' ),
\'not_found\' => __( \'No projects found\' ),
\'not_found_in_trash\' => __( \'No projects found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Projects\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Holds my projects and project specific data\',
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\',\'editor\', \'excerpt\'),
\'has_archive\' => false,
\'rewrite\' => array(\'slug\'=>\'/\',\'with_front\'=>false),
);
register_post_type( \'project\', $args );
}
add_action( \'init\', \'my_custom_post_project\' );
function my_taxonomies_project() {
$labels = array(
\'name\' => _x( \'Project Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Project Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Project Categories\' ),
\'all_items\' => __( \'All Project Categories\' ),
\'parent_item\' => __( \'Parent Project Category\' ),
\'parent_item_colon\' => __( \'Parent Project Category:\' ),
\'edit_item\' => __( \'Edit Project Category\' ),
\'update_item\' => __( \'Update Project Category\' ),
\'add_new_item\' => __( \'Add New Project Category\' ),
\'new_item_name\' => __( \'New Project Category\' ),
\'menu_name\' => __( \'Project Categories\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
);
register_taxonomy( \'product_category\', \'project\', $args );
}
add_action( \'init\', \'my_taxonomies_project\', 0 );
我现在更改了索引中的代码。php:
<?php get_header(); ?>
<div id="posts" class="row isotope ">
<!-- Start Project -->
<?php
$args = array(
\'post_type\' => \'project\'
);
$projects = new WP_Query($args);
if ($projects->have_posts()) {
while ($projects->have_posts()) {
$projects->the_post();
the_title();
get_the_content();
}
}
?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
标题被引入,但由于某种原因,其高度为0px。这与代码有关还是完全不同?此外,编辑器元数据库中的内容也不会显示。
最合适的回答,由SO网友:s_ha_dum 整理而成
鉴于提供的信息,很难回答,但您可能希望。。。
\'has_archive\' => false,
\'rewrite\' => array(\'slug\'=>\'/\',\'with_front\'=>false),
。。。当您注册帖子类型,然后您可以将帖子类型添加到该主页/首页的主查询时,作为参数的一部分。。。
function add_cpt_to_home($qry) {
if (is_front_page() || is_home()) {
$post_type = (array)$qry->get(\'post_type\');
$post_type[] = \'post\';
$post_type[] = \'project\';
$post_type = array_unique(array_filter($post_type));
$qry->set(\'post_type\',$post_type);
}
}
add_action(\'pre_get_posts\',\'add_cpt_to_home\');
或者编写一个辅助查询来获取该post\\u类型。
$args = array(
\'post_type\' => \'project\'
);
$projects = new WP_Query($args);
if ($projects->have_posts()) {
while ($projects->have_posts()) {
$projects->the_post();
the_title(); // etc
}
}
测试不太好,但正如我所说,鉴于提供的信息,很难回答。