suthanwebs
suthanwebs

Reputation: 21

List all pages from a category in Wordpress

I'm in the middle of a small crisis here. I need your help! I've done my research for hours but this is my last resort. Cutting to the story, I'm trying to list all the 'pages' from an active category in Wordpress.

Here are the steps I've done so far. I was able to enable categories for my pages by adding the following code to my functions.php file:

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

Next, I needed to make it so that the website has categories & sub-categories. The sub-categories will only show on the category page of the parent. I've added the following to my category.php file to get the list of all my sub-categories.

<?php

$categories = get_categories( array(
    'orderby' => 'name',
    'parent'  => $cat,
    'hide_empty' => FALSE
) );
if ($categories)
{
    foreach ( $categories as $category ) 
    {?>         
        <li><a href="<?php echo $category->slug ?>"><?php echo $category->name ?></a></li>                         
<?php 
    }
} ?>

Now, when you visit a specific sub-category (considering nothing will be assigned to the main one), I need to be able to list all the pages that is assigned that sub-category.

What can I do next? I have this in my index.php but this just lists all the pages. Regardless if you're in a specific category or not.

<ul>
    <?php
    global $post;

    $myposts = get_posts( array(
        'post_type' => 'page',
        'posts_per_page' => 5
    ) );

    if ( $myposts ) {
        foreach ( $myposts as $post ) : 
            setup_postdata( $post ); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php the_content(); ?>
            </li>
        <?php
        endforeach;
        wp_reset_postdata();
    }
    ?>
</ul>

Hope you can help! Thanks. :) Credits to replies/code from others over here to help me solve some of the earlier issues.

Upvotes: 2

Views: 4216

Answers (2)

Louis
Louis

Reputation: 116

On the page you wish to filter posts, you can try using $cat = get_the_category($post->id); to return array. Then use a foreach or just get the first item:

$cat_id = $cat[0]->cat_ID;

Then update the WP query to show all posts using that category.

By ID:

$query = new WP_Query( array( 
    'post_type' => 'page',
    'cat' => $cat_id
) );

By name:

$cat_name = get_cat_name($cat_id);

$query = new WP_Query( array( 'category_name' => $cat_name ) );

Source: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Upvotes: 1

Obelisk
Obelisk

Reputation: 39

Have you tried something like this, for your index.php code:

$myposts = get_posts( array(
    'post_type' => 'page',
    'taxonomy' => 'category'
) );

Also, I would try and avoid "get_categories", it's best to try and use the "get_terms" function whenever you can.

Upvotes: 0

Related Questions