• I’m building a magazine theme and I’m relying heavily on category post queries to display the latest category specific posts on the main index. I have a “Main Feature” category where I post my latest entry, but that entry also shows up in the category specific sections. Using the offset=1 variable offsets all categories by 1, which is undesirable.

    Confused? Try looking at the page to better understand.

    http://www.thadallender.com

Viewing 6 replies - 16 through 21 (of 21 total)
  • Thread Starter Thad Allender

    (@endortrails)

    Actually Kaf, I can create a FEATURE category and always post to the category (as well as the others) for all posts that I want to have show up in the FEATURE section. That would really be easy to do.

    Thad

    It’s up to you Thad. But note in either case there are a few additions I think your template needs to make it all work as expected.

    I’ll paste up a version of your template covering both options; since I already have most of the code in place for the expected Main Feature category, that won’t require any real work to complete. :)

    Ok, had some time tonight to piece these together.

    Displays Feature as latest post in *all* categories:
    http://wordpress.pastebin.ca/773036

    Displays Feature as latest post in Feature category:
    http://wordpress.pastebin.ca/773040

    Notes:

    1. The new code is much abbreviated in the first template, as I took advantage of each post being in only one category. I also simplified the offset test before each secondary sections’ query in both templates (just because I could!).

    2. In the second template look for this line near the beginning:

    $feature_cat = 1;

    You’ll want to edit the numeric value here to the category ID number for your Feature category.

    3. Notice at the start of the Feature section in both templates this part:

    <!–<h3>Feature</h3>–>

    It’s commented out so will not display, but certainly in the case of the first template you may want to switch to this for your header if you don’t want the category from the latest post listed instead of, you know, “Feature“.

    Thread Starter Thad Allender

    (@endortrails)

    Wow. Thanks ton. Not one, but two options. Very cool. I just sent you some loot. Enjoy and thanks Kaf.

    First, thanks. ‘Donations’ can be an incentive, but certainly not the reason I hang out here.

    If you have any further questions about the template, shoot away. I did change things a bit from above, and that loopy loop at the top (for the ‘Feature’ post) may seem unexpected. But, it does keep your Feature post from getting banged by all those query_posts().

    I have a similar problem for another theme (Branford Magazin) and I am trying to adapt your solution to that theme. The Theme uses one specific category for the Feature.

    So, it seems this is first main item:

    $main_query = new WP_Query("showposts=1");
    $category = wp_get_object_terms($main_query->post->ID, 'category');
    $cat_offset = $category[0]->term_id;
    echo $category[0]->term_id;

    – Line 1 saves the latest post from all categories into the Variable main_query
    – Line 2 gets the category from the latest post
    – Line 3 saves the ID from that category into the variable cat_offset

    What does Line 4 do?

    Okay, and this is the second important bit for the place where the remaining articles are displaced, in this case for category 12

    <?php $offset = 0;
    if ( 12 == $cat_offset ) {$offset = 1;}
    query_posts("showposts=1&cat=12&offset=$offset");
    ?>

    Line 1 puts the variable offset into False.
    Line 2 say that if the Category Number (12) is equal to the Category Number of the Feature-Article, then Offset is True
    Line 3 says to post the remaining article(s).

    If I want to use for another category number, I just have to replace 12 with 44 or 767 or whatever number, right?

    If I want to show more than of the remaining posts, than I change the number behind showposts, right?

    Then the normal loop follows for this category.

    The third important bit is this

    <?php if ($main_query->have_posts()) : ?>
    <?php while ($main_query->have_posts()) : $main_query->the_post(); ?>

    I don’t understand this if-clause. Why is it needed? It seems that already above the newest posts is in main_query? Why would you have to check whether there is a post (have_posts)? Is it so that the_post and the_excerpt and the_content give back the newest article?

Viewing 6 replies - 16 through 21 (of 21 total)

The topic ‘Query Post Within Category and Offset’ is closed to new replies.