add meta box to select category to select on custom blog page in wordpress

axessJosh
axessJosh used Ask the Experts™
on
I have created a custom template page in wordpress to display by category type.  I currently create a page for each category i'd like to display in a custom loop.  I'd like to make this a bit more functional for the user allowing them to select which categories display on the page through a custom meta box.

I've been doing some research about adding custom meta boxes to wordpress but want to make sure I'm on the right track.  Is there an easier way to get this accomplished?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ovid BurkeConsultant Instructor

Commented:
It might be useful to see your custom template to understand what is does and how. How do you currently select what category is displayed on a 'page'?

Author

Commented:
I've used the following code below.

 <?php

		global $post;

		$args = array( 'numberposts' => 35, 'category_name' => 'physicians');

		$myposts = get_posts( $args );

		foreach( $myposts as $post ) :	setup_postdata($post); ?>
        	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

        <?php endforeach; ?>

Open in new window


What i'd like to do is be able to add a meta box in the admin edit screen allowing the user to choose which categories to display on these pages.  This would enable the user to define specific posts in a category to display on this page, and disregard the unselected categories.

we used a custom template by elegant themes at one point that allowed for this but I can't seem to figure how to add this functionality native to wordpress.
Consultant Instructor
Commented:
Add this to functions.php
add_action('admin_init', 'my_page_categories');
function my_page_categories()
{
	register_taxonomy_for_object_type('category', 'page');
	add_post_type_support('page', 'category');
}

Open in new window


This part goes into your custom template:
<?php
// Get the category name of the current page
$category = get_the_category();
$cat_name = $category[0]->cat_name;

// Prepare query arguments
$args = array(
	'numberposts' => 35,
	'category_name' => $cat_name
);

$myposts = get_posts( $args );
foreach( $myposts as $post ) :	setup_postdata( $post ); ?>
	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php endforeach; ?> 

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial