Custom Loop, Match Category with Page

If this is going in a page.php (or similar) template, then using query_posts() is a bad idea and could have some pretty bad consequences. I also prefer WP_Query over get_posts() as it easily allows you to use template tags and it explicitly exists for running secondary loops on a page.

<?php
global $post;
$my_query_args = array(
    'posts_per_page' => 5, // change this to any number or '0' for all
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $post->post_name // this gets the page slug
        )
    )
);
// a new instance of the WP_query class   
$my_query = new WP_Query( $my_query_args );

if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>

    <li><a href="https://wordpress.stackexchange.com/questions/31064/<?php the_permalink() ?>"><?php the_title() ?></a></li>

<?php endwhile; endif; wp_reset_postdata(); ?>

Leave a Comment