wordpress标签页面
Tags have been supported in WordPress since version 2.3 was released in 2007. When used well, they can be a more effective form of navigation than categories or menus. But why has it never been possible to tag WordPress pages?
自2007年发布2.3版以来,WordPress就一直支持标签。使用得当,与类别或菜单相比,它们可以是更有效的导航形式。 但是为什么从来没有可能标记WordPress页面呢?
Fortunately, WordPress provides the tools to help us enable tags in pages and any other type of post. Beneath the surface, WordPress treats pages, posts and other content in much the same way; a page is just a custom post type. Therefore, enabling tags is simply a matter of saying “hey WordPress, I’d like to use tags on my pages and don’t forget to include them in the tag cloud!”
幸运的是,WordPress提供了工具来帮助我们在页面和任何其他类型的帖子中启用标签。 在表面之下,WordPress以几乎相同的方式处理页面,帖子和其他内容。 页面只是自定义帖子类型。 因此,启用标签只是说“嘿,WordPress,我想在页面上使用标签,别忘了将它们包含在标签云中!”。
Let’s convert that to code you can insert into your theme’s functions.php file (wp-content/themes/<themename>/functions.php):
让我们将其转换为可以插入主题的functions.php文件(wp-content / themes / <themename> /functions.php)中的代码:
// add tag support to pages function tags_support_all() { register_taxonomy_for_object_type('post_tag', 'page'); } // ensure all tags are included in queries function tags_support_query($wp_query) { if ($wp_query->get('tag')) $wp_query->set('post_type', 'any'); } // tag hooks add_action('init', 'tags_support_all'); add_action('pre_get_posts', 'tags_support_query');Simple. If you have further custom post types which require tags, you’ll need to add register_taxonomy_for_object_type calls for each — the second argument is the type name.
简单。 如果您还有其他需要标签的自定义帖子类型,则需要为每个标签添加register_taxonomy_for_object_type调用-第二个参数是类型名称。
Those running several WordPress sites or a network may find it easier to convert the code to a plugin so it can be enabled and disabled accordingly. In essence, that’s a matter of adding the code above to a suitably-named plugin file, i.e. wp-content/plugins/enable-tags.php, and placing comments at the top:
那些运行多个WordPress网站或网络的人可能会发现将代码转换为插件更加容易,因此可以相应地启用和禁用它。 本质上,这是将上面的代码添加到一个适当命名的插件文件中的问题,即wp-content / plugins / enable-tags.php,并在顶部放置注释:
<?php /* Plugin Name: Enable Tags in WordPress Pages Plugin URI: https://www.sitepoint.com/ Description: Enables tags in all content Version: 1.0 Author: Craig Buckler Author URI: http://twitter.com/craigbuckler License: Free to use and adapt */ // add tag support to pages // ... rest of code ...I hope you find it useful. Please use and adapt the code as you like in your own projects — a link back to this article is appreciated.
希望对你有帮助。 请在您自己的项目中随意使用和修改代码-感谢您返回本文的链接。
翻译自: https://www.sitepoint.com/wordpress-pages-use-tags/
wordpress标签页面