Get Posts Based On Page Name (Category)
-
I have an old code that does this perfectly except it is not written for WP_Query. I’ve gone over the codex but every time I try, it seems to spit nothing back out at me. Could someone show me an example of how this is done with WP_Query? I’m very new but I learn fast once I’m shown.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="content-container"> <h1><?php the_title(); ?></h1> <?php the_content(); ?> <?php endwhile; else: endif; ?> <?php query_posts('category_name='.get_the_title().'&post_status=publish');?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h1><a>"><?php the_title(); ?></a> </h1> <p><?php the_content(); ?> <?php endwhile; else: endif; ?> </div>In the above code, it retrieves all the posts in a Category that matches the name of the Title of the page, even if the page title is multiple words. I’m looking for this functionality. Thanks!
I think in theory I would use:
$query = new WP_Query( array( 'category_name' => 'slugname' ) );Where the slugname is the sanitized title name of the page, perhaps using get_the_title?
The topic ‘Get Posts Based On Page Name (Category)’ is closed to new replies.