there’s a sidebar with a different design elements where it needs to have 8 posts of the same category,
I assume this is a secondary loop. Try using get_posts for it.
http://codex.wordpress.org/Template_Tags/get_posts
You can get the current category with this:
$current_cat = get_query_var('cat'); // category ID
$current_cat_slug = get_query_var('category_name'); // category slug (only on category page)
Thanks a lot, reasearching a little bit seems like i can use something like this on that sidebar:
<?php global $post; // required
$args = array('numberposts'=>3, 'category'=>-6,-9, 'order'=>'ASC');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
...
endforeach;
?>
But the thing is, I need that page to place only posts on selected category, if people click on category B the sidebar will display 8 posts on category B, but people can click on any category they want and if the admin creates a new category this automatically needs to be available to be displayed, in the example above categories are pre defined, how to automatically display any given category?
Not sure why you want to exclude categories but try this:
$current_cat = get_query_var('cat'); // category ID
$args = array('posts_per_page' => 3, 'cat' => $current_cat, 'order' => 'ASC');
$custom_posts = get_posts($args);
// the rest
I don’t think you need global $post;
No no, I don’t want to exclude categories, that was just an example I found around, but the question is still the same, in your example above you are only calling category 3, what I need is to call any given category, no matter which one, just catch the category and post 8 posts different than the 3 featured posts in the main area.
you are only calling category 3,
I don’t think so. Does it get the posts for the current category archive page (but not different from the main loop)?
Sorry, you are right, didn’t see that one properly…..
Ok, just to be sure, I’ll place this first:
<?php global $post; // required
$current_cat = get_query_var('cat'); // category ID
$args = array('posts_per_page' => 3, 'cat' => $current_cat, 'order' => 'ASC');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
endforeach;
?>
And then, my regular loop?
Well, that worked actually, it works like a charm to be honest, but just one detail, it is printing the same 3 posts I have on my main area, in my main area where I have the 3 posts you helped me to with the function code I want to have some kind of featured posts on that category and then at the side bar the next 3 posts, but they needs to be different than the first 3 posts in the main area, is there a filter or something I can play with?
Also, the sidebar only shows 3 posts and I need 8, but I think it is because that function code you sent me?
This is my code on the sidebar:
<div id="sidebar-categories">
<?php global $post; // required
$current_cat = get_query_var('cat'); // category ID
$args = array('posts_per_page' => 8, 'cat' => $current_cat, 'order' => 'ASC');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
endforeach;
?>
<div id="title-sidebar-categories"><?php printf( __( '%s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="date-sidebar-categories"><?php the_time('F jS, Y') ?></div>
<div id="title-sidebar-categories"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></div>
<div id="contents-sidebar-categories"><?php echo substr(get_the_excerpt(), 0,66); ?></div>
<?php endwhile;?>
<?php else : ?>
<h1>Nothing Found</h1>
<?php endif; wp_reset_query(); ?>
</div>
But other than that works really cool, thanks so much keesiemeijer!!
Try it with this in your sidebar:
$args = array('posts_per_page' => 8, 'cat' => $current_cat, 'order' => 'ASC', 'offset' => 3);
$custom_posts = get_posts($args);
Do you need pagination on your main loop (not your sidebar)?
Hi there keesiemeijer, thanks,
Well tried what you suggested and no, didn’t worked, still displaying only 3 posts and repeating the 3 posts on the main area, and I don’t think I’m going to use pagination there because that’s a link below those 3 main posts that leads to the category archive.
Can you try it without:
global $post; // required
Can you post the full code of your category.php template and your sidebar code. see the Forum Rules on how the post code.
Sure, I took off the global thing but didn’t worked as well, here are the source codes:
Category: http://pastebin.com/ikvHrigj
Sidebar: http://pastebin.com/Ni0D4huT
Maybe the 3 posts is because the code in the functions.php:
function my_post_queries( $query ) {
// not an admin page and is the main query
if (!is_admin() && $query->is_main_query()){
if(is_category()){
$query->set('posts_per_page', 3);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
Thanks!!
You’re using a normal loop in your sidebar. Try it with the get_posts loop:
<?php
$current_cat = get_query_var( 'cat' ); // category ID
$args = array( 'posts_per_page' => 8, 'cat' => $current_cat, 'order' => 'ASC', 'offset' => 3 );
$custom_posts = get_posts( $args );
foreach ( $custom_posts as $post ) : setup_postdata( $post ); ?>
<div id="date-sidebar-categories"><?php the_time( 'F jS, Y' ) ?></div>
<div id="title-sidebar-categories"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></div>
<div id="contents-sidebar-categories"><?php echo substr( get_the_excerpt(), 0, 66 ); ?></div>
<?php endforeach;?>
Got to say, that worked wonderfully, thank you so much keesiemeijer for your help!
This topic has been resolved, thanks to doc4 and super special thanks to keesiemeijer for his time. Thanks buddy!!!