Forums

Query Post Within Category and Offset (22 posts)

  1. endortrails
    Member
    Posted 2 years ago #

    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.

    www.thadallender.com

  2. Kafkaesqui
    Moderator
    Posted 2 years ago #

    Let me see if I have this straight, before I see what you can do about it...

    On the front page you display the latest post from several different categories, each under its own 'heading' (i.e. category) in separate queries.

    You also have a Main Feature category, and -- also -- display the latest post from that on the front page in a separate query. But this Main Feature post may also be in one of the other categories displayed on the front page.

    So what you want is to assure that only the appropriate category query is offset (by 1) when the Main Feature post also falls under that category.

    Correct?

  3. endortrails
    Member
    Posted 2 years ago #

    You got it Kaf. Correct. Thoughts?

  4. Kafkaesqui
    Moderator
    Posted 1 year ago #

    I have a couple ideas on what could be done, but possibly the easiest solution is to use a custom variable assigned the category ID (or IDs if the Main Feature post can be set to two or more other front page categories) from the Main Feature post's data. Then you only need test against each category query to decide if the offset must be set.

    Here's an example where only one additional category exists for the Main Feature post. First, somewhere in the Main Feature post loop:

    <?php
    $categories = wp_get_object_terms($post->ID, 'category');
    foreach( $categories as $category ) {
    	if ( $category->term_id != '100' ) {
    		$cat_offset = $category->term_id;
    	}
    }
    ?>

    Modify 100 in ( $cat_id != '100' ) to the Main Feature category ID. If using category name in queries, change $category->term_id to $category->name (or $category->slug for the 'sanitized' version) and set the test string accordingly.

    Next, we need the code appearing *before* the other queries:

    <?php
    $offset = 0;
    $categories = wp_get_object_terms($post->ID, 'category');
    foreach( $categories as $category ) {
    	if ( $category->term_id == $cat_offset ) {
    		$offset = 1;
    	}
    }
    ?>

    Again, if necessary modify the associative array key ('term_id') on $category->term_id to the appropriate one.

    Finally, slip this in at the end of your query arguments:

    &offset=$offset

  5. endortrails
    Member
    Posted 1 year ago #

    Great Kaf! I'm almost there, but I'm having trouble with the php syntax.

    I would like to avoid duplicating Category 1, so based on your code above, would I put the following code above each query:

    <?php
    $offset = 0;
    $categories = wp_get_object_terms($post->ID, 'category');
    foreach( $categories as $category ) {
    	if ( $category->term_id =('1")= $cat_offset ) {
    		$offset = 1;
    	}
    }
    ?>

    My troubles seem to be happening on this line as I'm not sure about the proper syntax:

    if ( $category->term_id =('1")= $cat_offset ) {

    Thanks again!

  6. Kafkaesqui
    Moderator
    Posted 1 year ago #

    "I would like to avoid duplicating Category 1"

    Um, not sure I understand. Why are you trying to avoid duplicating it? And where is it *not* supposed to be duplicated?

  7. endortrails
    Member
    Posted 1 year ago #

    Perhaps I just confused you. I'm simply having troubling following your directions on where to insert the category number. My category number is 1. Being new to php, I'm causing fatal errors when/where I attempt to insert category 1 in the php.

    Again, if necessary modify the associative array key ('term_id') on $category->term_id to the appropriate one.

  8. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Ah! No, the category number (assuming this is your Main Feature category) would go in the first bit of code I have above (the code statement meant for the Main Feature post loop). This line specifically:

    if ( $category->term_id != '100' ) {

    Change 100 to 1. There should be no need to do anything to the code statement going before the other queries.

  9. endortrails
    Member
    Posted 1 year ago #

    Huh. Interesting. Well, then I guess it should be working because that is exactly the code that I have running live on my site. Doesn't seem like it's working.

  10. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Where in the template does "Main Feature" fall? Also, what are you using to generate the various queries?

  11. endortrails
    Member
    Posted 1 year ago #

    This is the Feature Category post code:

    <!--BEGIN FEATURE-->
    
        <h3>
    
        <?php wp_list_categories('include=1&title_li=&style=none'); ?></h3>
    
        <hr>
    
        <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
    
        <?php
        $categories = wp_get_object_terms($post->ID, 'category');
        foreach( $categories as $category ) {
        if ( $category->term_id != '1' ) {
        $cat_offset = $category->term_id;
        }
        }
        ?>
    
    [Edited by moderator]

    I have five queries similar to this one on the page too:

    <h6><?php wp_list_categories('include=4&title_li=&style=none'); ?></h6>
    
    <hr>
    
    <?php
    $offset = 0;
    $categories = wp_get_object_terms($post->ID, 'category');
    foreach( $categories as $category ) {
    	if ( $category->term_id == $cat_offset ) {
    		$offset = 1;
    	}
    }
    ?>
    
    		  <?php query_posts('showposts=1&cat=4&offset=$offset'); ?>
    
        <?php while (have_posts()) : the_post(); ?>
    
    [Edited by moderator]
  12. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Edited your last one as there was a lot of superfluous code not needed for dealing with the problem (also made it hard to read).

    Anyway, here may be the problem:

    <?php query_posts('showposts=1&cat=4&offset=$offset'); ?>

    In PHP anything within single quotes is interpreted literally (so $offset is passed as the text string '$offset'). Change it to double quotes so PHP will read in and pass the variable you set:

    <?php query_posts("showposts=1&cat=4&offset=$offset"); ?>

  13. endortrails
    Member
    Posted 1 year ago #

    Hmmm. That doesn't do it either. To reiterate my dilemma to make sure we're still on the same page:

    I have five post queries that display the last post in each category. I have one "Feature" that displays the latest post in all categories. So that I don't have redundant posts on the page, the queries need to check to see which category needs to be offset.

    So far, nothing seems to work.

  14. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Difficult to troubleshoot this way, so if you could paste your template up here:

    http://wordpress.pastebin.ca/

    and reply with the url to it, I'll take a look.

  15. endortrails
    Member
    Posted 1 year ago #

    Thanks Kaf. Here you go:

    http://wordpress.pastebin.ca/772131

  16. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Ok, looking over the template I need to go back to your original topic post. You stated:

    I have a "Main Feature" category where I post my latest entry

    I interpreted this to mean the post in this section of your front page was actually added to a post category called "Main Feature," along with the category it normally fell under. However, it seems the Main Feature post is intended as the latest post on the blog as a whole.

    I also got deeper in with your issue on duplicating category 1, which I assumed was the "Main Feature" category #.

    Since the code I gave you assumes there is a Main Feature category, I'll make some changes to it dealing with that. Let me look things over later today when I have time, then I'll paste up a mod of your template.

  17. endortrails
    Member
    Posted 1 year ago #

    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

  18. Kafkaesqui
    Moderator
    Posted 1 year ago #

    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. :)

  19. Kafkaesqui
    Moderator
    Posted 1 year ago #

    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".

  20. endortrails
    Member
    Posted 1 year ago #

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

  21. Kafkaesqui
    Moderator
    Posted 1 year ago #

    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().

  22. kasiblog
    Member
    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic