添加不使用GET_POST的筛选器

时间:2013-09-12 作者:vulcan raven

在调整“Bryans Childpage缩略图生成器”插件时,我将过滤器(带分页)应用于WP页面的get\\u帖子(post_type=page), 但这根本不是过滤。代码如下:

extract(shortcode_atts(array(
    \'order\'      => \'ASC\',
    \'orderby\'    => \'title\',
    \'id\'         => \'\',
    \'number\'     => -1,
    \'height\'    => \'100\',
    \'width\'     => \'\',
    \'size\'       => \'thumbnail\',
    \'style\'     => \'\',
    \'include\'    => \'\',
    \'pagetitle\'  => false,
    \'selector\'   => \'h2\',
    \'selector_height\' => \'30px\',
    \'selector_width\' => \'100%\',
    \'selector_padding\' => \'\',
    \'showimages\' => true,
    \'text_align\' => \'left\',
    \'text_margin\' => \'\',
    \'exclude\'    => \'\',
    \'posts_per_page\'  => \'5\',
    \'page_number\'   => \'1\',
    \'filter_year\' => \'0\'
), $attr));

if( $id == \'\')
{
    $id = $post->ID;
}

$id = intval($id);

if($width != null)
{
    $width=\'width:\' . $width . \'px\';
}

if ($order == \'RAND\')
{
    $orderby = \'none\';
}

if($text_margin == \'\')
{
    $textmargin = ($height/2)-10;
    $textmargin .= "px 0 0 0";

} else 
{
    $textmargin = $text_margin;
}

$offsettext = ($height*3);

$offset = ($page_number - 1) * $posts_per_page;

$offset = $offset < 1 ? 0 : $offset;

function filter_where( $where = \'\' ) {

    if($filter_year > 0)
        $where .= " AND post_date >= \'" . $filter_year . "-01-01\' AND post_date < \'" . ($filter_year + 1) . "-01-01\'";

        // e.g. AND post_date >= \'2011-01-01\' AND post_date < \'2012-01-01\'

        return $where;
}

add_filter( \'posts_where\', \'filter_where\' );

$countposts = count(get_posts(array(
    \'suppress_filters\' => false,
    \'post_parent\' => $id,
    \'post_type\' => \'page\',
    \'post_status\' => \'publish\',
    \'numberposts\' => $number,
    \'exclude\' => $exclude,
    \'orderby\' => $orderby
)));


$childpages = & get_posts(array(
    \'suppress_filters\' => false,
    \'post_parent\' => $id,
    \'post_type\' => \'page\',
    \'post_status\' => \'publish\',
    \'numberposts\' => $number,
    \'exclude\' => $exclude,
    \'orderby\' => $orderby,
    \'order\' => $order,
    \'posts_per_page\' => $posts_per_page,
    \'offset\' => $offset
));

remove_filter( \'posts_where\', \'filter_where\' );

$totalpages = $countposts / $posts_per_page;

$totalpages = intval($totalpages) + ($totalpages > intval($totalpages) ? 1 : 0);
下面是调用此插件的短代码:

<?php echo do_shortcode("[gallery_childpages number=\'-1\' height=\'220\' width=\'295\' size=\'medium\' style=\'armthumbs\' include=\'\' pagetitle=\'1\' selector=\'H2\' showimages=\'0\' showimages=\'true\' exclude=\'1,33,22\' posts_per_page = \'9\' page_number=\'" . get_query_var(\'page\') . "\' filter_year=\'" . $_GET[\'fyear\'] . "\']"); ?>
分页正在工作,但筛选器不工作。。

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

根据Wordpress文档post_where filter 不会影响get_posts() 作用另请参见default usage of get_posts() 在那里你可以看到论点suppress_filters Deafolt设置为true。为了在由生成的查询中运行post\\u where filterget_posts() 必须将suppress\\u filters参数设置为false的函数:

get_posts( array( \'suppress_filters\' => FALSE ) );
因此,您的代码可能类似于:

$countposts = count(get_posts(array(
                   \'suppress_filters\' => false,
                   \'post_parent       => $id,
                   \'post_type\'        => \'page\',
                   \'post_status\'      => \'publish\',
                   \'numberposts\'      => $number,
                   \'exclude\'          => $exclude,
                   \'orderby\'          => $orderby
             )));
此外,您还有许多未初始化的变量,例如,$id、$number、$exclude、$orderby或$filter\\u year。

SO网友:birgire

看起来此条件从未填满:

if($filter_year > 0)
因为您尚未定义变量$filter_year.

结束

相关推荐

Pagination in category

Updated: I have 50 categories and i wants to show each 5 category on one page and next 5 on next page and next 5 on next page. and so on. with pagination.我在WordPress网站上有50个类别。我想在每一页上显示五个类别,用10篇文章分页。我已经尝试过了,但分页并没有产生效果。<?php // get all the categ