0

I am attempting to use categories for pages (not posts) as a way of filtering child pages.

I am working towards having a 'Work' page that lists all its children, and each of these children will also have categories applied to them. Another sub menu will allow you to choose a category and only those child pages will be listed.

So far:

functions.php - Show categories meta box for pages

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

Set up a category menu, which is rendered on my 'work' page

<?php wp_nav_menu(array('theme_location' => 'category-menu')); ?>

category.php - Get the ID of the category we're looking at

<?php get_header(); ?>

<?php $cat_id = get_cat_id( single_cat_title("",false) ); ?>

<p>
<?php echo $cat_id; ?>
</p>

<?php get_footer(); ?>

This is where I am stuck: How can I loop though my work child pages that have the category ID that I am getting in my category.php template?

I understand that categories aren't really designed for pages so I am open to suggestions as to how to set and filter my pages by some kind of category.

I am already using Advanced Custom Fields so there is scope to use that; I would rather not use any other plugins.

1

As you have stated, pages aren't meant to have categories. I would rather look into custom post types and taxonomies, but the implementation is still up to you as I don't know your structures

I have never tried this before, neither do I know if this will actually work, but you can try the pre_get_posts action to amend the main query to display pages on your category.php template

add_action( 'pre_get_posts', function ( $query ) {
    if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page' ) );
    }
});
  • Any luck on this issue. Would like to know if this is resolved, and if my solution did not work, please post your solution and accept your own answer :-) Thank you – Pieter Goosen Nov 29 '14 at 19:39
  • Thanks for this - my inexperience with WordPress really shone through here! I ended up creating a custom post type and a custom taxonomy to store my categories. – Alan Shortis Dec 5 '14 at 10:24
  • No problem, glad something worked out for you. Enjoy :-) – Pieter Goosen Dec 5 '14 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.