IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    begin二次开发-修改随机文章小工具指定/排除某分类

    yumanutong发表于 2015-12-27 04:32:12
    love 0

    begin自带一个随机文章的小工具,可以拿来放页脚或者侧边栏。但是,有一些分类也许不想显示。怎么办呢?

    首先,找到文件:/wp-content/themes/begin/inc/core/widgets.php

    PS:我是怎么找到这个文件的呢?建设我是一个菜鸟,我可以利用文件搜索功能,搜索相对的关键字,如果找不到呢?可以找同一个类型的,比如说tag小工具,搜索tag总可以吧?或者搜索widget。

    OK。找到文件,我们编辑器打开,进去搜索下关键词“随机文章”,看到有两处地方,其中一处关键在这里:

    Code   ViewPrint
    1. // 随机文章
    2. class random_post extends WP_Widget {
    3.      function random_post() {
    4.         $widget_ops = array('description' => '显示随机文章');
    5.         $this->WP_Widget('random_post', '主题  随机文章', $widget_ops);
    6.      }
    7.      function widget($args, $instance) {
    8.         extract($args);
    9.         $title = apply_filters( 'widget_title', $instance['title'] );
    10.         echo $before_widget;
    11.         if ( ! emptyempty( $title ) )
    12.         echo $before_title . $title . $after_title;
    13.         $number = strip_tags($instance['number']) ? absint( $instance['number'] ) : 5;
    14. ?>
    15. <div id="random_post_widget">
    16.     <ul>
    17.         <?php query_posts( array ( 'orderby' => 'rand', 'showposts' => $number, 'ignore_sticky_posts' => 10 ) ); while ( have_posts() ) : the_post(); ?>
    18.             <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    19.         <?php endwhile; ?>
    20.         <?php wp_reset_query(); ?>
    21.     </ul>
    22. </div>

    这个代码第18行可以看到有一组数组在这里,就是改这里了。如果要指定分类,那么:

    Code   ViewPrint
    1. <?php
    2. //原代码
    3. query_posts( array ( 'orderby' => 'rand', 'showposts' => $number, 'ignore_sticky_posts' => 10 ) ); while ( have_posts() ) : the_post();
    4. // 修改为 
    5. query_posts( array ( 'orderby' => 'rand', 'showposts' => $number, 'category__in' => array(1,2), 'ignore_sticky_posts' => 10 ) ); while ( have_posts() ) : the_post();
    6. ?>

    这里用 category__in 这个系统函数来指定。也可以用 cat

    但是如果只是想排除分类呢?

    Code   ViewPrint
    1. <?php
    2. //原代码
    3. query_posts( array ( 'orderby' => 'rand', 'showposts' => $number, 'ignore_sticky_posts' => 10 ) ); while ( have_posts() ) : the_post();
    4. // 修改为 
    5. query_posts( array ( 'orderby' => 'rand', 'showposts' => $number, 'category__not_in' => array(3,4), 'ignore_sticky_posts' => 10 ) ); while ( have_posts() ) : the_post();
    6. ?>

    这里用 category__not_in 这个系统函数来指定排除的分类。

    修改好了文件,保存,替换线上的文件,然后多刷新几次测试看看,看看有没有显示被排除的分类。

    参考:

    widgets-category-1223

     



沪ICP备19023445号-2号
友情链接