Page name vs Post name (for custom loop pages?)
-
Thanks for patience and understanding, this is my first WP theme and I’m learning php as I do it.
I am making a theme and have a template page for a loop of posts and I want to use this template for 4 different categories. What I have so far looks like this:
<?php /* Template Name: Gallery of Single Listings */ get_header(); query_posts(); ?> <div id="page-wrap"> <div id="gallery-wrapper"> <div id="recent"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <a href="<?php the_permalink(); ?>"> <div class="featured"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?> <span class="caption"> <?php the_title(); ?></span> </div></a> <?php endwhile; endif; ?> </div> </div> <!--End of Gallery-Wrapper --> </div><!--END PAGE WRAP--> <?php get_footer(); ?>
This loop works and displays properly with my css, but it shows every post. I want to limit it to the respective category. I wasn’t sure where to start, so I tried making it work for only one category (by slug)
<div id="recent"> <?php $cat_posts = new WP_Query("category_name=business"); ?> <?php if ($cat_posts->have_posts() ) :?> <?php while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?> <a href="<?php the_permalink(); ?>"> <div class="featured"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?> <span class="caption"> <?php the_title(); ?></span> </div></a> <?php endwhile; endif; ?> </div>
one of my category slugs is “business” but this doesn’t display anything at all.
I only have 4, so if I can get this to work, I can 4 copies of this and change the slug (hard coded) but would that be less efficient for page speed compared to using php logic? If so, how would I get the slug name from the current page being displayed to use in the php?Thanks!!
- The topic ‘Page name vs Post name (for custom loop pages?)’ is closed to new replies.