Multiple post types

时间:2014-11-18 作者:sixli

我正在使用Wordpress的“类型”插件创建两种自定义帖子类型,效果很好。我在archive slug的基础上构建了自定义模板。其中“slug”是post类型的slug。它可以很好地应用所有内容,但在我的导航中,我打开了一个自定义的帖子类型,但它会将默认帖子类型的页面标记为活动页面。

似乎存在冲突,WP无法确定要将哪个菜单项标记为活动,因此它使用作为默认帖子类型的类别页面的菜单项,而不是自定义类型。

有什么想法吗?

1 个回复
SO网友:Ariful Islam

是否要多个自定义网站帖子?您告诉我您已经为您的网站创建了一个自定义帖子。别担心。现在我会帮你解决你的问题

首先,在您的和粘贴功能上创建另一个自定义帖子。php或自定义帖子。php文件。现在我将创建自定义帖子Movie 类型。你可以改变它Movie 将其替换为自定义帖子类型名称。

    function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Movies\', \'Post Type General Name\' ),
        \'singular_name\'       => _x( \'Movie\', \'Post Type Singular Name\'),
        \'menu_name\'           => __( \'Movies\'),
        \'parent_item_colon\'   => __( \'Parent Movie\' ),
        \'all_items\'           => __( \'All Movies\'),
        \'view_item\'           => __( \'View Movie\'),
        \'add_new_item\'        => __( \'Add New Movie\'),
        \'add_new\'             => __( \'Add New\' ),
        \'edit_item\'           => __( \'Edit Movie\' ),
        \'update_item\'         => __( \'Update Movie\' ),
        \'search_items\'        => __( \'Search Movie\'),
        \'not_found\'           => __( \'Not Found\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\'),
    );

// Set other options for Custom Post Type

    $args = array(
        \'label\'               => __( \'movies\' ),
        \'description\'         => __( \'Movie news and reviews\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',

        // This is where we add taxonomies to our CPT
        \'taxonomies\'          => array( \'category\' ),
    );

    // Registering your Custom Post Type
    register_post_type( \'movies\', $args );

}



add_action( \'init\', \'custom_post_type\', 0 );

Displaying Multiple Post Types

<?php 
$args = array( \'post_type\' => \'movies\', \'posts_per_page\' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?> 
</div>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; ?>

结束

相关推荐