wordpress : how to add categories and tags on page

2019-03-18 00:28发布

问题:

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

<?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?

回答1:

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

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' );


回答2:

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');


回答3:

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'); 
}


回答4:

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.

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)

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)



回答5:

this plugin sorted me out :

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.


标签: wordpress