• Hi all

    Currently I have a category page listing all posts within that particular category.

    How would I force posts within that category, but that have another particular category assigned to them to display first without hard-coding the original category (and pref with additional markup around it so I can style it differently)?

    So in English it would be:

    Display posts within category “Ad” and “Premium” order by category and post_date.

    But without hardcoding the category “Ad” so that this works within other categories.

Viewing 8 replies - 1 through 8 (of 8 total)
  • First, create a Page and note the page ID (look in Dashboard > Manage > Pages).

    Then copy your theme’s “category.php” and save it as “page-[page ID].php.” Example: If the ID of your newly created page is 134, you should save your new file as “page-134.php”.

    In your new template file, locate The Loop. Also make a note of the Category ID of both Ad and Premium categories (Look in Dashboard > Manage > Categories).

    Just before The Loop, insert <?php query_posts('cat=[the Ad ID]'); ?> Example: If the Ad category ID is 1, it should be <?php query_posts('cat=1'); ?>

    After The Loop, insert <?php query_posts('cat=[the Premium ID]'); ?> and then paste a copy of The Loop.

    A visit to your Page should now display all your Ad posts followed by all your Premium posts.

    Edit: typo

    Just realized that this setup may not include all your posts. Could perhaps be fixed by adding the parameter showposts=[an extreme number] to the query_posts(). Example: query_posts(cat=134&showposts=9999)

    Thread Starter noobie

    (@noobie)

    Thanks for this dnusim – I’ll give it a go and report back!

    Thread Starter noobie

    (@noobie)

    Ah just noticed that you’ve got both categories hard-coded. Although I can hard-code the premium category I don’t want to hard-code the other one as I have multiple sections and likely to have more created by the person who will be using it.

    Thanks anyway though!

    I’m not sure if I understand you correctly. Do you want to first display posts within one category and then display posts within every other category?

    If you first query cat=1 and then display them, you can always query
    cat=-1 later to display every post that is not in category 1.

    Does that help?

    Thread Starter noobie

    (@noobie)

    I know, I’m not explaining it well!

    Ok so the site area I’m talking about is pretty much like a directory with lots of categories of company for instance – “Restaurants in London”. And on the Restaurants in London category page I’ve obviously got all posts with that category listed along with their content.

    The complication is that I have a further category “Premium Ad” (not a child category). I would like all category pages (e.g. “restaurants in London” or “Restaurants in Birmingham”) to not only list those restaurants within it’s category BUT to list first those restaurants that ALSO have the Premium Ad category so those will be at the top of the list.

    Obviously listing the posts for the current category is no problem. What I can’t do is come up with a function above that which would list the posts that ALSO have the category “premium-ad”. The closest I can get is to list all posts with the category Premium Ad but I can’t work out how to make it list all posts in current category & category=”premium-ad” without hard-coding the current category (which I can’t do because the template is for use for all categories).

    <?php if (have_posts()) : ?>
    
     <?php
       $premiumposts = get_posts('category=63');
        foreach($premiumposts as $post) :
        setup_postdata($post);
     ?>
    
     <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
        <?php endforeach; ?>
    
        <?php while (have_posts()) : the_post(); ?>
    
        <div class="post" id="post-<?php the_ID(); ?>">
          <h2><?php the_title(); ?></h2>
    
           <div class="entry">
           <?php the_content('Read the rest of this entry &raquo;'); ?>
           </div> <!-- end entry -->

    Allright, I see. Let’s try this approach:

    1. Query all cat=[genericCategoryID] posts
    2. Start The Loop with a test if the post is a premium post using get_the_category()
    3. Display these posts and end The Loop
    4. Now query cat=[genericCategoriID],-63 and do another Loop

    Any luck?

    Thread Starter noobie

    (@noobie)

    Thanks for that – I’ll give it a go!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Force posts with certain cat to be first’ is closed to new replies.