ExcludeCat函数颠倒博客帖子的顺序

时间:2014-06-28 作者:Desi

我正在使用下面的代码从主页上的查询中删除类别#5377。然而,出于某种原因,此代码也颠倒了博客帖子的顺序。如何将此代码更改为仅删除类别#5377并保持相同的帖子顺序?

function excludeCat($query) {
    if ( $query->is_home ) {
        $query->set(\'cat\', \'-5377\');
    }
    return $query;
}
add_filter(\'pre_get_posts\', \'excludeCat\');
编辑1:完整index.php 代码:

<?php get_header(); ?>

    <?php if (dt_is_sidebar_active(\'homepage_ad\')) : ?>
        <div id="homepage-ad" class="widget-area">
                <?php dynamic_sidebar(\'homepage_ad\'); ?>
        </div><!-- #homepage-ad .widget-area -->
    <?php endif; ?>

    <!--BEGIN #content -->
    <div id="content">

        <div id="sidebar">
            <div id="home-sidebar" class="widget-area">
                    <?php get_sidebar(); ?>
            </div><!-- #home-sidebar .widget-area -->
        </div><!-- #sidebar -->

        <!--BEGIN #masonry -->  
        <div id="masonry">

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <!--BEGIN .item --> 
            <div class="item normal" data-order=\'1\'>

                <!--BEGIN .hentry -->
                <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">

                    <?php if ( has_post_thumbnail() ) : ?>
                    <!--BEGIN .featured-image -->
                    <div class="featured-image">
                        <div class="da-hover">
                            <span class="da-wrap">
                                <span class="title"><?php the_title(); ?></span>
                                <?php dt_overlay_icon(); ?>
                            </span>
                        </div>
                        <a href="<?php the_permalink(); ?>"><?php dt_image(300, \'\'); ?></a>
                    <!--END .featured-image -->
                    </div>
                    <?php endif; ?>

                    <span class="meta-category"><?php the_category(\', \'); ?></span>

                    <h2 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

                    <!--BEGIN .post-content -->
                    <div class="post-content">

                        <?php $format = get_post_format(); ?>
                        <?php if ($format == "image" || $format == "gallery" || $format == "video") : ?>
                        <span class="icon"><img src="<?php echo get_template_directory_uri(); ?>/images/icon-<?php echo $format; ?>.png" alt="<?php echo $format; ?>" /></span>
                        <?php endif; ?>

                        <?php dt_excerpt(20); ?>

                    <!--END .post-content -->
                    </div>

                    <a class="read-more" href="<?php the_permalink(); ?>"><?php _e(\'Read More\', \'engine\'); ?></a>

                    <!--BEGIN .post-footer -->
                    <div class="post-footer">

                        <span class="meta-published"><?php echo human_time_diff( get_the_time(\'U\'), current_time(\'timestamp\') ) . \' \'.  __(\'ago\', \'engine\'); ?></span>

                        <span class="meta-comments"><?php comments_number(__(\'No Comments\', \'engine\'), __(\'1 Comment\', \'engine\'), __(\'% Comments\', \'engine\')); ?></span>

                    </div>
                    <!--END .post-footer -->

                <!--END .hentry-->  
                </div>

            <!--END .item -->   
            </div>
            <?php endwhile; endif; ?>

            <?php get_template_part(\'includes/index-loadmore\'); ?>

        <!--END #masonry -->
        </div>

        <div id="masonry-new"></div>

        <!--BEGIN .post-navigation -->
        <div class="post-navigation clearfix">
            <?php dt_pagination(); ?>
        <!--END .post-navigation -->
        </div>

    </div><!-- #content -->

<?php get_footer(); ?>
编辑2:添加<?php global $wp_query; var_dump($wp_query->query_vars); ?> 就在之前<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 在上面的代码中得到:

array(61) {
  ["error"]=>
  string(0) ""
  ["m"]=>
  string(0) ""
  ["p"]=>
  int(0)
  ["post_parent"]=>
  string(0) ""
  ["subpost"]=>
  string(0) ""
  ["subpost_id"]=>
  string(0) ""
  ["attachment"]=>
  string(0) ""
  ["attachment_id"]=>
  int(0)
  ["name"]=>
  string(0) ""
  ["static"]=>
  string(0) ""
  ["pagename"]=>
  string(0) ""
  ["page_id"]=>
  int(0)
  ["second"]=>
  string(0) ""
  ["minute"]=>
  string(0) ""
  ["hour"]=>
  string(0) ""
  ["day"]=>
  int(0)
  ["monthnum"]=>
  int(0)
  ["year"]=>
  int(0)
  ["w"]=>
  int(0)
  ["category_name"]=>
  string(0) ""
  ["tag"]=>
  string(0) ""
  ["cat"]=>
  string(0) ""
  ["tag_id"]=>
  string(0) ""
  ["author"]=>
  string(0) ""
  ["author_name"]=>
  string(0) ""
  ["feed"]=>
  string(0) ""
  ["tb"]=>
  string(0) ""
  ["paged"]=>
  int(0)
  ["comments_popup"]=>
  string(0) ""
  ["meta_key"]=>
  string(0) ""
  ["meta_value"]=>
  string(0) ""
  ["preview"]=>
  string(0) ""
  ["s"]=>
  string(0) ""
  ["sentence"]=>
  string(0) ""
  ["fields"]=>
  string(0) ""
  ["menu_order"]=>
  string(0) ""
  ["category__in"]=>
  array(0) {
  }
  ["category__not_in"]=>
  array(0) {
  }
  ["category__and"]=>
  array(0) {
  }
  ["post__in"]=>
  array(0) {
  }
  ["post__not_in"]=>
  array(0) {
  }
  ["tag__in"]=>
  array(0) {
  }
  ["tag__not_in"]=>
  array(0) {
  }
  ["tag__and"]=>
  array(0) {
  }
  ["tag_slug__in"]=>
  array(0) {
  }
  ["tag_slug__and"]=>
  array(0) {
  }
  ["post_parent__in"]=>
  array(0) {
  }
  ["post_parent__not_in"]=>
  array(0) {
  }
  ["author__in"]=>
  array(0) {
  }
  ["author__not_in"]=>
  array(0) {
  }
  ["ignore_sticky_posts"]=>
  bool(false)
  ["suppress_filters"]=>
  bool(false)
  ["cache_results"]=>
  bool(true)
  ["update_post_term_cache"]=>
  bool(true)
  ["update_post_meta_cache"]=>
  bool(true)
  ["post_type"]=>
  string(0) ""
  ["posts_per_page"]=>
  int(6)
  ["nopaging"]=>
  bool(false)
  ["comments_per_page"]=>
  string(2) "50"
  ["no_found_rows"]=>
  bool(false)
  ["order"]=>
  string(4) "DESC"
}

1 个回复
SO网友:Brad Dalton

function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( \'cat\', \'-5377\' );
    $query->set( \'orderby\', \'date\' );
    $query->set( \'order\', \'DESC\' );
    }
}
add_action( \'pre_get_posts\', \'exclude_category\' );
对订单使用ASC或DESC。

向子主题函数文件添加代码。

已测试并正常工作。

结束

相关推荐

如何根据页面模板使用idangero快捷键的不同jQuery函数?

我一直在努力将idangero swiper jquery插件添加到我的站点中,但还是成功地生成了下面的代码。这非常有效,但仅适用于中定义的一个自定义页面模板function register_swiper() 以下代码的一部分。但是,我需要在另一组使用不同页面模板的页面上使用swiper,为滑块使用不同的选项,如中所定义var mySwiper = $(\'.swiper-container\').swiper({ mode:\'horizontal\', 例如slidesPerView: 3. f