Query Post Within Category and Offset
-
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.
-
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?
You got it Kaf. Correct. Thoughts?
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
100in( $cat_id != '100' )to the Main Feature category ID. If using category name in queries, change$category->term_idto$category->name(or$category->slugfor 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_idto the appropriate one.Finally, slip this in at the end of your query arguments:
&offset=$offsetGreat 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!
“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?
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.
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.
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.
Where in the template does “Main Feature” fall? Also, what are you using to generate the various queries?
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]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"); ?>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.
Difficult to troubleshoot this way, so if you could paste your template up here:
and reply with the url to it, I’ll take a look.
Thanks Kaf. Here you go:
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.
The topic ‘Query Post Within Category and Offset’ is closed to new replies.