• Resolved jclark5093

    (@jclark5093)


    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!!

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

    (@jclark5093)

    I did some searching through, and think that what I need to alter is the query, not the loop. I should be able to run a standard loop after using a query like this:

    if ( is_category( 'category-slug' ) ) : 
    
    	 query_posts( array( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) ); 
    
    endif;

    but is there a difference between ‘category-slug’ and ‘my-category-slug’? Let’s say a slug is “business” so should my code read:

    if ( is_category( 'business' ) ) : 
    
    	 query_posts( array( 'category_name' => 'business', 'posts_per_page' => -1 ) ); 
    
    endif;

    or would I need to define ‘category_name’ somewhere (or is that a system recognized name?)

    Thread Starter jclark5093

    (@jclark5093)

    I found a single line that works where ‘business-for-sale’ is the (default?) slug for the category.

    <?php query_posts( array( 'category_name' => 'business-for-sale', 'posts_per_page' => -1 ) ); ?>

    I’m not sure what the php if statement is for, is it important?

    Also, what can I put in that spot for the slug that would be a variable based on the current page (I need 4)?

    Thread Starter jclark5093

    (@jclark5093)

    I think this is in the wrong section.

    Moved thread to troubleshooting section (new thread is here), closing this thread.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page name vs Post name (for custom loop pages?)’ is closed to new replies.