小工具      在线工具  汉语词典  dos游戏  css  js  c++  java

wordpress 自定义_如何在WordPress中为自定义帖子类型添加类别

java,python,php,js,javascript,ViewUI 额外说明

收录于:17天前

WordPress 定制

Recently one of our user asked us if it was possible to add 类别 to a 自定义帖子类型 they have created. Categories are one of the built-in 分类法 in WordPress. By default they appear only for posts. However, in some scenarios you may want to share them with a custom post type as well. In this article, we will show you how to add categories to a custom post type in WordPress. We will also show you how to display multiple post types on your category archive page.

最近,我们的一位用户问我们是否可以将类别添加到他们创建的自定义帖子类型 。 类别是WordPress中的内置分类之一。 默认情况下,它们仅显示在帖子中。 但是,在某些情况下,您可能还需要与自定义帖子类型共享它们。 在本文中,我们将向您展示如何在WordPress中为自定义帖子类型添加类别。 我们还将向您展示如何在类别存档页面上显示多种帖子类型。

Adding categories to a custom post type
插件方法 The Plugin Method

For our beginner level users, we recommend using Custom Post Type UI plugin to 创建自定义帖子类型. When using Custom Post Type UI plugin, you have the option to associate your custom post type to any built-in or custom taxonomy including categories.

对于初学者用户,我们建议使用“自定义帖子类型” UI插件来创建自定义帖子类型 。 使用“自定义帖子类型” UI插件时,您可以选择将自定义帖子类型与任何内置或自定义分类法(包括类别)相关联。

First you need to install and activate the 自定义帖子类型 UI plugin. For more details, see our step by step guide on 如何安装 WordPress 插件.

首先,您需要安装并激活“ 自定义帖子类型”UI插件。 有关更多详细信息,请参阅有关如何安装 WordPress 插件步指南。

Upon installation, you need to visit CPT UI » 添加/编辑帖子类型 to create a new custom post type or edit an existing custom post type you created with the plugin.

安装后,您需要访问CPT UI » 添加/编辑帖子类型以创建新的自定义帖子类型或编辑使用插件创建的现有自定义帖子类型。

Editing post types with CPT UI plugin

Scroll down on the Advanced Options to the bottom and there you will see the 内置分类法 option. Check the box next to categories and save your custom post type.

向下滚动到底部的“高级选项”,您将在里面看到“ 内置分类法”选项。 选中类别旁边的框,然后保存您的自定义帖子类型。

Turn on categories for a Custom Post Type in WordPress

不要忘记单击“保存帖子类型”按钮来存储您的设置。

不要忘记单击“保存帖子类型”按钮来存储您的设置。

手动将类别添加到自定义帖子类型 Manually Adding Categories to a Custom Post Type

If you created your custom post type by adding the code in your theme’s 函数.php file or a 特定于站点的插件, then you will have to modify the code to add category as supported taxonomy.

如果您通过在主题的函数.php文件或具体的在网站的插件中添加代码来创建自定义帖子类型,则必须修改代码以将类别添加为受支持的分类法。

您所需要做的就是在 CPT 的参数中添加这一行。

您所需要做的就是将此行添加到 CPT 的参数中。


'taxonomies'  => array( 'category' ),

您的 CPT 的现有代码中可能已经有这一行,其中还包含一些其他自定义分类法。如果这样做,那么您只需要在后面添加一个逗号并添加类别,如下所示:

此行可能已经包含在 CPT 的现有代码中,以及一些其他自定义分类法。如果这样做,您只需在其后添加一个逗号并添加类别,如下所示:


'taxonomies'		  => array('topics', 'category' ),

这是一个完整的示例代码,我们在其中创建了一个名为“电影”的自定义帖子类型,并支持内置类别。

这是完整的示例代码,我们在其中创建了一个名为“电影”的自定义帖子类型,并支持内置类别。


