显示来自自定义分类的自定义 post
Displaying a custom post from a custom taxonomy
我做了一个自定义分类:
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');
并将其注册到自定义 post:
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' );
我需要获取自定义分类 'Project Categories' 的 post 标题、post 自定义 post 缩略图和内容 'projects'。
这是我尝试实现至少内容的代码:
$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;
?>
显然它不 return 来自自定义分类的自定义 post。
这是来自documentation:
$taxonomy: (string) (required) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).
您指定了项目类别,正如您在上面看到的,这不是很好。
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
好的,所以我开始工作了:
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(当您将鼠标悬停在它上面时可以看到),以及该类别中的一个测试 post。
它表明 post 很好。现在,如果您在 tax_query
中遗漏了 fields
和 terms
,这将不起作用。但我会先把它排除在外,然后把所有的项目都拿出来(然后再弄清楚排序)。
希望对您有所帮助。
我做了一个自定义分类:
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');
并将其注册到自定义 post:
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' );
我需要获取自定义分类 'Project Categories' 的 post 标题、post 自定义 post 缩略图和内容 'projects'。
这是我尝试实现至少内容的代码:
$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;
?>
显然它不 return 来自自定义分类的自定义 post。
这是来自documentation:
$taxonomy: (string) (required) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).
您指定了项目类别,正如您在上面看到的,这不是很好。
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
好的,所以我开始工作了:
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(当您将鼠标悬停在它上面时可以看到),以及该类别中的一个测试 post。
它表明 post 很好。现在,如果您在 tax_query
中遗漏了 fields
和 terms
,这将不起作用。但我会先把它排除在外,然后把所有的项目都拿出来(然后再弄清楚排序)。
希望对您有所帮助。