开发者

WordPress: Prevent Showing of Sub Category Posts

I'd like to know how to prevent showing of sub-category posts. My home page lists all posts from three "main categories" (parent category), but unfortunately it's also listing some posts from the sub-categories.

Here's the code that I'm using to get the posts from specific category:

<h2>Category Name</h2>
<ul>
    <?php $category_query = new WP_Query(array('category_name' => 'category1', 'showposts' => 5)); ?>
    <?php while ($profissionais_query->have_posts()) : $profissionais_query->the_post(); ?>
    <li>
        <a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_excerpt(); ?>
    </li>
    <?php endwhile; ?>
</ul>

Does 开发者_C百科anyone have an idea?

Thank you.


Try this style of new query; it only shows the one category. It can be used mutliple times in a page or post (with php execution enabled) without conflict:

<?php $my_query = new WP_Query('category_name=mycategory&showposts=5'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php the_excerpt(); ?>
<?php endwhile; ?>


This should work:

<?php $category_ID = $cat; // get ID of current category ?>

<?php $excludes = get_categories('child_of='.$category_ID) ;

    // For each child, add just the ID to an array
    foreach ( $excludes as $key => $value ){
        $exs[] = $value->cat_ID;
    }

$my_query = new WP_Query(array(
            'cat' => $category_ID,
            'category__not_in' => $exs

));
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
?>


Below code will show posts only from the current category

<?php
$current_cat = get_query_var('cat');

$args=array(
    'category__in' => array($current_cat),
    'showposts' => 5
);

query_posts($args);

set_query_var("cat",$current_cat);

if (have_posts()) :

    while (have_posts()) : the_post();
?>
        <a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_excerpt(); ?>
<?php

    endwhile;

else : 

?>
        <h2>Nothing found</h2>
<?php 

endif;

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