Plugin Update: Assign Categories to Pages – version 1.2

by Amit Verma on August 15, 2010

in Blogging

Gmail adds a new labs feature to undo send mail. This feature can retrieve the sent message.

Gmail Labs : Undo Send

Gmail Labs : Undo Send

Gmail will hold the message for 5 seconds after hitting send button. This feature is quite useful when you press send button accidently before finishing your message or forget to attach file. You don’t have write a “Oops” message because you can undo now. But remember that the message can be retrieve only with in 5 seconds.

It will also make your email to deliver in more then 5 seconds.

Gmail Labs : Undo Send

Gmail Labs : Undo Send

Don’t forget to enable the “Undo Send” feature from Gmail Labs. It is turned off by default.

Gmail adds a new labs feature to undo send mail. This feature can retrieve the sent message.

Gmail Labs : Undo Send

Gmail Labs : Undo Send

Gmail will hold the message for 5 seconds after hitting send button. This feature is quite useful when you press send button accidently before finishing your message or forget to attach file. You don’t have write a “Oops” message because you can undo now. But remember that the message can be retrieve only with in 5 seconds.

It will also make your email to deliver in more then 5 seconds.

Gmail Labs : Undo Send

Gmail Labs : Undo Send

Don’t forget to enable the “Undo Send” feature from Gmail Labs. It is turned off by default.

Map Categories to Pages

Latest Version: 1.1

Map Categories to Pages displays the category box on the “Edit Page” page. By default, ambulance
Every page in wordpress is mapped to the default category but this plugin gives the option to select categories for a pages. Read Plugin: Assign Categories to Pages – version 1.1 and Assign Categories to Pages in WordPress for more information.

Download from WordPress
WordPress has different options for organizing posts. One of them is Categories. The categories can only be assigned to posts, view not to pages in WordPress. But sometime you might want to categorize the pages as well. Here are a few cases.

  1. To display related posts on a page.
  2. To display pages and categories in navigation menu in mixed order. WordPress provides the two different functions to display a list of Pages and Categories. But it doesn’t give the option to setup a navigation menu mixed with pages and categories, prosthetic like the example given below. In such a case you can map pages to categories and use them as category pages. Obviously it will need some changes in the template.
    • Page 1
    • Page 2
    • Category 1
    • Page 3
    • Category 2
  3. To display pages on the category pages with the posts.

How to map Categories to pages

Add following lines of code in the function.php of your theme directory. If you don’t have a function.php in the theme directory then you can add it using any text editor. You can also download the plugin which I have made from http://wordpress.org/extend/plugins/map-categories-to-pages/.

function add_category_box_on_page(){
//add meta box
add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'low');
}

add_action('admin_menu', 'add_category_box_on_page');

It will show the category selection box on the right side on “Edit Page” page.

Template changes

You will also have to edit the page template to use categories. You can use standard category code on your page template. Take a look at http://codex.wordpress.org/Function_Reference/get_the_category and http://codex.wordpress.org/Template_Tags/the_category.

Here is a small piece of code to build a page of posts. This example uses the styles from WordPress default theme.

<?php
global $post;
$categories = get_the_category($post->ID);
$showposts = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => $categories,
'showposts' => $showposts,
'caller_get_posts' => $do_not_show_stickies
);
$my_query = new WP_Query($args);

?>

<?php if( $my_query->have_posts() ) : ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
//necessary to show the tags
global $wp_query;
$wp_query->in_the_loop = true;
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>

<p class="postmetadata"><?php the_tags('Tags: ', ', ', '
'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>

<?php endwhile; ?>

<?php else : ?>

<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>

<?php endif; ?>

Displaying pages on Category pages

When WordPress fetches the posts from database, the default post type is ‘post’. So even when you have mapped a page to a category, it won’t display the page on that category page. The post type needs to be changed explicitly on the category page. To change the post type, add following code in function.php.

//call a function just before the query runs to fetch posts
add_action('pre_get_posts','change_post_type');

function change_post_type($var) {
if(is_category()) {
$var->query_vars['post_type'] = 'any';
//it will change the value to 'any' from the default value of ;post';
//can be any, attachment,  page, post, or revision.
//'any' retrieves any type except revisions.
}
}

WordPress has different options for organizing posts. One of them is Categories. The categories can only be assigned to posts, more about not to pages in WordPress. But sometime you might want to categorize the pages as well. Here are a few cases.

  1. To display related posts on a page.
  2. To display pages and categories in navigation menu in mixed order. WordPress provides the two different functions to display a list of Pages and Categories. But it doesn’t give the option to setup a navigation menu mixed with pages and categories, like the example given below. In such a case you can map pages to categories and use them as category pages. Obviously it will need some changes in the template.
    • Page 1
    • Page 2
    • Category 1
    • Page 3
    • Category 2
  3. To display pages on the category pages with the posts.

How to map Categories to pages

Add following lines of code in the function.php of your theme directory. If you don’t have a function.php in the theme directory then you can add it using any text editor. You can also download the plugin which I have made from http://wordpress.org/extend/plugins/map-categories-to-pages/.

function add_category_box_on_page(){
//add meta box
add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'low');
}

add_action('admin_menu', 'add_category_box_on_page');

It will show the category selection box on the right side on “Edit Page” page.

Template changes

You will also have to edit the page template to use categories. You can use standard category code on your page template. Take a look at http://codex.wordpress.org/Function_Reference/get_the_category and http://codex.wordpress.org/Template_Tags/the_category.

Here is a small piece of code to build a page of posts. This example uses the styles from WordPress default theme.

<?php
global $post;
$categories = get_the_category($post->ID);
$showposts = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => $categories,
'showposts' => $showposts,
'caller_get_posts' => $do_not_show_stickies
);
$my_query = new WP_Query($args);

?>

<?php if( $my_query->have_posts() ) : ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
//necessary to show the tags
global $wp_query;
$wp_query->in_the_loop = true;
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>

<p class="postmetadata"><?php the_tags('Tags: ', ', ', '
'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>

<?php endwhile; ?>

<?php else : ?>

<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>

<?php endif; ?>

Displaying pages on Category pages

When WordPress fetches the posts from database, the default post type is ‘post’. So even when you have mapped a page to a category, it won’t display the page on that category page. The post type needs to be changed explicitly on the category page. To change the post type, add following code in function.php.

//call a function just before the query runs to fetch posts
add_action('pre_get_posts','change_post_type');

function change_post_type($var) {
if(is_category()) {
$var->query_vars['post_type'] = 'any';
//it will change the value to 'any' from the default value of ;post';
//can be any, attachment,  page, post, or revision.
//'any' retrieves any type except revisions.
}
}

WordPress has different options for organizing posts. One of them is Categories. The categories can only be assigned to posts, more about not to pages in WordPress. But sometime you might want to categorize the pages as well. Here are a few cases.

  1. To display related posts on a page.
  2. To display pages and categories in navigation menu in mixed order. WordPress provides the two different functions to display a list of Pages and Categories. But it doesn’t give the option to setup a navigation menu mixed with pages and categories, like the example given below. In such a case you can map pages to categories and use them as category pages. Obviously it will need some changes in the template.
    • Page 1
    • Page 2
    • Category 1
    • Page 3
    • Category 2
  3. To display pages on the category pages with the posts.

How to map Categories to pages

Add following lines of code in the function.php of your theme directory. If you don’t have a function.php in the theme directory then you can add it using any text editor. You can also download the plugin which I have made from http://wordpress.org/extend/plugins/map-categories-to-pages/.

function add_category_box_on_page(){
//add meta box
add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'low');
}

add_action('admin_menu', 'add_category_box_on_page');

It will show the category selection box on the right side on “Edit Page” page.

Template changes

You will also have to edit the page template to use categories. You can use standard category code on your page template. Take a look at http://codex.wordpress.org/Function_Reference/get_the_category and http://codex.wordpress.org/Template_Tags/the_category.

Here is a small piece of code to build a page of posts. This example uses the styles from WordPress default theme.

<?php
global $post;
$categories = get_the_category($post->ID);
$showposts = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => $categories,
'showposts' => $showposts,
'caller_get_posts' => $do_not_show_stickies
);
$my_query = new WP_Query($args);

?>

<?php if( $my_query->have_posts() ) : ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
//necessary to show the tags
global $wp_query;
$wp_query->in_the_loop = true;
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>

<p class="postmetadata"><?php the_tags('Tags: ', ', ', '
'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>

<?php endwhile; ?>

<?php else : ?>

<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>

<?php endif; ?>

Displaying pages on Category pages

When WordPress fetches the posts from database, the default post type is ‘post’. So even when you have mapped a page to a category, it won’t display the page on that category page. The post type needs to be changed explicitly on the category page. To change the post type, add following code in function.php.

//call a function just before the query runs to fetch posts
add_action('pre_get_posts','change_post_type');

function change_post_type($var) {
if(is_category()) {
$var->query_vars['post_type'] = 'any';
//it will change the value to 'any' from the default value of ;post';
//can be any, attachment,  page, post, or revision.
//'any' retrieves any type except revisions.
}
}

Map Categories to Pages

Latest Version: 1.1

Map Categories to Pages displays the category box on the “Edit Page” page. By default, here
Every page in wordpress is mapped to the default category but this plugin gives the option to select categories for a pages. Read Plugin: Assign Categories to Pages – version 1.1 and Assign Categories to Pages in WordPress for more information.

Download from WordPress
Updates in version 1.2:
Compatible with WordPress 3

Why it wasn’t working with WP3

WordPress had introduced Post Types in WP2.9. The default post types are post, buy page, visit this attachment, revision, and nav-menu-item. WordPress had no restriction on applying taxonomies (category and tag) on any post type but had given the option to select for “post” post type only.

In WP3, the post type needs to be registered with taxonomy to be able to attached to it. The “page” post type is not registered with Category taxonomy so the plugin wasn’t able to save categories for pages in database.

What have been changed to make it compatible with WP3

I have used register_taxonomy_for_object_type function to enable “page” post type for Categories.

Be Sociable, Share!

{ 20 comments… read them below or add one }

Jie September 3, 2010 at 9:08 am

hi, Please help me, I new to wordpress… I’ve downloaded and activated your plugin but some how the option for “auto-insertion of Posts on the Pages which belong to the same category” feature does not seemed to be working even after I’ve checked the option….

Reply

Amit Verma September 3, 2010 at 2:12 pm

Have you mapped the page to the category which you are checking?

Reply

Toscano September 15, 2010 at 7:57 pm

Hi, I think this plugin is great, but i don’t know how to show pages and post of chechek in the same category, I have this (before I’ve activated the “Show the Pages on Category pages” preference), but this only shows posts, not pages on the same category:

have_posts()) : $featured_query->the_post();
$do_not_duplicate[] = $post->ID ?>

<div id="post-” >
<a href="” rel=”bookmark” title=”Permanent Link to “>

Reply

Amit Verma September 16, 2010 at 9:10 pm

change post type before running the query.

$query->set( ‘post_type’, array( ‘post’, ‘page’ ) );

Reply

Toscano September 16, 2010 at 10:43 pm

that’s works, thanks a lot!!

Reply

NikC September 20, 2010 at 9:42 pm

Hi

I Installed the pluging and did some testing with it, it worked as the page I wanted was created, but I have since removed the plugin but the page for the categories that was auto generated is still there, any idea who to get rid of this page as it just gives me a 404 ?

Many Thanks

Nik

Reply

Amit Verma September 21, 2010 at 10:51 pm

This plugin doesn’t create any page. If you are opening a category page, you can check for permanent link structure.

Reply

DG September 22, 2010 at 1:03 am

