• 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. I did some searching through the codex, 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;

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

    latest:

    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)?

    Thanks for patience and understanding, this is my first WP theme and I’m learning php as I do it. [I posted originally in the wrong place, so I’m re-posting part of my thread with myself here]

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

    (@jclark5093)

    I think I realized that I’m trying to make my pages behave like a category.php page. Is there a way to gather a category type for the posts I want to display inside a custom page.php?

    Thread Starter jclark5093

    (@jclark5093)

    Really not elegant, but I found a solution that works for me for now. It’s hard coded for my 4 categories instead of dynamically generating them, but it’s functional.

    If there’s a better solution, please post. I will leave this thread open for a few days.

    <?php // assign the variable as current category based on the current page
    $cat_slug = '';
    global $post;
    $pagename = $post->post_name;
    
    if ($pagename =='business') { $cat_slug = 'business-for-sale'; }
    elseif ($pagename =='commercial') { $cat_slug = 'commercial-listing'; }
    elseif ($pagename =='residential') { $cat_slug = 'residential-listing'; }
    elseif ($pagename =='hotels-motels') { $cat_slug = 'hotel-motel-listing'; }
    
    query_posts( array( 'category_name' => $cat_slug, 'posts_per_page' => -1 ) );  ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Page of posts by $slug’ is closed to new replies.