hosting tutorials WeChat Top

👋🏼Welcome to my WP-Host blog where I am excited to share my knowledge and expertise on WordPress hosting and website construction tutorials with you. Let’s connect and learn from each other! You can reach me at info@yrshare.com.

Email Me

(如果你会中文,可以点击微信图标与我联系。)

扫一扫加我

注:因个人英文水平有限,所以暂时只能为懂中文的朋友提供wordpress建站服务

微信:18200592859 或 yrwordpress

How to add category and tags to pages of WordPress website?

How to add category and tags to pages of WordPress website? Why do wordpress articles have tags, but pages don’t? Why? Because generally the most content in a website is the article, the label will be more useful when there are many articles, and the independent pages of a website are generally relatively small, and most of them may only have a few pages. In this case, use If there is no label, there is no need for classification. If you must add classification and label attributes to the page, you can refer to the wordpress tutorial I shared today.

【WP-Host/悦然wordpress建站】
2023031907451340

Continue to share the wordpress tutorials. WordPress content can be updated in two forms: articles and pages. The basic content editing is the same, but WordPress pages cannot add tags and categories.
So in most cases, we update content through articles and rarely use pages. If you use more pages, you can refer to the following methods to add categories and labels to pages.

Use plugin

Using a plug-in is the easiest way. You can search and download a plugin – Post Tags and Categories for Pages, and then install it. No special settings are required.

Use code

If your wordpress website already uses a lot of plugins, or this plugin is incompatible with your current wordpress version theme, you can try to use code directly to add tags and classification attributes.

The code to share with you is as follows: the content is extracted from the plugin:

//Add tags and categories to Word Press pages
class PTCFP{
 function __construct(){
 add_action( 'init', array( $this, 'taxonomies_for_pages' ) );
 /**
 * Make sure that these query modifications do not affect the admin background
 */
 if ( ! is_admin() ) {
 add_action( 'pre_get_posts', array( $this, 'category_archives' ) );
 add_action( 'pre_get_posts', array( $this, 'tags_archives' ) );
 } // ! is_admin
 } // __construct
 /**
 * Add "Tags" and "Categories" to "Pages"
 *
 * @uses register_taxonomy_for_object_type
 */
 function taxonomies_for_pages() {
 register_taxonomy_for_object_type( 'post_tag', 'page' );
 register_taxonomy_for_object_type( 'category', 'page' );
 } // taxonomies_for_pages
 /**
 * Include "pages" in the tag archive
 */
 function tags_archives( $wp_query ) {
 if ( $wp_query->get( 'tag' ) )
 $wp_query->set( 'post_type', 'any' );
 } // tags_archives
 /**
 * Include "pages" in Category Archives
 */
 function category_archives( $wp_query ) {
 if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
 $wp_query->set( 'post_type', 'any' );
 } // category_archives
} // PTCFP
$ptcfp = new PTCFP();

Operation method: copy the above code, add it to the functions.php file of the current wordpress theme template, save it, and refresh the background.

Effect

2023031907453252

After using a plugin or code to successfully add tags and category attributes to a page, a category and label menu will appear under the page following WordPress. The content is the same as the article. You can also directly assign categories and labels to the page in the quick editing section on the right.

Summarize

Adding categories or tags to wordpress pages is a very niche requirement, generally speaking, it doesn’t make much sense, and it is generally not recommended for everyone to do so. After the website is completed, the most important thing is to update the basic website content, and don’t always worry about some less important functions or details.

The existence of wordpress pages has other more important meanings, such as some more complex design or development, and the pages can be used to design a separate home page or special page.

2023031907451340

继续分享wordpress建站教程。wordpress内容更新可以通过文章和页面两种形式,基本的内容编辑都是一样的,不过wordpress的页面不能添加标签和分类。所以大多数情况下我们都是通过文章在更新内容,很少用页面,如果你页面使用的比较多,那么可以参考下面的方法给页面加上分类和标签。

使用插件

使用插件是最简单的方法,大家可以去搜索下载一个插件-Post Tags and Categories for Pages,然后安装就可以了,并不需要特别的设置。

使用代码

如果你的wordpress网站已经使用了比较多的插件,或者是这个插件与你当前的wordpress版本是主题不兼容,那么可以尝试直接使用代码来添加标签和分类属性。

给大家分享的代码如下:内容是从插件中提取出来的

//为WordPress页面添加标签和分类
class PTCFP{
 function __construct(){
 add_action( 'init', array( $this, 'taxonomies_for_pages' ) );
 /**
 * 确保这些查询修改不会作用于管理后台,防止文章和页面混杂 
 */
 if ( ! is_admin() ) {
 add_action( 'pre_get_posts', array( $this, 'category_archives' ) );
 add_action( 'pre_get_posts', array( $this, 'tags_archives' ) );
 } // ! is_admin
 } // __construct
 /**
 * 为“页面”添加“标签”和“分类”
 *
 * @uses register_taxonomy_for_object_type
 */
 function taxonomies_for_pages() {
 register_taxonomy_for_object_type( 'post_tag', 'page' );
 register_taxonomy_for_object_type( 'category', 'page' );
 } // taxonomies_for_pages
 /**
 * 在标签存档中包含“页面”
 */
 function tags_archives( $wp_query ) {
 if ( $wp_query->get( 'tag' ) )
 $wp_query->set( 'post_type', 'any' );
 } // tags_archives
 /**
 * 在分类存档中包含“页面”
 */
 function category_archives( $wp_query ) {
 if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
 $wp_query->set( 'post_type', 'any' );
 } // category_archives
} // PTCFP
$ptcfp = new PTCFP();

操作方法:复制上面的代码,把它添加到当前wordpress主题模板的functions.php文件中,然后保存,刷新后台即可。

使用效果

2023031907453252

当使用插件或代码成功给页面添加标签和分类属性后,在wordpress后面的页面下就会多出分类和标签菜单,内容与文章是一样的,右边的页面快速编辑中也可以直接给页面指定分类,添加标签。

总结

给wordpress页面添加分类或标签是一个非常小众的要求,总的来说没有太大的意义,一般情况下并不建议大家这样去做。网站做好后最重要的还是做好基本的网站内容更新,不要总去纠结一些不太重要的功能或细节。

wordpress页面的存在有其它更重要的意义,比如进行一些更复杂设计或开发,而且页面可以用来设计单独的首页或专题页。

WordPress Hosting / 悦然wordpress建站

WordPress Hosting / 悦然wordpress建站

【WordPress Hosting / 悦然wordpress建站】I am a WordPress hobby from China, and I like to communicate with friends from all over the world to learn WordPress website building and maintenance related knowledge.