Widget Categories WordPress,更改显示数量 [英] Widget Categories WordPress, change display count

查看:18
本文介绍了Widget Categories WordPress,更改显示数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 WordPress 和 Genesis 框架如果我在侧边栏中添加小部件类别",我会显示项目数,数字会括在括号中.例如:(2), (12), (7)...

Using WordPress and Genesis Framework If I add the widget "Categories" in the sidebar I display the count of items, the number is enclosed in parentheses. For example: (2), (12), (7)...

为了实现这一点,我打开了文件 wp-includes/category-template.php我发现以下行:

To achieve this, I opened the file wp-includes/category-template.php I found the following line:

if ( !empty($show_count) )
        $link .= ' (' . number_format_i18n( $category->count ) . ')';

然后我按以下方式

if ( !empty($show_count) )
        $link .= ' <div class="myclass">' . number_format_i18n( $category->count ) . '</div>';

在文件 style.css 中,我创建了类 .myclass 并且一切正常,如下例所示:http://i.imgur.com/vdtCbjm.jpg

in the file style.css I created the class .myclass and everything works beautifully, as in the following example: http://i.imgur.com/vdtCbjm.jpg

但我不认为修改WordPress的核心是一件好事.如何在不更改 wordpress 的情况下获得相同的结果?我想在我的主题的functions.php 文件中使用那段代码,但我不知道该怎么做.我不是程序员,发现那段代码已经疯了.我使用 Genesis Framework,带有主题子示例.

But I do not think it's a good thing modifying the core of WordPress. How can I get the same result without changing wordpress? I want to use that piece of code in the functions.php file of my theme, but I do not know how to do that. I'm not a programmer, and to find that piece of code have gone mad. I Use Genesis Framework, with theme child sample.

感谢您的帮助

推荐答案

您可以通过在主题中创建另一个小部件来获得相同的结果.

You can get same result by creating another widget in theme.

以下代码将创建另一个Widget.

Following code will create another Widget.

并且您也可以通过更改$replacement 字符串来更改显示.记住,不要更改$1, $2, $3, $4 变量.

And you also can change display by changes $replacement String. Remember, Don't change $1, $2, $3, $4 variables.

将此代码添加到您的主题的functions.php文件中:-

Add This code into your theme's functions.php file:-

// Register our tweaked Category Archives widget
function myprefix_widgets_init() {
    register_widget( 'WP_Widget_Categories_custom' );
}
add_action( 'widgets_init', 'myprefix_widgets_init' );

/**
 * Duplicated and tweaked WP core Categories widget class
 */
class WP_Widget_Categories_Custom extends WP_Widget {

    function __construct()
    {
        $widget_ops = array( 'classname' => 'widget_categories widget_categories_custom', 'description' => __( "A list of categories, with slightly tweaked output.", 'mytextdomain'  ) );
        parent::__construct( 'categories_custom', __( 'Categories Custom', 'mytextdomain' ), $widget_ops );
    }

    function widget( $args, $instance )
    {
        extract( $args );

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories Custom', 'mytextdomain'  ) : $instance['title'], $instance, $this->id_base);

        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title;
        ?>
        <ul>
            <?php
            // Get the category list, and tweak the output of the markup.
            $pattern = '#<li([^>]*)><a([^>]*)>(.*?)<\/a>\s*\(([0-9]*)\)\s*<\/li>#i';  // removed ( and )

            // $replacement = '<li$1><a$2>$3</a><span class="cat-count">$4</span>'; // no link on span
            // $replacement = '<li$1><a$2>$3</a><span class="cat-count"><a$2>$4</a></span>'; // wrap link in span
            $replacement = '<li$1><a$2><span class="cat-name">$3</span> <span class="cat-count">($4)</span></a>'; // give cat name and count a span, wrap it all in a link


        $args = array(
                'orderby'       => 'name',
                'order'         => 'ASC',
                'show_count'    => 1,
                'title_li'      => '',
                'exclude'       => '2,5,31',
                'echo'          => 0,
                'depth'         => 1,
        );

            $subject      = wp_list_categories( $args );
            echo preg_replace( $pattern, $replacement, $subject );
            ?>
        </ul>
        <?php
        echo $after_widget;
    }

    function update( $new_instance, $old_instance )
    {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['count'] = 1;
        $instance['hierarchical'] = 0;
        $instance['dropdown'] = 0;

        return $instance;
    }

    function form( $instance )
    {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
        $title = esc_attr( $instance['title'] );
        $count = true;
        $hierarchical = false;
        $dropdown = false;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title', 'mytextdomain' ); ?>"><?php _e( 'Title:', 'mytextdomain'  ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" <?php checked( $count ); ?> disabled="disabled" />
        <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts', 'mytextdomain'  ); ?></label>
        <br />
        <?php
    }
}

希望对您有所帮助.

这篇关于Widget Categories WordPress,更改显示数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