function custom_post_type() {

// Set UI labels for Custom Post Type
	$labels = array(
		'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
		'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
		'menu_name'           => __( 'Movies', 'twentythirteen' ),
		'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
		'all_items'           => __( 'All Movies', 'twentythirteen' ),
		'view_item'           => __( 'View Movie', 'twentythirteen' ),
		'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
		'add_new'             => __( 'Add New', 'twentythirteen' ),
		'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
		'update_item'         => __( 'Update Movie', 'twentythirteen' ),
		'search_items'        => __( 'Search Movie', 'twentythirteen' ),
		'not_found'           => __( 'Not Found', 'twentythirteen' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
	);
	
// Set other options for Custom Post Type
	
	$args = array(
		'label'               => __( 'movies', 'twentythirteen' ),
		'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
		'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 );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

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

在类别页面上显示多种帖子类型 Displaying Multiple Post Types on Category Page

By default the 类别页面 on your WordPress site will only display the default ‘Posts’ post type. To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php or a site-specific plugin.

默认情况下,WordPress网站上的类别页将仅显示默认的“帖子”帖子类型。 要在与默认帖子相同的类别页面上显示自定义帖子类型,您需要将此代码添加到主题的functions.php或特定于站点的插件中。


add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if( is_category() ) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item', 'post', 'movies'); // don't forget nav_menu_item to allow menus to work!
    $query->set('post_type',$post_type);
    return $query;
    }
}

不要忘记将电影替换为您自己的自定义帖子类型的名称。

不要忘记将电影替换为您自己的自定义帖子类型的名称。

就这样,我们希望本文能帮助您在 WordPress 中向自定义帖子类型添加类别。您也可以使用相同的方法将标签添加到自定义帖子类型。

就这样,我们希望本文能帮助您在 WordPress 中向自定义帖子类型添加类别。您还可以使用相同的方法将标签添加到自定义帖子类型。

If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and 谷歌+.

如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特谷歌+上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-categories-to-a-custom-post-type-in​​-wordpress/

WordPress 定制

. . .

相关推荐

额外说明

Spring Bean作用域(Scope)原理及源码分析

Spring内常见的Scope有singleton,prototype,request,session;还有globalSession,application。 如果用注解定义Bean的作用域,则可以使用@Scope,将@Scope标识在一个Bean的类

额外说明

MyBatis学习详细笔记(持续更新中...)

一、MyBatis 框架简介 MyBatis 是一个优秀的基于 java 的持久层框架,内部封装了 jdbc,开发者只需要关注 sql 语句本身,而不需要处理加载驱动、创建连接、创建 statement、关闭连接,资源等繁杂的过程。MyBatis 通过

额外说明

【Python入门教程】第44章 集合求导

本篇我们学习如何利用集合推导式(set comprehension)基于已有集合创建一个新的集合。 集合推导式简介 tags 是由三个标签组成的集合: tags = { 'Django', 'Pandas', 'Numpy'} 为了将集合中的标签转换为

额外说明

企业级实战——畅购商城SpringCloud-JAVA实战商城管理后台——工具工程搭建和工具类导入

QQ 1274510382 Wechat JNZ_aming 商业联盟 QQ群538250800 技术搞事 QQ群599020441 解决方案 QQ群152889761 加入我们 QQ群649347320 共享学习 QQ群674240731 纪年科技am

额外说明

SpringBoot+SpringSecurity+dubbo图书电商后台实战-课程介绍,架构说明,案例说明,前置知识

QQ 1274510382 Wechat JNZ_aming 商业联盟 QQ群538250800 技术搞事 QQ群599020441 解决方案 QQ群152889761 加入我们 QQ群649347320 共享学习 QQ群674240731 纪年科技am

额外说明

MySQL 5.7限制general_log日志大小

背景 需求: 在MySQL 5.7.41中开启general_log 并限制其大小,避免快速增长占用硬盘空间。 解决: 通过定时任务,执行简单的脚本,判断general_log 日志的大小,实现对通用查询日志的“每日备份”或“每日清理”的功能。 多说几句

额外说明

【Unity VR开发窍门】如何在Unity中以VR视角捕捉游戏360度全景

【背景】 经常看到有VR游戏中玩家可以在虚拟场景中拍照的功能,所以打算做一篇系列帖子,主要介绍三个功能,第一个功能是如何在Unity VR项目中从玩家视角捕捉游戏全景,第二个功能是如何将这个捕捉到的全景转换为一般的平面图,第三个功能是如何将这个平面图保存

额外说明

使用UiPath和AA构建的解决方案 5. 使用UiPath ReFramework处理采购订单

在本章中,我们将使用UiPath Robotic Enterprise Framework(简称ReFramework)创建自动化。ReFramework是一个快速构建强大的UiPath自动化的模板。它可以作为所有UiPath项目的起点。 模板可以满足您

额外说明

sliverlight: tagControl dynamic generate headerTemplate have problem

.Background I want to create the TagItem by code , It is easy job. new TagItem and set headerTeamplate and Content  code here:

额外说明

详解javaScript的事件中,复杂数据类型的传参(数组,对象,函数)

文章目录 前言 一、何谓预编译,变量提升? 二、复杂数据类型的传递 1.数组 2.对象 3.函数 总结   前言 在JavaScript这门编程语言学习中,有太多关键问题,比如如何传参,什么是变量提升,js代码预编译是怎么回事等等。要想成为一名优秀的js

ads via 小工具