wordpress:如何在页面上添加类别和标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14323582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:07:54  来源:igfitidea点击:

wordpress : how to add categories and tags on pages?

wordpress

提问by Matoeil

I have generated pages using a custom template by creating a php file in my theme directory something like :

我通过在我的主题目录中创建一个 php 文件来使用自定义模板生成页面,例如:

<?php
 *
 * Template Name: Contact Page
 */
 ?>
 <html ..... </html>

and then adding a new page on the dashboard selecting this new template

然后在仪表板上添加一个新页面,选择这个新模板

How can i now associate tags and categories to each pages ? Is creating posts instead of pages the only solution?

我现在如何将标签和类别关联到每个页面?创建帖子而不是页面是唯一的解决方案吗?

回答by Matoeil

Even better is to add to functions.php in your theme folder:

更好的是添加到您的主题文件夹中的functions.php:

function myplugin_settings() {  
    // Add tag metabox to page
    register_taxonomy_for_object_type('post_tag', 'page'); 
    // Add category metabox to page
    register_taxonomy_for_object_type('category', 'page');  
}
 // Add to the admin_init hook of your theme functions.php file 
add_action( 'init', 'myplugin_settings' );

回答by Robin Carlo Catacutan

Tried using the accepted answer but for some reason it only shows the Post types and none of the Pages shows in the category page. E.g. /category/entertainment/

尝试使用接受的答案,但由于某种原因,它只显示帖子类型,类别页面中没有显示任何页面。例如/类别/娱乐/

To fix that, I have to do this:

为了解决这个问题,我必须这样做:

// add tag and category support to pages
function tags_categories_support_all() {
  register_taxonomy_for_object_type('post_tag', 'page');
  register_taxonomy_for_object_type('category', 'page');  
}

// ensure all tags and categories are included in queries
function tags_categories_support_query($wp_query) {
  if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
  if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
}

// tag and category hooks
add_action('init', 'tags_categories_support_all');
add_action('pre_get_posts', 'tags_categories_support_query');

回答by Anne

Not at all helpful to say 'download plugin' for beginners who are most likely not going to have downloaded wordpress and are therefore not able to install said plugin. Here is some short code for those like me that have been scouring the web for something that actually works on regular pages with regular accounts - ie you're not a developer.

对于很可能不会下载wordpress并因此无法安装所述插件的初学者来说,说“下载插件”完全没有帮助。这里有一些简短的代码,适用于像我这样一直在网上搜索可以在具有常规帐户的常规页面上实际运行的内容的人 - 即您不是开发人员。

First, make sure you have your pages in your menu set up properly. YOU DO NOT NEED TO MAKE YOUR PAGES 'Categories' or 'Tags'! This wouldn't give you actual pages to then go and edit, so if you are wanting to add sliders, text, an intro, or anything for that matter, you wouldn't be able to.

首先,确保您的菜单中的页面设置正确。您无需将页面设为“类别”或“标签”!这不会给你实际的页面然后去编辑,所以如果你想添加滑块、文本、介绍或任何与此相关的内容,你将无法添加。

Then go to WP Admin > Pages Select a page to edit and go to the text editor instead of visual editor (far right hand side tab)

然后转到 WP Admin > Pages 选择要编辑的页面并转到文本编辑器而不是可视化编辑器(最右侧的选项卡)

Then past the following short code:

然后通过以下短代码:

[display-posts category="hair,makeup,reviews,beauty" posts_per_page="10" include_date="true" text-decoration: none date_format="F j, Y" order="DESC" include_excerpt="true" wrapper="div" image_size="large"]
&lt;

(The shortcode collects all the posts that you have assigned certain categories in your blog posts i.e. mine was hair and beauty. So obviously change yours to ones that are appropriate. It then allocates how many posts (mine was 10), the date (in descending order,) with a large image and an excerpt of the post)

(短代码收集了您在博客帖子中分配了某些类别的所有帖子,即我的头发和美容。因此显然将您的帖子更改为合适的帖子。然后分配多少帖子(我的是 10),日期(在降序,) 带有大图和文章摘录)

回答by Fred K

Try this:

尝试这个:

add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats(){
    register_taxonomy_for_object_type('post_tag', 'page');
    register_taxonomy_for_object_type('category', 'page'); 
}

回答by Matoeil

this plugin sorted me out :

这个插件帮我解决了:

http://wordpress.org/extend/plugins/add-tags-and-category-to-page/

http://wordpress.org/extend/plugins/add-tags-and-category-to-page/

with the standard instructions :

使用标准说明:

Upload the plugin files to the /wp-content/plugins/ directory
Activate the plugin through the 'Plugins' menu in WordPress
Use the setting page of the plugin from Settings > Add Tags And Category For Page.