• Resolved venkmanuk

    (@venkmanuk)


    Hi there,

    I understand that pages aren’t posts, so don’t usually have a category. But that fact aside i need to show pages in a particular category.

    i added this to functions.php:

    // Adds categories to pages
    add_action('admin_init', 'reg_tax');
    function reg_tax() {
    register_taxonomy_for_object_type('category', 'page');
    add_post_type_support('page', 'category');
    }

    i thought that i could show pages in that particular category. but i have no idea how…

    the pages don’t show up in the archive page for that category, and get_pages doesn’t have the option to filter by category.

    nightmare! if you can help please do! thanks ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter venkmanuk

    (@venkmanuk)

    this is working to list pages with specific category names:

    <?php
    
       $pages = get_pages();
    
    foreach ($pages as $page) {
    
        $category = get_the_category($page->ID);
    	$chek = $category[0]->cat_name;
    	//echo $chek;
    
    	if ( $chek == "My Category" ) { echo "yes, the channel = My Category and post title = "; echo $page->post_title; echo "<br />"; }
    
    }
        ?>

    venkmanuk,

    This is surely a very common case but unfortunately we have to mold things little bit on our own to get this thing working. AS you know categories are meant for posts but we can use categories with pages as you had done by above code. Now the only problem left is how to recall the pages or list of pages within that category.

    What i had done to resolve this problem is : created a new template page where i had done some custom code to display the list of pages with in specific category. Code which i used is below

    <?php
    $category_id = 7;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $new_query = new WP_Query();
    $new_query->query( 'showposts=1&cat='.$category_id.'&post_type=page&paged='.$paged );
    
    ?>
    <?php if ($new_query->have_posts()) : while ($new_query->have_posts()) : $new_query->the_post(); ?>
       <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    ----------YOUR CUSTOM CODE HERE--------				
    
    <?php endwhile; ?>

    Notice that i had used “post_type=page” with WP_QUERY arguments, by default posts are returned when fetching records from category thats why i had added post_type=page to fetch pages.

    I know there can be other ways to do this. I am open to other suggestions but till other suggestions shows up this will help you.

    ??

    Thread Starter venkmanuk

    (@venkmanuk)

    interesting, thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show Pages in a Category’ is closed to new replies.