获取每个术语的Wordpress自定义后分类描述

时间:2013-06-24 21:55:29

标签: wordpress taxonomy term

我正在尝试显示Wordpress Woothemes小部件“品牌缩略图”的分类法描述。

这里的代码返回一个随机的术语(即1个随机品牌),但我还希望它包含随机品牌描述。最好用div包裹。

/** Variables to setup the widget. */
var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;

/** constructor */
function __construct() {

    /* Widget variable settings. */
    $this->woo_widget_name          = __('WooCommerce Brand Thumbnails MJ', 'wc_brands' );
    $this->woo_widget_description   = __( 'Show a RANDOM Brand (Supplier) Thumbnail', 'wc_brands' );
    $this->woo_widget_idbase        = 'wc_brands_brand_thumbnails';
    $this->woo_widget_cssclass      = 'widget_brand_thumbnails';

    /* Widget settings. */
    $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );

    /* Create the widget. */
    $this->WP_Widget( $this->woo_widget_idbase, $this->woo_widget_name, $widget_ops );

}

/** @see WP_Widget */
function widget( $args, $instance ) {
    extract( $args );

    $exclude = array_map( 'intval', explode( ',', $instance['exclude'] ) );
    $order = $instance['orderby'] == 'name' ? 'asc' : 'desc';


    /**MJ added args and else function MJ*/
    $args = array( 'hide_empty' => $instance['hide_empty'], 'orderby' => $instance['orderby'], 'exclude' => $exclude,               'number' => $instance['number'], 'order' => $order );
    if ($instance['orderby'] == 'random') {
    $args['orderby'] = 'none';
    $brands = get_terms( 'product_brand', $args );
    shuffle($brands);
    $brands = array_slice($brands, 0, 1);

    }
    else {
    $brands = get_terms( 'product_brand', $args );
    }

    /**MJ added args and else function MJ*/


    if ( ! $brands ) 
        return;

    echo $before_widget;

    if ( ! empty( $instance['title'] ) )
        echo $before_title . $instance['title'] . $after_title;

    woocommerce_get_template( 'widgets/brand-thumbnailsMJ.php', array(
        'brands'    => $brands,
        'columns'   => $instance['columns']
    ), 'woocommerce-brands', untrailingslashit( plugin_dir_path( dirname( dirname( __FILE__ ) ) ) ) . '/templates/' );

    echo $after_widget;
}

/** @see WP_Widget->update */
function update( $new_instance, $old_instance ) {
    $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) );
    $instance['columns'] = strip_tags( stripslashes( $new_instance['columns'] ) );
    $instance['orderby'] = strip_tags( stripslashes( $new_instance['orderby'] ) );
    $instance['exclude'] = strip_tags( stripslashes( $new_instance['exclude'] ) );
    $instance['hide_empty'] = strip_tags( stripslashes( $new_instance['hide_empty'] ) );
    $instance['number'] = strip_tags( stripslashes( $new_instance['number'] ) );

    if ( ! $instance['columns'] )
        $instance['columns'] = 1;

    if ( ! $instance['orderby'] )
        $instance['orderby'] = 'name';

    if ( ! $instance['exclude'] )
        $instance['exclude'] = '';

    if ( ! $instance['hide_empty'] )
        $instance['hide_empty'] = 0;

    if ( ! $instance['number'] )
        $instance['number'] = '';

    return $instance;
}

/** @see WP_Widget->form */
function form( $instance ) {
    if ( ! isset( $instance['hide_empty'] ) ) 
        $instance['hide_empty'] = 0;
    if ( ! isset( $instance['orderby'] ) ) 
        $instance['orderby'] = 'name';

    ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php if ( isset ( $instance['title'] ) ) echo esc_attr( $instance['title'] ); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'columns' ); ?>"><?php _e('Columns:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'columns' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'columns' ) ); ?>" value="<?php if ( isset ( $instance['columns'] ) ) echo esc_attr( $instance['columns'] ); else echo '1'; ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php if ( isset ( $instance['number'] ) ) echo esc_attr( $instance['number'] ); ?>" placeholder="<?php _e('All', 'wc_brands'); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e('Exclude:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php if ( isset ( $instance['exclude'] ) ) echo esc_attr( $instance['exclude'] ); ?>" placeholder="<?php _e('None', 'wc_brands'); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>"><?php _e('Hide empty brands:', 'wc_brands') ?></label>
            <select id="<?php echo esc_attr( $this->get_field_id( 'hide_empty' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_empty' ) ); ?>">
                <option value="1" <?php selected( $instance['hide_empty'], 1 ) ?>><?php _e('Yes', 'wc_brands') ?></option>
                <option value="0" <?php selected( $instance['hide_empty'], 0 ) ?>><?php _e('No', 'wc_brands') ?></option>
            </select>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Order by:', 'wc_brands') ?></label>
            <select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
                <option value="name" <?php selected( $instance['orderby'], 'name' ) ?>><?php _e('Name', 'wc_brands') ?></option>
                <option value="count" <?php selected( $instance['orderby'], 'count' ) ?>><?php _e('Count', 'wc_brands') ?></option>
                <option value="random" <?php selected( $instance['orderby'], 'random' ) ?>><?php _e('Random', 'wc_brands') ?></option>

            </select>
        </p>
    <?php
}

}

1 个答案:

答案 0 :(得分:0)

我修好了。因此,对于偶然发现类似问题的其他人,我使用了

<?php echo $brand->description; ?>

我还需要截断它,我在function.php中使用了这个函数

function limit_words($string, $word_limit) {
$newstring = substr($string, 0, $word_limit);
if(strlen($newstring) < strlen($string))
$newstring = $newstring."...";
echo "<p>".$newstring."</p>";
}

和此修改后的描述调用(而不是上面的描述调用)

<?php echo limit_words($brand->description,100); ?>