Fatih Tercan
asked 5 years ago

We have headline widget older version on focus. How can i add widget like this http://prntscr.com/8896z1

2 Answers
DominicStaff
answered 5 years ago

Hi,
Please send me username & password of your site, I will check and help you resolve it.
Regards,

DominicStaff
answered 5 years ago

To resolve this issue, you can do as the following:

  • Add the following code to the functions.php file:
    require get_template_directory() . '/inc/widgets/dw-focus-latest-headlines.php';

  • Add the following code to the style.css file:
.news-headlines-wrap.headlines a {
    color: #000;
    font-size: 13px;
}
.news-headlines-wrap.headlines a:hover {
    color: #ee3224;
}
.post-date-headlines {
    color: #666;
    font-size: 13px;
}
.news-headlines-wrap.headlines ul {
    list-style: square;
}
  • Go to your theme folder wp-content\themes\dw-focus\inc\widgets, inside your widget folder, create a new file: dw-focus-latest-headlines.php.
    Now, open up the new file in notepad or any code editor, and copy/paste the following code:
<?php  
add_action( 'widgets_init', 'dw_focus_latest_headlines_init' );
function dw_focus_latest_headlines_init() {
    register_widget( 'DW_Focus_latest_headlines' );
}

class dw_focus_latest_headlines extends WP_Widget {

    function __construct() {
       $widget_ops = array( 'classname' => 'dw_focus_headlines', 'description' => 'Show Your Posts as News Ticker.' );
        parent::__construct( 'dw_focus_news_headlines', 'DW Focus: Headlines', $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') );
    }

    function widget($args, $instance) {
        $cache = wp_cache_get('dw_focus_widget_query_posts', '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;
        $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;
        $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,
                );
        if ( $post_format ) {
                $query['post_format'] = 'post-format-'.$post_format;
        }

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

            if ( 0 != $cat_id ) {
                $query['cat'] = $cat_id;
            }
        }

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

        if ( $r->have_posts() ) :
?>
        <?php echo $args['before_widget']; ?>
        <?php
        if ( $title ) {
            echo $args['before_title']. wp_kses_post( $title ) . $args['after_title'];
        }
        ?>
        <div class="news-headlines-wrap headlines" data-interval="4000">
            <ul>
            <?php while ( $r->have_posts() ) : $r->the_post(); ?>
                <li>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php if ( $show_date ) : ?>
                <?php echo ' - ' ?>
                    <span class="post-date-headlines"><?php the_date(); ?></span>
                <?php endif; ?>
                </li>
            <?php endwhile; ?>
            </ul>
        </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_query_posts', $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['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;

        $this->flush_widget_cache();

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

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

    public function form( $instance ) {
        $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $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_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : 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( '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&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>
<?php
    }
}

Hope this helps !

wptrinfo
replied 5 years ago

thanks. Evrything is ok

Powered by DW Question & Answer Pro