Hi Amit,
I’m working on my personal site: diegograglia.net/site/
I downloaded your plugin because I’m using pages on the Nav Bar, but I can’t get some of those pages to show on the featured slideshow. The slideshow works by assigning a category to it, that’s why I thought your plugin would help.
I understand I have to edit the page.php but I don’t really see what it is that I have to do. Any help would be much appreciated.
Thanks a lot!
DG

Reply

Amit Verma September 23, 2010 at 9:24 pm

what are these slideshows? Are these posts or are you using custom post types?

Reply

DG September 24, 2010 at 3:19 am

Hi Amit, thanks for your reply. The slideshow is part of the theme and it’s configured through a Featured Category. The stories featured there are posts, and I couldn’t manage to include pages there.
In the end I found a solution for what I was looking for through a different method. I created posts assigning one each to the Categories on the nav bar, so they basically look like pages, and I could still feature them on the Slides.
Best,
DG

Reply

Cesar November 27, 2010 at 2:32 am

Dear Amit:

This plugin is great. I was wondering if a categories Widget is available for this plugin.

I would like to see, in the sidebar of each page, just the subcategories of the category associated. Is that possible?

Thanks,

César

Reply

Amit Verma November 29, 2010 at 1:15 pm

It is very much possible. I will post the code in a day or two.

Reply

cesar December 7, 2010 at 4:54 am

Thank you very very much for this help Amit. I will try this tomorrow and keep you updated.

I think this plugin is very important. The only way to distribute contents over pages.

Kind Regards,

César

Reply

Amit Verma December 7, 2010 at 9:44 am

I am glad you find it useful.

Reply

Marlyse Comte December 15, 2010 at 1:03 am

Great plugin – saves me the gray hair I was just about going to get, trying to do it myself 🙂

How can I display the assigned categories on the pages => admin page?

I can chose the category from the popup and it filters just fine – but it would be nice if it gets listed just as in the posts => admin page. Obviously the intention was there to show them as the header contains the column name for it.

Thanks!

Reply

Marlyse Comte December 15, 2010 at 10:16 pm

Great plugin and saved me from getting gray hair as I was just about to try and write something the like!

Especially useful that it allows for filtering on the PAGES overview page. One problem though, it does not list the categories in the column. The header shows, but not the rows. The categories are obviously assigned as I can filter via popup.

Can you fix that?

Reply

Amit December 31, 2010 at 6:35 pm

Sorry for the late reply. Could you please put some more light on the problem with example links?

Reply

pat January 23, 2011 at 10:40 pm

I tried the following code that you posted above but it errors out on the following line:
$includeCat.=$category->cat_ID.’, ‘; because of the comma. Is there a fix for that?

[php]
<?php
global $post;

$categories=get_the_category($post->ID);
if(count($categories)>0){
$i=1;
foreach((get_the_category($post->ID)) as $category) {
if($i==count($categories))
$includeCat.=$category->cat_ID;
else
$includeCat.=$category->cat_ID.’, ‘;
$i+=1;
}
?>

<ul><?php wp_list_categories(‘title_li=&child_of=’.$includeCat.’&show_count=0′); ?></ul>
<?php } ?>
[/php]

Reply

Amit January 24, 2011 at 3:36 pm

It was because of faulty single quote. Try using code given below.

[php]
<?php
global $post;

$categories=get_the_category($post->ID);
if(count($categories)>0){
$i=1;
foreach((get_the_category($post->ID)) as $category) {
if($i==count($categories))
$includeCat.=$category->cat_ID;
else
$includeCat.=$category->cat_ID.’, ‘;
$i+=1;
}
?>

<ul><?php wp_list_categories(‘title_li=&child_of=’.$includeCat.’&show_count=0′); ?></ul>
<?php } ?>
[/php]

Reply

Sandor Kiss February 5, 2011 at 5:47 pm

I would like to show full posts on the pages with category. Now I can see only the clickable titles.
I know, there is some tweeking in the template files, and sure, You can give me the solution here, but would you be kind to instruct me, where to put the given lines?
I have no clue, I am not a php guy.
Thanks

Sandor

Reply

Leave a Comment

Previous post:

Next post: