I think you are looking for display related posts. If yes, there are some plugins which do that.
Just search on google for display related posts wordpress plugin
Isn’t there a way to do this with, excluding category if category = 1 …. or something like that.
I really don’t like to use to many plugins for these kinda things.
You’ll need a function or something to automate it, a plugin is just a series of functions, classes, variables etc…
Find a basic one that covers what you want, and strip the code out if you don’t want to use it as a plugin..
Agreeing with kapiljain.in and t31os, but for your educational use:
In a category template this returns the current queried category:
$cat = get_query_var('cat');
Returns first category on a post:
$cats = wp_get_post_categories($post->ID);
$first_cat = $cats[0];
@michaelh:
Thank you for the educational bit, but I have to admit that I know nothing or very little about implementing PHP code.
<?php $my_query = new WP_Query('showposts=6');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
Where in this bit should I try to implement
$cat = get_query_var('cat');
?
If you are using a category template, WordPress will handle the query for you.
But just in case you need it:
$cat = get_query_var('cat');
$args=array(
'cat__in' => array($cat),
'showposts'=>6
);
$my_query = new WP_Query($args);
See template tag, query_posts()
<?php
$cat = get_query_var('cat');
$args=array(
'cat__in' => array($cat),
'showposts'=> 6
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<div class="contentslider_menu">
<div class="contentslider_menubalk_streep">
<div class="contentslider_menubalk">
<ul><li>
<a href="<?php the_permalink(); ?>" class="toc">
<span class="knop_titel"><?php the_title(); ?></span>
<span class="knop_datum"><?php the_time('j F Y H:i'); ?></span>
<span class="knop_category"><?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';} ?></span>
</a>
</li></ul>
</div></div></div>
<?php endwhile; ?>
I’m using this now, but it still gives me the 6 newest posts from all categories. I’m using this code in the header.php because I want to minimize my code, I wanted it to work on category.php and single.php
You’ll want that in a category template.
@michaelh:
It doesnt work, it still gets me all news posts of all categories combined, but that kinda makes sense, it shouldn’t matter if its in the header of single or category.php right?
I don’t want to make a catergory-1.php category-2.php etc. i’m really trying to minimalize the code.
This is wrong
$args=array(
'cat__in' => array($cat),
'showposts'=> 6
);
change to
$args=array(
'cat' => $cat,
'showposts'=> 6
);
Of course, I don’t know why you are showing posts for the given category in a category template–seems repetitive.
@michaelh:
It did the trick for categories but with single posts it still shows the 6 newest posts of all categories.
The reason I want this to work, is because i’m using a content-slider in the header. I want it to show the posts in the specific categories and of specific categories. On the homepage it does what it has to do, showing all the newest posts of all categories.
[link moderated]
I’m sorry for not being clear enough ….
For a single post template, this should work:
if ( is_single() ) (
$cats = wp_get_post_categories($post->ID);
$args=array(
'cat' => $cats[0],
'showposts'=> 6
);
}
<?php
if ( is_single() ) (
$cats = wp_get_post_categories($post->ID);
$args=array(
'cat' => $cats[0],
'showposts'=> 6
);
}
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
I’ve got that right now, but this doesn’t work.
I don’t understand the } closing bracket… isn’t this suppose to be a ) ?
I’ve also tried to close the ) and replace the last } with a ), but that doesn’t work either.
<?php
if ( is_single(); ); (
$cats = wp_get_post_categories($post->ID);
$args=array(
'cat' => $cats[0],
'showposts'=> 6
);
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
P.S.: Would you be so kind to remove the URL I mentioned some posts above this one?
@michealh:
Perfect, but now it only detects on single.php, on the index.php and category.php it doesn’t detect any posts anymore π
Maybe an -else- somewhere?
(Thanks very much for removing the url btw!)