Custom post types category

时间:2014-08-16 作者:kamran shahid

我创建了一个名为“project”的自定义帖子类型的主题,但出现了一个问题。我想在一个页面的不同部分显示5个不同的自定义帖子类型类别,就像在页面上显示各自类别的帖子一样。有什么解决办法吗?

这是我使用自定义分类法的自定义帖子类型代码

 <?php
    /**
     * Registers a custom post type.
     */
    function Project_post_type()
    {
        register_post_type(
            \'Project\',
            array(
    \'labels\'               => array(
    \'name\'                => __(\'Project\', \'text_domain\'),
    \'singular_name\'       => __(\'Project\', \'text_domain\'),
    \'menu_name\'           => __(\'Project\', \'text_domain\'),
    \'name_admin_bar\'      => __(\'Project Item\', \'text_domain\'),
    \'all_items\'           => __(\'All Items\', \'text_domain\'),
    \'add_new\'             => _x(\'Add New\', \'prefix_portfolio\', \'text_domain\'),
    \'add_new_item\'        => __(\'Add New Item\', \'text_domain\'),
    \'edit_item\'           => __(\'Edit Item\', \'text_domain\'),
    \'view_item\'           => __(\'View Item\', \'text_domain\'),
    \'search_items\'        => __(\'Search Items\', \'text_domain\'),
    \'not_found\'           => __(\'No items found.\', \'text_domain\'),
    \'not_found_in_trash\'  => __(\'No items found in Trash.\', \'text_domain\'),
    \'parent_item_colon\'   => __(\'Parent Items:\', \'text_domain\'),
                ),
    \'public\'     => true,
    \'menu_position\'  => 5,
    \'supports\'    => array(
                    \'title\',
                    \'editor\',
                    \'thumbnail\',
                    \'excerpt\',
                    \'custom-fields\',
                ),
    \'taxonomies\'   => array(
                    \'Project_categories\',
                ),

    \'menu_icon\'   => get_template_directory_uri() . \'/img/project-icon.png\',
    \'has_archive\' => true,
    \'query_var\'  => true,
    \'rewrite\' => array(
    \'slug\'    => \'Project\',
    ),
    )
   );
    }

    add_action(\'init\', \'Project_post_type\');


    /**
     * Registers a custom taxonomy.
     */
    function Project_taxonomy()
    {
        register_taxonomy(
            \'Project_categories\',
            array(
                \'Project\',
            ),
    array(
    \'labels\'               => array(
    \'name\'                => _x(\'Categories\', \'prefix_portfolio\', \'text_domain\'),
    \'singular_name\'       => _x(\'Category\', \'prefix_portfolio\', \'text_domain\'),
    \'menu_name\'           => __(\'Categories\', \'text_domain\'),
    \'all_items\'           => __(\'All Categories\', \'text_domain\'),
    \'edit_item\'           => __(\'Edit Category\', \'text_domain\'),
    \'view_item\'           => __(\'View Category\', \'text_domain\'),
    \'update_item\'         => __(\'Update Category\', \'text_domain\'),
    \'add_new_item\'        => __(\'Add New Category\', \'text_domain\'),
    \'new_item_name\'       => __(\'New Category Name\', \'text_domain\'),
    \'parent_item\'         => __(\'Parent Category\', \'text_domain\'),
     \'parent_item_colon\'   => __(\'Parent Category:\', \'text_domain\'),
    \'search_items\'        => __(\'Search Categories\', \'text_domain\'),
                ),
    \'show_admin_column\'    => true,
    \'hierarchical\'   => true,
   \'rewrite\'   => array(
  \'slug\'    => \'portfolio/category\',
 ),
)
);
}

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

function theme_prefix_rewrite_flush() {
flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'theme_prefix_rewrite_flush\' );
这是我显示一个类别的代码

$args = array(
\'post_type\' => \'project\',
\'posts_per_page\' => 200, 
\'cat\' => \'Initiatieffase\'
    );
$loop = new WP_Query($args); while ($loop->have_posts()) : $loop->the_post();
echo \'<li><a href="\' . the_permalink() . \'">\' . the_post_thumbnail() . \'</a></li>\';
endwhile;
wp_reset_query();

EDIT

这是我的新post type函数

<?php
// Register Custom Post Type
function project_post_type() {

    $labels = array(
        \'name\'                => _x( \'Projects\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Project\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Project\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Items\', \'text_domain\' ),
        \'view_item\'           => __( \'View Item\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Item\', \'text_domain\' ),
        \'add_new\'             => __( \'Add New\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Item\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Item\', \'text_domain\' ),
        \'not_found\'           => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' ),
    );
    $args = array(
        \'label\'               => __( \'project_post_type\', \'text_domain\' ),
        \'description\'         => __( \'Project Description\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\', ),
        \'taxonomies\'          => array( \'category\', \'post_tag\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_icon\' => get_template_directory_uri() . \'/img/project-icon.png\',
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
    );
    register_post_type( \'project_post_type\', $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'project_post_type\', 0 );


// Register Custom Taxonomy
function project_taxonomy() {

    $labels = array(
        \'name\'                       => _x( \'Projects\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Project\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Project\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Items\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'text_domain\' ),
        \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
    );
    register_taxonomy( \'project\', array( \'post\' ), $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'project_taxonomy\', 0 );


function theme_prefix_rewrite_flush() {
flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'theme_prefix_rewrite_flush\' );
这是模板函数

<?php
  $args = array(
  \'post_type\' => \'project\',
   \'tax_query\' => array(
    array(
    \'taxonomy\' => \'Initiatieffase\',
     \'field\' => \'slug\',
      \'terms\' => \'bob\'
       )
     )
  );
$query = new WP_Query( $args );
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
 <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
但还是什么都没表现出来

这是我的新职位类型功能

<?php
// Register Custom Post Type
function project_post_type() {

    $labels = array(
        \'name\'                => _x( \'Projects\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Project\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Project\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Items\', \'text_domain\' ),
        \'view_item\'           => __( \'View Item\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Item\', \'text_domain\' ),
        \'add_new\'             => __( \'Add New\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Item\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Item\', \'text_domain\' ),
        \'not_found\'           => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' ),
    );
    $args = array(
        \'label\'               => __( \'project_post_type\', \'text_domain\' ),
        \'description\'         => __( \'Project Description\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\', ),
        \'taxonomies\'          => array( \'category\', \'post_tag\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_icon\' => get_template_directory_uri() . \'/img/project-icon.png\',
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
    );
    register_post_type( \'project_post_type\', $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'project_post_type\', 0 );


// Register Custom Taxonomy
function project_taxonomy() {

    $labels = array(
        \'name\'                       => _x( \'Projects\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Project\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Project\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Items\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'text_domain\' ),
        \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
    );
    register_taxonomy( \'project\', array( \'post\' ), $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'project_taxonomy\', 0 );


function theme_prefix_rewrite_flush() {
flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'theme_prefix_rewrite_flush\' );
这是模板函数

<?php
  $args = array(
  \'post_type\' => \'project\',
   \'tax_query\' => array(
    array(
    \'taxonomy\' => \'Initiatieffase\',
     \'field\' => \'slug\',
      \'terms\' => \'bob\'
       )
     )
  );
$query = new WP_Query( $args );
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
 <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
但还是什么都没表现出来

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

您在命名约定、函数结构、保留名称的无效使用以及传递给参数的错误值方面存在严重问题

首先,命名约定。函数名称应为小写字母,不应以字母开头,单词应以下划线分隔(_). 自定义帖子类型和分类名称也是如此。放下你的大写字母。重写规则也是如此

其次,不要创建多个应该连接到同一个函数的实例。在中正确地将代码分组one 函数,并将该函数添加到预期的挂钩中,在本例中init

第三,不要使用保留名称,甚至不要重写slug。这很混乱,以后可能会与模板层次结构发生冲突。在你的情况下,你已经使用category, 这是一个保留名称。

最后,查看codex,了解应该传递给特定参数的正确值。cat 除类别ID外,不包括Slug或name。顺便说一句,您正在使用自定义分类法,因此应该使用tax_query 参数,而不是类别参数。

供参考

EDIT

有一件事我忘了补充,wp_reset_query 应与一起使用query_posts, 你永远不应该使用它。无论何时使用WP_Queryget_posts, 您需要使用重置自定义查询wp_reset_postdata

EDIT 2

你错过了我说过的几点。令人困惑的是,你已经改变了一些你应该做的事情。在我发布代码之前,只需要一些额外的东西

当您创建自定义分类法,并且需要它像内置类别一样工作时,您需要设置hierarchical 为真。如果将其设置为false,则自定义分类法的行为类似于内置的post标记

每当您在自定义分类法中创建“类别”时,它都被称为术语。

你的分类法叫做project, 但是你通过了Initiatieffase (我认为这可能是分类学的一个术语project 到分类参数。

当您在tax_queryslug, 您必须使用slug 您在terms 参数not the name 本学期的。

您的自定义帖子类型被称为project_post_type 但是你通过了project (这是您的分类名称)到post_type 自定义查询中的参数

这就是您的代码应该是什么样子

<?php
// Register Custom Post Type
function create_cpt_and_ct() {

    $labels1 = array(
        \'name\'                => _x( \'Projects\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Project\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Project\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Items\', \'text_domain\' ),
        \'view_item\'           => __( \'View Item\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Item\', \'text_domain\' ),
        \'add_new\'             => __( \'Add New\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Item\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Item\', \'text_domain\' ),
        \'not_found\'           => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' ),
    );
    $args1 = array(
        \'label\'               => __( \'project_post_type\', \'text_domain\' ),
        \'description\'         => __( \'Project Description\', \'text_domain\' ),
        \'labels\'              => $labels1,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\', ),
        \'taxonomies\'          => array( \'project\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_icon\'           => get_template_directory_uri() . \'/img/project-icon.png\',
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
    );
    register_post_type( \'project_post_type\', $args1 );

    $labels2 = array(
        \'name\'                       => _x( \'Projects\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Project\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Project\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Items\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'text_domain\' ),
        \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
    );
    $args2 = array(
        \'labels\'                     => $labels2,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
    );
    register_taxonomy( \'project\', \'project_post_type\', $args2 );

}

// Hook into the \'init\' action
add_action( \'init\', \'create_cpt_and_ct\', 0 );


function theme_prefix_rewrite_flush() {
flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'theme_prefix_rewrite_flush\' );
为了你的tax_query

<?php
$args = array(
    \'post_type\' => \'project_post_type\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'project\',
            \'field\' => \'slug\',
            \'terms\' => \'Initiatieffase\' //THIS MUST BE YOUR TERM SLUG, NOT NAME
        )
    )
);
$query = new WP_Query( $args );
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); 
?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
    </li>
<?php 
endwhile; 

wp_reset_postdata();
?>

ADDITIONAL READING

-Debugging in Wordpress

EDIT 3

要正确命名和正确使用模板,请检查Template Hierarchy 在法典中。这对你将来会有很大帮助

您的单个页面将是single-{$post\\u type}。转换为single-project\\u post\\u类型的php。php。存档页面也是如此。

对于自定义分类法页面,可以使用taxonomy-$taxonomy。php转换为分类法项目。php

结束

相关推荐

从WP_LIST_CATEGORIES中删除‘Category’一词

我有以下代码:wp_list_categories();它输出我的所有类别,但是,它使它们成为列表元素的子元素,称为CATEGORIES.所以,我得到了一个无序的列表,如下所示:CATEGORIES * FASHION * DAILY FASHION CANDY * TRENDS * BEAUTY * ACCESSORIES * CELEBRITIES * LIFESTYLE 但是,我只需要:* FASHION * DAILY FA