如何让滑块显示4最近的新闻和新闻从头开始而不是从4开始

时间:2014-05-20 作者:user51905

我对wordpress有点陌生,我想知道如何让我的wordpress站点在这个滑块中显示最近的4条新闻,以及下面的所有新闻:

    class s_naujienos extends WP_Widget {

    function s_naujienos() {
        $widget_ops = array(\'classname\' => \'s_naujienos\', \'description\' => \'Naujienų slideris\');
        $this->WP_Widget(\'s_naujienos\', \'Naujienų slideris\', $widget_ops);
    }

    function form($instance) {
        $instance = wp_parse_args((array) $instance, array(\'title\' => \'\'));
        $title = $instance[\'title\'];
        ?><p><label for="<?php echo $this->get_field_id(\'title\'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p><?php
            }

            function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance[\'title\'] = $new_instance[\'title\'];
                return $instance;
            }

            function widget($args, $instance) {
                extract($args, EXTR_SKIP);
                echo $before_widget;
                $title = empty($instance[\'title\']) ? \' \' : apply_filters(\'widget_title\', $instance[\'title\']);
                if (!empty($title)) {
                    echo $before_title . $title . $after_title;
                }
                if (have_posts()) {
                    //$count_posts = wp_count_posts();
                    //$published_posts = $count_posts->publish;
                    $naujienu_sk = 4;
                    ?>
            <script>
                var newsslider = new pro3bSlider(\'#snauj\', 500, <?php echo $naujienu_sk; ?>, true, 5000, false);
            </script>
            <style type="text/css">
                .snauj_left { position: absolute; top: 0; bottom: 0; margin: auto; z-index: 2; margin-left: 15px; opacity:0.4; filter:alpha(opacity=40); }
                .snauj_left:hover { position: absolute; top: 0; bottom: 0; margin: auto; z-index: 2; margin-left: 15px; opacity:1.0; filter:alpha(opacity=100); }           
                .snauj_right { position: absolute; top: 0; bottom: 0; margin: auto; z-index: 2; margin-right: 15px; right: 0; opacity:0.4; filter:alpha(opacity=40); }          
                .snauj_right:hover { position: absolute; top: 0; bottom: 0; margin: auto; z-index: 2; margin-right: 15px; right: 0; opacity:1.0; filter:alpha(opacity=100); }   
                .snauj_title { text-align: center; opacity:0.8; filter:alpha(opacity=80); font-size: 18px; font-weight: 400; background: #000; font-family: MyriadPro-Regular,\'Myriad Pro Regular\',MyriadPro,\'Myriad Pro\'; position:absolute; z-index:2; bottom:0; right:0; width: 500px; height: 40px; margin-right: 0px; margin-bottom: 0px; text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000; }
            </style>
            <link rel="stylesheet" href="./css/foundation.css" type="text/css" media="screen"/>
            <div class="large-8 small-16 collumns" style=\'min-width: 500px; min-height: 282px; position: relative; overflow: hidden; border-right: 1px solid #DDDCDC;\'>
                <img onclick=\'newsslider.left()\' class=\'snauj_left\' src=\'<?php echo get_template_directory_uri(); ?>/img/nauj_left.png\'/>
                <img onclick=\'newsslider.right()\' class=\'snauj_right\' src=\'<?php echo get_template_directory_uri(); ?>/img/nauj_right.png\'/>            
                <div style=\'width: <?php echo $naujienu_sk * 500; ?>px; height: 282px;\'>
                    <?php
                    $counter = 0;
                    $argum = array(\'numberposts\' => $naujienu_sk);
                    $recent_posts = wp_get_recent_posts($argum);
                    foreach ($recent_posts as $recent) {
                        the_post();
                        $counter++;
                        ?>
                        <div id=\'snauj<?php echo $counter; ?>\' style=\'width: 500px; height: 282px; display: inline-block; position: relative; color: #fff; float: left; background: #000;\'>
                            <div class=\'snauj_title\'><?php the_title(); ?></div>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php
//add_image_size(\'my_image_media\', 600, 338);
//add_image_size( \'category-thumb\', 300, 9999 );
//set_post_thumbnail_size( 500, 282 );

                                the_post_thumbnail(\'large\', array(\'class\' => \'wp-caption\')); // Declare pixel size you need inside the array the_post_thumbnail(\'newsslider-size\');  
                                ?>
                            </a>
                        </div>
                        <?php
                    }
                    ?></div></div><?php
        }

//echo get_bloginfo("template_url"); 

        echo $after_widget;
    }

}

add_action(\'widgets_init\', create_function(\'\', \'return register_widget("s_naujienos");\'));

2 个回复
SO网友:Nicolai Grossherr

我想你只是错过了一个wp_reset_postdata() 每次循环后打电话。看看get_posts(), 所使用的wp_get_recent_posts(), 查看用法示例。

除此之外,您不需要if (have_posts())the_post(); 在小部件代码中。但应该利用setup_postdata().

<小时>

Edit:

示例:

$argum = array( 
    \'numberposts\' => $naujienu_sk
);
$recent_posts = wp_get_recent_posts( $argum );
foreach ( $recent_posts as $post ) {
    setup_postdata( $post );
    // code
}
wp_reset_postdata();

SO网友:user51905

我错过的不是wp_reset_postdata(), 但是rewind_posts();, 它完全起作用了。

结束

相关推荐