The format of this post is whacked. You should start a new thread and try to figure out what caused the problem.
Please explain what you mean by ‘2 posts in each element’. What is an element?
If I am reading the code correctly, what you are getting is:
List of Posts in category category_name_1
Post 1 in category_name_1
Post 2 in category_name_1
List of Posts in category category_name_2
Post 1 in category_name_2
…
What do you want to get?
EDIT: I see that you want two posts in each <li> element. An un-backticked <li> is messing up the format.
Thread Starter
Nith
(@nith)
Yeah, I want to be able to separate the posts returned into different <li> elements.
How is this achievable?
Thanks again.
Please post a few more lines (maybe 10 or 15) after if ( $my_query->have_posts() ) { so I can tell more what you are doing for each post.
Thread Starter
Nith
(@nith)
Ok, below is a bit more code.
Basically, I have a ‘slider’ system which uses jQuery. And each <li> item is each ‘slide’ so to speak.
If you go to nickythorne.com you’ll see what I mean by ‘slider’. I want to list a number of posts on each slide. This is where the scope of my knowledge with WordPress ends 🙂
See the commented bit.
<div id="slider">
<ul>
<?php
$taxonomy = 'category';
$param_type = 'category__in';
$term_args=array(
'include'=> '3,4,11',
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<div class="post-details">
<!-- It's here where i want the code which will display 5 posts
So as it's a while loop, the <li> element will contain 5 posts in each slide -->
</div>
</li>
<?php
endwhile;
}
}
}
wp_reset_query();?>
</ul>
</div>
I am not quite sure this is what you want, but it is worth a try. Change the ‘posts_per_page’ to 5 if the comment in the while loop is correct, and move the <li> and </li> tags outside the while loop:
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name; ?>
<li>
<?phpwhile ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post-details">
<!-- It's here where i want the code which will display 5 posts
So as it's a while loop, the <li> element will contain 5 posts in each slide -->
</div>
<?php endwhile; ?>
</li>
Thread Starter
Nith
(@nith)
Not quite.
Lets say I want to display 15 posts altogether, then the ‘posts_per_page’ would be 15.
Below is the functionality I’m looking for
This would mean that i’d have 3 slides or <li> elements with 5 posts in each (making up the 15 altogether).
I need the existing code to be there as I only want to select the posts from certain categories.
I couldn’t post the complete code – the moderators will probably delete it if it gets too long. What I suggested will display 5 posts in each li tag for each of the categories 3,4, and 11.
Don’t eliminate the code you already have, just modify it by moving the li tags outside the while loop, and set the posts_per_page to the number of posts to display in each category.
Thread Starter
Nith
(@nith)
Oh right.
I have set the ‘posts_per_page‘ to 2 to test this and after trying what you said, only the first two posts are showing in each <li> element for some reason. The other posts in the category are not getting displayed (there are other posts in the category). http://www.nickythorne.com/pages/blog/
I’ve pastebin’d the code: http://pastebin.com/j38kzsq1
Thanks for your help.
Thread Starter
Nith
(@nith)
Yeah, as I first thought, because I’ve moved the <li> tags outside the while loop, they are not getting duplicated (there is only ever one).
This is where I was running into difficulty in the beginning. The <li> needs to be inside the while loop in order to get replicated, but the issue is displaying the ‘post_per_page‘ in each <li>.
Something just isn’t adding up. In the code I see a line echo 'List of Posts in '.$taxonomy .' '.$term->name; ?>, but I don’t see that text on the page. What am I missing.
As I read the code, you should get the query executed 3 times, once for each category 3,4,11. If you have posts_per_page set to 2, and there are 2 or more posts in each category, and the li tags are outside the while loop, you should see this on the page:
List of Posts in category name1
<li>
post 1 in category name1
post 2 in category name1
</li>
List of Posts in category name2
<li>
post 1 in category name2
post 2 in category name2
</li>
List of Posts in category name3
<li>
post 1 in category name3
post 2 in category name3
</li>
Is that what you want?
Thread Starter
Nith
(@nith)
Oops. Yeah, I forgot to update the original code.
I’ve removed the echo 'List of Posts in '.$taxonomy .' '.$term->name; ?> line and also change line 'include'=> '3,4,11', to 'include'=> '3', to only return the posts in the ‘blog’ category.
So, I’m aiming in having the output be:
<li>
post 1 in the category 'blog'
post 2 in the category 'blog'
</li>
<li>
post 3 in the category 'blog'
post 4 in the category 'blog'
</li>
<li>
post 5 in the category 'blog'
post 6 in the category 'blog'
</li>
But it’s not happening. I’m guessing it’s because the ‘posts_per_page’ is set to 2, but removing that will just display all the posts in one <li> element right? And I want to ‘split’ the posts and have two of the posts in each element (like the output above).
Sorry for the confusion, my bad.
OK, change posts_per_page to the number you want for each category and try this:
if( $my_query->have_posts() ) {
$count = 0; ?>
<li>
<?phpwhile ($my_query->have_posts()) : $my_query->the_post(); ?>
if ( ++$count > 1 && ($count - 1) % 2 == 0 ) echo '</li><li>' ; ?>
<div class="post-details">
<!-- It's here where i want the code which will display 5 posts
So as it's a while loop, the <li> element will contain 5 posts in each slide -->
</div>
<?php endwhile; ?>
</li>
}
Thread Starter
Nith
(@nith)
Works like a charm 🙂
Thanks so much for your help/patience, appreciate it.
All the best.