我制作了自定义分类法:
function project_register_taxonomy(){
$singular = 'Project Categories';
$plural = 'Projects Categories';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . ' found.',
'menu_name' => $plural,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => $$slug ),
);
register_taxonomy ('Project Categories','projects', $args);
}
add_action( 'init', 'project_register_taxonomy');
并将其注册到自定义帖子:
function create_galblock() {
register_post_type( 'projects',
// CPT Options
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Project' )
),
'taxonomies' => array('category'),
'description' => 'Projects by Almog',
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'projects'),
'supports' => array( 'title','editor', 'thumbnail')
)
);
}
add_action( 'init', 'create_galblock' );
我需要获取自定义分类“项目类别”的自定义帖子“项目”的帖子标题,发布缩略图和内容。
这是我尝试至少实现内容的代码:
$args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'Projects Categories',
'field' => 'id',
'terms' => '8'
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
//content
endwhile;
?>
显然,它不会从自定义分类中返回自定义帖子。
答案 0 :(得分:1)
这来自documentation:
$ taxonomy :(字符串)(必填)分类法的名称。名称应 仅包含小写字母和下划线字符 ,且长度不得超过32个字符(数据库结构限制)。
您指定了项目类别,如上所示,它不会很好。
register_taxonomy ('project_categories','projects', $args);
<强>更新强>
您还必须修改您的查询:
$args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projects_categories',
'field' => 'term_id',
'terms' => '8'
)
)
);
请注意tax_query
:
taxonomy
应为projects_categories
field
应为term_id
答案 1 :(得分:1)
好的,所以我让这个工作:
function project_register_taxonomy(){
$singular = 'Project Categories';
$plural = 'Projects Categories';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . ' found.',
'menu_name' => $plural,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => $slug ),
);
register_taxonomy ('project_categories','projects', $args);
}
add_action( 'init', 'project_register_taxonomy');
function create_galblock() {
register_post_type( 'projects',
// CPT Options
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Project' )
),
'taxonomies' => array('category'),
'description' => 'Projects by Almog',
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'projects'),
'supports' => array( 'title','editor', 'thumbnail')
)
);
}
add_action( 'init', 'create_galblock' );
我使用page-projects.php
模板创建了一个测试页面,看起来像这样
<?php
/**
* The template for displaying project pages
* Template Name: Projects Page
*/
get_header(); ?>
<div id="primary" class="content-area">
<h3>Projects</h3>
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'project_categories',
'field' => 'term_taxonomy_id',
'terms' => 61,
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo the_title();
endwhile;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
现在我添加了一个测试项目类别,其中tag_id
61(当您将鼠标悬停在它上面时可以看到它),以及该类别中的测试帖。
它显示发布就好了。现在如果你遗漏了fields
中的terms
和tax_query
,这就不行了。但我首先要把它排除在外,然后把所有的项目都拿出来(以后再搞清楚)。
希望这有帮助。