自定义年度存档插件

时间:2013-02-18 作者:Shwet

我正在使用Annual Archive 在侧边栏中显示年份的插件。我想在下拉列表中只显示在类别17中有帖子的年份。我怎样才能做到这一点
我的代码在下面。。。

    function widget( $args, $instance ) {

    extract($args);
    //$c = $instance[\'count\'] ? \'1\' : \'0\';
    //$d = $instance[\'dropdown\'] ? \'1\' : \'0\';

    $format = empty($instance[\'format\']) ? \'html\' : apply_filters(\'widget_type\', $instance[\'format\']);
    $type = empty($instance[\'type\']) ? \'yearly\' : apply_filters(\'widget_type\', $instance[\'type\']);
    $before = empty($instance[\'before\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'before\']);
    $after = empty($instance[\'after\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'after\']);
    $limit = apply_filters(\'widget_limit\', $instance[\'limit\']);
    $title = apply_filters(\'widget_title\', empty($instance[\'title\']) ? __(\'Annual Archive\', \'anarch\') : $instance[\'title\'], $instance, $this->id_base);

    //$wpdb->query(\'select * from my_plugin_table where foo = "bar"\');

    echo $before_widget;
    if ( $title )
        echo $before_title . $title . $after_title;

    if ($format == \'option\') {
        $dtitle = __(\'Select Year\', \'anarch\');
        if ($type == \'monthly\'){
            $dtitle = __(\'Select Month\', \'anarch\');
        }
        else if($type == \'weekly\'){
            $dtitle = __(\'Select Week\', \'anarch\');
        }
        else if($type == \'daily\'){
            $dtitle = __(\'Select Day\', \'anarch\');
        }
        else if($type == \'postbypost\' || $type == \'alpha\'){
            $dtitle = __(\'Select Post\', \'anarch\');
        }

    ?>
    <select name="archive-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> <option value=""><?php echo esc_attr(__($dtitle, \'anarch\')); ?></option>  <?php wp_get_archives(apply_filters(\'widget_archive_dropdown_args\', array(\'type\' => $type, \'format\' => \'option\', \'show_post_count\' => $c, \'before\' => $before, \'after\' => $after, \'limit\' => $limit))); ?> </select>
    <?php
    } else {
    ?>
    <ul>
    <?php wp_get_archives(apply_filters(\'widget_archive_args\', array(\'type\' => $type, \'limit\' => $limit, \'format\' => $format, \'before\' => $before, \'after\' => $after, \'show_post_count\' => $c))); ?>
    </ul>
    <?php
    }

    echo $after_widget;
}

1 个回复
最合适的回答,由SO网友:Rajeev Vyas 整理而成

尝试一下,您需要使用两个过滤器“getarchives\\u where”和“getarchives\\u join”来改变归档文件的显示方式并包括类别17。。

function widget( $args, $instance ) {
extract($args);
    //$c = $instance[\'count\'] ? \'1\' : \'0\';
    //$d = $instance[\'dropdown\'] ? \'1\' : \'0\';

    $format = empty($instance[\'format\']) ? \'html\' : apply_filters(\'widget_type\', $instance[\'format\']);
    $type = empty($instance[\'type\']) ? \'yearly\' : apply_filters(\'widget_type\', $instance[\'type\']);
    $before = empty($instance[\'before\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'before\']);
    $after = empty($instance[\'after\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'after\']);
    $limit = apply_filters(\'widget_limit\', $instance[\'limit\']);
    $title = apply_filters(\'widget_title\', empty($instance[\'title\']) ? __(\'Annual Archive\', \'anarch\') : $instance[\'title\'], $instance, $this->id_base);

    //$wpdb->query(\'select * from my_plugin_table where foo = "bar"\');

    echo $before_widget;
    if ( $title )
        echo $before_title . $title . $after_title;

    if ($format == \'option\') {
        $dtitle = __(\'Select Year\', \'anarch\');
        if ($type == \'monthly\'){
            $dtitle = __(\'Select Month\', \'anarch\');
        }
        else if($type == \'weekly\'){
            $dtitle = __(\'Select Week\', \'anarch\');
        }
        else if($type == \'daily\'){
            $dtitle = __(\'Select Day\', \'anarch\');
        }
        else if($type == \'postbypost\' || $type == \'alpha\'){
            $dtitle = __(\'Select Post\', \'anarch\');
        }

add_filter( \'getarchives_where\', \'customarchives_where\' );
add_filter( \'getarchives_join\', \'customarchives_join\' );

function customarchives_join( $x ) {

    global $wpdb;

    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";

}

function customarchives_where( $x ) {

    global $wpdb;

    $include = \'17\'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = \'category\' AND $wpdb->term_taxonomy.term_id IN ($include)";

}

    ?>
    <select name="archive-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> <option value=""><?php echo esc_attr(__($dtitle, \'anarch\')); ?></option> <?php wp_get_archives(apply_filters(\'widget_archive_dropdown_args\', array(\'type\' => $type, \'format\' => \'option\', \'show_post_count\' => $c, \'before\' => $before, \'after\' => $after, \'limit\' => $limit))); ?> </select>
    <?php
    } else {
    ?>
    <ul>
    <?php wp_get_archives(apply_filters(\'widget_archive_args\', array(\'type\' => $type, \'limit\' => $limit, \'format\' => $format, \'before\' => $before, \'after\' => $after, \'show_post_count\' => $c))); ?>
    </ul>
    <?php
    }

    echo $after_widget;

remove_filter( \'getarchives_where\', \'customarchives_where\' );
remove_filter( \'getarchives_join\', \'customarchives_join\' );

}

结束

相关推荐

自定义年度存档插件 - 小码农CODE - 行之有效找到问题解决它

自定义年度存档插件

时间:2013-02-18 作者:Shwet

我正在使用Annual Archive 在侧边栏中显示年份的插件。我想在下拉列表中只显示在类别17中有帖子的年份。我怎样才能做到这一点
我的代码在下面。。。

    function widget( $args, $instance ) {

    extract($args);
    //$c = $instance[\'count\'] ? \'1\' : \'0\';
    //$d = $instance[\'dropdown\'] ? \'1\' : \'0\';

    $format = empty($instance[\'format\']) ? \'html\' : apply_filters(\'widget_type\', $instance[\'format\']);
    $type = empty($instance[\'type\']) ? \'yearly\' : apply_filters(\'widget_type\', $instance[\'type\']);
    $before = empty($instance[\'before\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'before\']);
    $after = empty($instance[\'after\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'after\']);
    $limit = apply_filters(\'widget_limit\', $instance[\'limit\']);
    $title = apply_filters(\'widget_title\', empty($instance[\'title\']) ? __(\'Annual Archive\', \'anarch\') : $instance[\'title\'], $instance, $this->id_base);

    //$wpdb->query(\'select * from my_plugin_table where foo = "bar"\');

    echo $before_widget;
    if ( $title )
        echo $before_title . $title . $after_title;

    if ($format == \'option\') {
        $dtitle = __(\'Select Year\', \'anarch\');
        if ($type == \'monthly\'){
            $dtitle = __(\'Select Month\', \'anarch\');
        }
        else if($type == \'weekly\'){
            $dtitle = __(\'Select Week\', \'anarch\');
        }
        else if($type == \'daily\'){
            $dtitle = __(\'Select Day\', \'anarch\');
        }
        else if($type == \'postbypost\' || $type == \'alpha\'){
            $dtitle = __(\'Select Post\', \'anarch\');
        }

    ?>
    <select name="archive-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> <option value=""><?php echo esc_attr(__($dtitle, \'anarch\')); ?></option>  <?php wp_get_archives(apply_filters(\'widget_archive_dropdown_args\', array(\'type\' => $type, \'format\' => \'option\', \'show_post_count\' => $c, \'before\' => $before, \'after\' => $after, \'limit\' => $limit))); ?> </select>
    <?php
    } else {
    ?>
    <ul>
    <?php wp_get_archives(apply_filters(\'widget_archive_args\', array(\'type\' => $type, \'limit\' => $limit, \'format\' => $format, \'before\' => $before, \'after\' => $after, \'show_post_count\' => $c))); ?>
    </ul>
    <?php
    }

    echo $after_widget;
}

1 个回复
最合适的回答,由SO网友:Rajeev Vyas 整理而成

尝试一下,您需要使用两个过滤器“getarchives\\u where”和“getarchives\\u join”来改变归档文件的显示方式并包括类别17。。

function widget( $args, $instance ) {
extract($args);
    //$c = $instance[\'count\'] ? \'1\' : \'0\';
    //$d = $instance[\'dropdown\'] ? \'1\' : \'0\';

    $format = empty($instance[\'format\']) ? \'html\' : apply_filters(\'widget_type\', $instance[\'format\']);
    $type = empty($instance[\'type\']) ? \'yearly\' : apply_filters(\'widget_type\', $instance[\'type\']);
    $before = empty($instance[\'before\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'before\']);
    $after = empty($instance[\'after\']) ? \'\' : apply_filters(\'widget_type\', $instance[\'after\']);
    $limit = apply_filters(\'widget_limit\', $instance[\'limit\']);
    $title = apply_filters(\'widget_title\', empty($instance[\'title\']) ? __(\'Annual Archive\', \'anarch\') : $instance[\'title\'], $instance, $this->id_base);

    //$wpdb->query(\'select * from my_plugin_table where foo = "bar"\');

    echo $before_widget;
    if ( $title )
        echo $before_title . $title . $after_title;

    if ($format == \'option\') {
        $dtitle = __(\'Select Year\', \'anarch\');
        if ($type == \'monthly\'){
            $dtitle = __(\'Select Month\', \'anarch\');
        }
        else if($type == \'weekly\'){
            $dtitle = __(\'Select Week\', \'anarch\');
        }
        else if($type == \'daily\'){
            $dtitle = __(\'Select Day\', \'anarch\');
        }
        else if($type == \'postbypost\' || $type == \'alpha\'){
            $dtitle = __(\'Select Post\', \'anarch\');
        }

add_filter( \'getarchives_where\', \'customarchives_where\' );
add_filter( \'getarchives_join\', \'customarchives_join\' );

function customarchives_join( $x ) {

    global $wpdb;

    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";

}

function customarchives_where( $x ) {

    global $wpdb;

    $include = \'17\'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = \'category\' AND $wpdb->term_taxonomy.term_id IN ($include)";

}

    ?>
    <select name="archive-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> <option value=""><?php echo esc_attr(__($dtitle, \'anarch\')); ?></option> <?php wp_get_archives(apply_filters(\'widget_archive_dropdown_args\', array(\'type\' => $type, \'format\' => \'option\', \'show_post_count\' => $c, \'before\' => $before, \'after\' => $after, \'limit\' => $limit))); ?> </select>
    <?php
    } else {
    ?>
    <ul>
    <?php wp_get_archives(apply_filters(\'widget_archive_args\', array(\'type\' => $type, \'limit\' => $limit, \'format\' => $format, \'before\' => $before, \'after\' => $after, \'show_post_count\' => $c))); ?>
    </ul>
    <?php
    }

    echo $after_widget;

remove_filter( \'getarchives_where\', \'customarchives_where\' );
remove_filter( \'getarchives_join\', \'customarchives_join\' );

}

相关推荐