Paging in a sidebar mini loop

时间:2010-10-01 作者:edelwater

我切换到了另一个主题,并决定用其中的一些默认代码制作一个小部件,在自定义循环中显示我的美味帖子、推特帖子、su帖子和youtube视频(不包括主循环中的这些类别)。

但是现在。。。分页不再工作。

我制作了这个小部件:

// =============================== EDL Sidebar Posts Widget ======================================
class SidebarPosts extends WP_Widget {

function SidebarPosts() {
    parent::WP_Widget(false, $name = \'Sidebar Posts\'); 
}

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

    $title = apply_filters(\'widget_title\', $instance[\'title\']);
    echo $before_widget; if ( $title )
    echo $before_title . $title . $after_title; 
    $title = apply_filters(\'widget_title\', $instance[\'title\']);

    global $wp_query;

    $wp_query = new WP_Query();
    $wp_query->set(\'cat\', \'1172,14,867\');
    $wp_query->set(\'posts_per_page\', 30);
    $wp_query->set(\'offset\', $paged*30);  
    $posts = $wp_query->get_posts(); 
    if ($posts) : 
        foreach ($posts as $post) : the_post();
            if (in_category(14)) { 
                echo $this->createSideBarLine($post, \'http://populair.eu/wp-content/themes/pop/bookmark_info/com/delicious/f.png\', false, true, true);  
            } elseif (in_category(1172)) { 
                echo $this->createSideBarLine($post, \'/wp-content/themes/edl/inc/banners/twitter.gif\', false, true, true); 
            } elseif (in_category(867)) { 
                echo $this->createSideBarLine($post, \'http://populair.eu/wp-content/themes/pop/bookmark_info/com/stumbleupon/www/f.png\', true, true, true);
            } 
        endforeach; 
        echo "<p>" . _e(\'More on the Next Page!\') . "</p>";   
        wp_link_pages(array(\'next_or_number\'=>\'next\', \'previouspagelink\' => \' &laquo; \', \'nextpagelink\'=>\' &raquo;\'));

    else: 
        echo "<p>" . _e(\'Jump to the frontpage for the list\') . \'</p>\';
    endif; 

    echo $after_widget;
}

/** @see WP_Widget::update */
function update($new_instance, $old_instance) {    
    return $new_instance;
}

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

function get_the_content_with_formatting ($more_link_text = \'(more...)\', $stripteaser = 0, $more_file = \'\') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters(\'the_content\', $content);
    $content = str_replace(\']]>\', \']]&gt;\', $content);
    return $content;
}

//
// Shows a line in the LifeFeed / IssueFeed / BrainFeed sidebar
// widgets
//
function createSideBarLine($post,$strImage, $boolShowTitle, $boolShowExcerpt, $boolShowContent) {
    global $wp_query;

    $strBuffer="";
    $strTitle = strip_tags(get_the_title());
    $strPermalink = get_permalink($post->ID);
    $intId = $post->ID;
    $strContent = $this->get_the_content_with_formatting();
    $strContent = str_replace(\'<p>\', \'\', $strContent);
    $strEditPostLink = get_edit_post_link();

    // if content contains "YouTube" then use YouTube icon
    if (strstr($strContent, \'YouTube\')) {
        $strImage = \'http://populair.eu/wp-content/themes/pop/bookmark_info/com/youtube/www/f.png\';
    }

    $strBuffer.= \'<div class="tb-link" id="post-\'.  $intId . \'">\';
    $strBuffer.= \'<p><a href="\' . $strPermalink; 
    $strBuffer.= \'" rel="nofollow bookmark" title="Permanent Link to \';
    $strBuffer.= $title . \'">\';
    $strBuffer.= \'<img src="\'. $strImage . \'" align="left" \';
    $strBuffer.= \'width="12" style="border: 0px none;\';
    $strBuffer.= \'margin: 0px 10px 0px 0px; display: inline;">\';
    $strBuffer.= \'</a>\';
    if ($boolShowTitle) {
        $strBuffer.= \'<a href="\' . $strPermalink;
        $strBuffer.= \'" rel="nofollow bookmark"\'; 
        $strBuffer.= \'title="Permanent Link to \' . $strTitle . \'">\';
        $strBuffer.= $strTitle . "</a>";
    }
    if ($boolShowExcerpt) {
        $strBuffer.= $post->post_excerpt;
    }
    if ($boolShowContent) {
        $strBuffer.= $strContent;
    }  
    if (is_admin()) {
        $strBuffer.= \'<a href="\'. $strEditPostLink . \'" class="editpost">\';
        $strBuffer.= \' [edit]</a>\';
    }  
    $strBuffer.= "</p></div>";   
    return $strBuffer;   
}
}
我认为在上一个主题中,它可以从主循环中获取$paged或其他内容,现在它不再显示任何分页,所以它总是只显示前30个项目。

(见http://edward.de.leau.net/ 对于事物,忙于转换为swift主题)

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

试着去做global $paged 在小部件函数中使用变量之前。

结束

相关推荐

Sidebar slideshow widget

有人知道可以放在边栏上的高质量图像幻灯片小部件吗?