egor maltsev
asked 9 years ago

Hi.

One question – i need to display last new post types by new term in awesome DW Focus: News List widget, but i don’t know how i can it?

P.s. i need display don’t "post" by "tags", but some new "books" by term "genre", for example.

2 Answers
Allen
answered 9 years ago

@egor maltsev : hi , about your issue , i’m not sure what you really want , can you explain it clearly? It’s like you have a custom post type name books that have the tag genre ?? and you want to show it like News List widget ? if that is it , you need a new widget for that custom post type and i canhelp 🙂

egmalt
replied 9 years ago

Yes, that’s right! I need to display last post "book" by "genre" in News List Widget!

Allen
answered 9 years ago

@egor maltsev : hi, i have this solution for you this can use for different custom post type.
First, you should make a new widget to differ it from the : DW Focus: News List widget

First : create a new file : dw-focus\inc\widgets\dw-custom-posttype-list.php ( you can copy from this link : http://www.mediafire.com/view/gaw04taxru5s6n6/dw-custom-posttype-list.php )

<?php
/**
 * DW Focus Custom post type List Widget
 *
 * @package DW Focus
 * @since DW Focus 1.2.6
 */
add_action( 'widgets_init', 'dw_focus_widgets_custom_post_type_list_init' );
function dw_focus_widgets_custom_post_type_list_init() {
    register_widget( 'DW_Focus_Widget_custom_posttype_list' );
}

class DW_Focus_Widget_custom_posttype_list extends WP_Widget {
    public function __construct() {
        $widget_ops = array( 'classname' => 'dw_focus_widget_custom_posttype_list', 'description' => 'Show Your Posts as List.' );
        parent::__construct( 'custom-posttype-list', 'DW Focus: Custom Post Type List', $widget_ops );
        add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
        add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
        add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
    }

    public function widget( $args, $instance ) {
        $cache = array();
        $category_style = '';
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get( 'dw_focus_widget_custom_posttype_list', 'widget' );
        }

        if ( ! is_array( $cache ) ) {
            $cache = array();
        }

        if ( ! isset( $args['widget_id'] ) ) {
            $args['widget_id'] = $this->id;
        }

        if ( isset( $cache[ $args['widget_id'] ] ) ) {
            echo $cache[ $args['widget_id'] ];
            return;
        }

        ob_start();

        $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
        $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
        $show_content = ( ! empty( $instance['show_content'] )) ? $instance['show_content'] : '';
        $cat_id = ( ! empty( $instance['cat_id'] ) ) ? absint( $instance['cat_id'] ) : 0;
        $tags = ( ! empty( $instance['tags'] ) ) ? $instance['tags'] : '';
        $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
        $show_author = isset( $instance['show_author'] ) ? $instance['show_author'] : false;
        $show_comment = isset( $instance['show_comment'] ) ? $instance['show_comment'] : false;
        $show_sub_cat = isset( $instance['show_sub_cat'] ) ? $instance['show_sub_cat'] : false;
        $post_format = isset( $instance['post_format'] ) ? $instance['post_format'] : '';

        $query = array(
                    'posts_per_page' => $number,
                    'no_found_rows' => true,
                    'post_status' => 'publish',
                    'ignore_sticky_posts' => true,
                    'post_type' => '< your-custom-post-type >',
                );
        if ( $post_format ) {
                $query['post_format'] = 'post-format-'.$post_format;
        }

        if ( '' != $tags && 0 != $cat_id ) {
            $query['tax_query'] = array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => '< your-custom-category >',
                        'field' => 'id',
                        'terms' => array( $cat_id ),
                        ),
                    array(
                        'taxonomy' => '< your-custom-tag >',
                        'field' => 'name',
                        'terms' => explode( ',', $tags ),
                        ),
                );
        } else {
            if ( '' != $tags ) {
                $query['tag_slug__in'] = explode( ',', $tags );
            }

            if ( 0 != $cat_id ) {
                $query['cat'] = $cat_id;
                $options = dw_focus_get_category_option( $cat_id );
                if ( 'none' != $options['style'] ) {
                    $category_style = 'color-'.$options['style'];
                } else {
                    $category_style = '';
                }
            }
        }

        $r = new WP_Query( apply_filters( 'dw_focus_widget_custom_posttype_list', $query ) );

        if ( $r->have_posts() ) :
?>
        <?php echo $args['before_widget']; ?>
        <div class="<?php echo esc_attr( $category_style ); ?>">
        <?php if ( $title ) : ?>
            <?php echo $args['before_title']; ?>
                    <?php if ( 0 != $cat_id ) : ?>
                        <a href="<?php echo esc_url( get_category_link( $cat_id ) ); ?>"><?php echo wp_kses_post( $title ); ?></a>
                    <?php else : ?>
                        <?php echo wp_kses_post( $title ); ?>
                    <?php endif; ?>
                <?php echo $args['after_title']; ?>
            <?php if ( $show_sub_cat && ( 0 != $cat_id ) ) : ?>
                <?php $sub_categories = get_categories( 'hide_empty=0&child_of='.$cat_id ); ?>
                <?php if ( $sub_categories ) : ?>
                <ul class="sub-categories list-inline hidden-xs">
                    <?php foreach ( $sub_categories as $sub_category ) { ?>
                        <li><a href="<?php echo esc_url( get_category_link( $sub_category->term_id ) ); ?>"><?php echo wp_kses_post( $sub_category->name ); ?></a></li>
                    <?php } ?>
                </ul>
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?>
        <div class="news-grid">
            <div class="row">
            <?php $i = 1; ?>
            <?php while ( $r->have_posts() ) : $r->the_post(); ?>
                <?php if ( 1 === $i ) : ?>
                <div class="col-md-6">
                    <article <?php post_class('post'); ?>>
                        <?php if ( has_post_thumbnail() ) : ?>
                            <div class="entry-thumbnail"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'thumbnail' ); ?></a></div>
                        <?php endif; ?>
                        <h3 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <div class="entry-meta">
                            <?php if ( $show_date ) : ?>
                                <span class="entry-date"><i class="fa fa-clock-o"></i> <?php echo get_the_date( __('F j, Y', 'dw-focus') ); ?></span>
                            <?php endif; ?>
                            <?php if ( $show_author ) : ?>
                                <span class="entry-author"><i class="fa fa-user"></i> <?php the_author(); ?></span>
                            <?php endif; ?>
                            <?php if ( $show_comment && ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
                                <span class="comments-link"><?php _e( '<i class="fa fa-comment"></i> ', 'dw-focus' ); ?><?php comments_popup_link( __( '0', 'dw-focus' ), __( '1', 'dw-focus' ), __( '%', 'dw-focus' ) ); ?></span>
                            <?php endif; ?>
                        </div>

                        <?php if ( 'content' == $show_content ) :
                            $more = 0;
                        ?>
                            <div class="entry-content"><?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'dw-focus' ) ); ?></div>
                        <?php elseif ( 'excerpt' == $show_content ) : ?>
                            <div class="entry-summary"><?php the_excerpt(); ?></div>
                        <?php endif; ?>
                    </article>
                </div>
                <div class="col-md-6">
                    <ul class="list-unstyled">
                <?php else : ?>
                        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endif; ?>
            <?php $i++; ?>
            <?php endwhile; ?>
                    </ul>
                </div>
            </div>
        </div>
        </div>
        <?php echo $args['after_widget']; ?>
        <?php
        wp_reset_postdata();

        endif;

        if ( ! $this->is_preview() ) {
            $cache[ $args['widget_id'] ] = ob_get_flush();
            wp_cache_set( 'dw_focus_widget_custom_posttype_list', $cache, 'widget' );
        } else {
                ob_end_flush();
        }
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['number'] = (int) $new_instance['number'];
        $instance['show_content'] = strip_tags( $new_instance['show_content'] );
        $instance['cat_id'] = (int) $new_instance['cat_id'];
        $instance['tags'] = strip_tags( $new_instance['tags'] );
        $instance['post_format'] = strip_tags( $new_instance['post_format'] );
        $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        $instance['show_author'] = isset( $new_instance['show_author'] ) ? (bool) $new_instance['show_author'] : false;
        $instance['show_comment'] = isset( $new_instance['show_comment'] ) ? (bool) $new_instance['show_comment'] : false;
        $instance['show_sub_cat'] = isset( $new_instance['show_sub_cat'] ) ? (bool) $new_instance['show_sub_cat'] : false;

        $this->flush_widget_cache();

        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset( $alloptions['dw_focus_widget_custom_posttype_list'] ) ) {
            delete_option( 'dw_focus_widget_custom_posttype_list' );
        }
        return $instance;
    }

    public function flush_widget_cache() {
        wp_cache_delete( 'dw_focus_widget_custom_posttype_list', 'widget' );
    }

    public function form( $instance ) {

        $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $show_content = isset( $instance['show_content'] ) ? esc_attr( $instance['show_content'] ) : '';
        $cat_id = isset( $instance['cat_id'] ) ? esc_attr( $instance['cat_id'] ) : 0;
        $tags = isset( $instance['tags'] ) ? esc_attr( $instance['tags'] ) : '';
        $post_format = isset( $instance['post_format'] ) ? esc_attr( $instance['post_format'] ) : '';
        $show_category = isset( $instance['show_category'] ) ? (bool) $instance['show_category'] : false;
        $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
        $show_author = isset( $instance['show_author'] ) ? (bool) $instance['show_author'] : false;
        $show_comment = isset( $instance['show_comment'] ) ? (bool) $instance['show_comment'] : false;
        $show_sub_cat = isset( $instance['show_sub_cat'] ) ? (bool) $instance['show_sub_cat'] : false;
    ?>
        <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'dw-focus' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'dw-focus' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>"><?php _e( 'Display post content?', 'dw-focus' ) ?></label>
        <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>">
            <option value="" <?php selected( $show_content, '' ) ?>></option>
            <option value="excerpt" <?php selected( $show_content, 'excerpt' ) ?>><?php _e( 'Excerpt', 'dw-focus' ); ?></option>
            <option value="content" <?php selected( $show_content, 'content' ) ?>><?php _e( 'Content', 'dw-focus' ); ?></option>
        </select></p>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'cat_id' ) ); ?>"><?php _e( 'Category:', 'dw-focus' ); ?></label>
        <?php wp_dropdown_categories( 'name='.$this->get_field_name( 'cat_id' ).'&class=widefat&show_option_all=All&hide_empty=0&taxonomy=< your-custom-category >&hierarchical=1&depth=2&selected='.$cat_id ); ?></p>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'tags' ) ); ?>"><?php _e( 'Tags:', 'dw-focus' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'tags' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'tags' ) ); ?>" placeholder="<?php _e( 'tag 1, tag 2, tag 3','dw-focus' )?>" type="text" value="<?php echo esc_attr( $tags ); ?>" /></p>

        <p><label for="<?php echo esc_attr( $this->get_field_id( 'post_format' ) ); ?>"><?php _e( 'Post Formats:', 'dw-focus' ); ?></label>
        <?php if ( current_theme_supports( 'post-formats' ) ) {
                $valid_formats = get_theme_support( 'post-formats' );
            ?>
            <select name="<?php echo esc_attr( $this->get_field_name( 'post_format' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'post_format' ) ); ?>" class="widefat">';
            <option <?php selected( $post_format, '' ); ?> value="">All</option>';
            <?php foreach ( $valid_formats[0] as $format ) {
                ?>
                    <option <?php selected( $post_format, $format );?> class="level-0" value="<?php echo esc_attr( $format ); ?>"><?php echo esc_attr( ucfirst( $format ) ); ?></option>';
            <?php } ?>
            </select>
        <?php } ?>
        </p>

        <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" />
        <label for="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>"><?php _e( 'Display post date?', 'dw-focus' ); ?></label></p>

        <p><input class="checkbox" type="checkbox" <?php checked( $show_author ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_author' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_author' ) ); ?>" />
        <label for="<?php echo esc_attr( $this->get_field_id( 'show_author' ) ); ?>"><?php _e( 'Display post author?', 'dw-focus' ); ?></label></p>

        <p><input class="checkbox" type="checkbox" <?php checked( $show_comment ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_comment' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_comment' ) ); ?>" />
        <label for="<?php echo esc_attr( $this->get_field_id( 'show_comment' ) ); ?>"><?php _e( 'Display comment count?', 'dw-focus' ); ?></label></p>

        <p><input class="checkbox" type="checkbox" <?php checked( $show_sub_cat ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_sub_cat' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_sub_cat' ) ); ?>" />
        <label for="<?php echo esc_attr( $this->get_field_id( 'show_sub_cat' ) ); ?>"><?php _e( 'Display sub-categories?', 'dw-focus' ); ?></label></p>
<?php
    }
}

In that file, you can find :

  • your-custom-post-type : replace it with your custom post type( line 61 )
  • your-custom-category : replace it with your custom post type category ( line 71 , 234 )
  • your-custom-tag : replace it with your custom post type tag ( line 76 )

Second: Open file : dw-focus\functions.php go to the last line an add this code :

require get_template_directory() . '/inc/widgets/dw-custom-posttype-list.php';

Last: Copy this style to file : dw-focus\style.css

.dw_focus_widget_custom_posttype_list .widget-title {
  margin: 0;
}
.dw_focus_widget_custom_posttype_list .sub-categories {
  margin-top: -30px;
  float: right;
  text-transform: uppercase;
  font-size: 11px;
  font-family: "Montserrat";
  line-height: 20px;
}
.dw_focus_widget_custom_posttype_list .sub-categories a {
  color: #555555;
}
.dw_focus_widget_custom_posttype_list .sub-categories a:hover,
.dw_focus_widget_custom_posttype_list .sub-categories a:active {
  color: #333333;
}
@media (min-width: 1200px) {
  .dw_focus_widget_custom_posttype_list .row {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
  }
  .dw_focus_widget_custom_posttype_list .row > div:first-child {
    position: relative;
  }
  .dw_focus_widget_custom_posttype_list .row > div:first-child:after {
    content: "";
    width: 1px;
    background: #dddddd;
    height: 100%;
    position: absolute;
    display: block;
    top: 0;
    right: 0;
  }
}
.dw_focus_widget_custom_posttype_list .post {
  padding-left: 130px;
  position: relative;
  min-height: 130px;
  border: none;
  margin-bottom: 0;
  padding-bottom: 0;
  padding-top: 20px;
}
.dw_focus_widget_custom_posttype_list .post .entry-title {
  margin: 0 0 10px;
  font-size: 16px;
  line-height: 22px;
}
.dw_focus_widget_custom_posttype_list .post .entry-thumbnail {
  position: absolute;
  left: 0;
}
.dw_focus_widget_custom_posttype_list .post .entry-thumbnail:hover {
  opacity: .8;
  -webkit-transition: opacity 0.5s;
  -o-transition: opacity 0.5s;
  transition: opacity 0.5s;
}
@media (min-width: 1200px) {
  .dw_focus_widget_custom_posttype_list .post .entry-thumbnail img {
    max-width: 110px;
  }
}
.dw_focus_widget_custom_posttype_list .post .entry-content,
.dw_focus_widget_custom_posttype_list .post .entry-summary {
  color: #999;
}
.dw_focus_widget_custom_posttype_list .list-unstyled {
  margin-top: 10px;
}
.dw_focus_widget_custom_posttype_list .list-unstyled li {
  border-bottom: 1px solid #dddddd;
  padding: 10px 0;
  font-family: "Roboto Slab", Georgia, "Times New Roman", Times, serif;
}
.dw_focus_widget_custom_posttype_list .list-unstyled li:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}
.dw_focus_widget_custom_posttype_list a {
  color: #000000;
  text-decoration: none;
}
.dw_focus_widget_custom_posttype_list a:hover,
.dw_focus_widget_custom_posttype_list a:focus {
  color: #ee3224;
}
.color-blue .dw_focus_widget_custom_posttype_list a:hover,
.color-blue .dw_focus_widget_custom_posttype_list a:focus {
  color: #0077cc;
}
.color-orange .dw_focus_widget_custom_posttype_list a:hover,
.color-orange .dw_focus_widget_custom_posttype_list a:focus {
  color: #ff9500;
}
.color-cyan .dw_focus_widget_custom_posttype_list a:hover,
.color-cyan .dw_focus_widget_custom_posttype_list a:focus {
  color: #009999;
}
.color-green .dw_focus_widget_custom_posttype_list a:hover,
.color-green .dw_focus_widget_custom_posttype_list a:focus {
  color: #00a53c;
}
.color-violet .dw_focus_widget_custom_posttype_list a:hover,
.color-violet .dw_focus_widget_custom_posttype_list a:focus {
  color: #c64f9d;
}
.color-yellow .dw_focus_widget_custom_posttype_list a:hover,
.color-yellow .dw_focus_widget_custom_posttype_list a:focus {
  color: #ff9500;
}
.dw_focus_widget_custom_posttype_list .color-blue a:hover,
.dw_focus_widget_custom_posttype_list .color-blue a:focus {
  color: #0077cc;
}
.dw_focus_widget_custom_posttype_list .color-orange a:hover,
.dw_focus_widget_custom_posttype_list .color-orange a:focus {
  color: #ff9500;
}
.dw_focus_widget_custom_posttype_list .color-cyan a:hover,
.dw_focus_widget_custom_posttype_list .color-cyan a:focus {
  color: #009999;
}
.dw_focus_widget_custom_posttype_list .color-green a:hover,
.dw_focus_widget_custom_posttype_list .color-green a:focus {
  color: #00a53c;
}
.dw_focus_widget_custom_posttype_list .color-violet a:hover,
.dw_focus_widget_custom_posttype_list .color-violet a:focus {
  color: #c64f9d;
}
.dw_focus_widget_custom_posttype_list .color-yellow a:hover,
.dw_focus_widget_custom_posttype_list .color-yellow a:focus {
  color: #ff9500;
}

Hope this help ! , if you have problems occur when trying to implement this , please let us know.

Powered by DW Question & Answer Pro