大家好,我正在与Timber一起从事wordpress的工作(以支持树枝)。我创建了一个名为gift的自定义帖子类型和一个名为gift_type的自定义分类法。我还创建了一个小部件以按点过滤gitfs(所有礼物都有一个点定制字段)。我已经为此自定义类型创建了存档页面:
<?php
get_header('shop');// Get content width and sidebar position
$content_class = "content-class";
?>
<div class="site-content <?php echo esc_attr( $content_class ); ?>" role="main" id="gifts_main_container">
<?php
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$args = array(
'posts_per_page' => get_field('gifts_per_page','option'),'post_type' => 'gift','paged' => $paged,'tax_query' => array(
array(
'taxonomy' => 'gift_type','field' => 'term_id','terms' => get_queried_object_id(),'include_children' => false
)
)
);
require_once(dirname(__FILE__).'/partials/partial-archive-gifts.php');
?>
</div>
<?php
get_sidebar();
dynamic_sidebar( 'mg-widgets-area' );
get_footer();
?>
我制作了partial-archive-gifts.php,以在过滤器小部件和标准存档页面之间共享模板。这是内容
<?php
Timber::$dirname = '../templates';
$context = Timber::context();
$context['posts'] = Timber::get_posts($args);
$context['pagination'] = Timber::get_pagination($context['posts']);
if(isset($min) && $min !== null) {
$context['min'] = $min;
}
if(isset($max) && $max !== null) {
$context['max'] = $max;
}
if(isset($show_filters_controls) && $show_filters_controls !== null) {
$context['show_filters_controls'] = $show_filters_controls;
}
$context['strings'] = array(
'clear_filters' => __('Clear filters','MG_MARKETS'),'add_to_cart' => __('Add to Cart','points' => __('Points','add_to_wishlist' => __('Add to wishlist','MG_MARKETS')
);
Timber::render( 'archive.twig',$context );
?>
在小部件上,我创建了一个函数,该函数通过前端的ajax调用来调用。这是功能
function filter_gifts() {
$tax = array(intval($_REQUEST['term_id']));
ob_start();
global $posts,$paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$args =array(
'posts_per_page' => get_field('gifts_per_page','meta_query' => array(
'relation' => 'AND',array(
'key' => 'n_points_to_withdraw_gift','value' => $_REQUEST['min'],'type' => 'NUMERIC','compare' => '>='
),'value' => $_REQUEST['max'],'compare' => '<='
)
),'tax_query' => array(
array(
'taxonomy' => 'gift_type','terms' => $tax,'include_children' => false
)
)
);
$template_path = MG_MARKETS_giftS_TEMPLATE.'/partials/partial-archive-gifts.php';
$min = $_REQUEST['min'];
$max = $_REQUEST['max'];
$show_filters_controls = $_REQUEST['show_filters_controls'] === 'true' ? true : false;
include($template_path);
$content = ob_get_contents();
ob_end_clean();
echo $content;
wp_die();
}
当我第一次加载存档页面时,我可以使用(在我的partial-archive-gifts.php文件中)检索分页(并且可以导航到其他页面)
$context['pagination'] = Timber::get_pagination($context['posts']);
当我启动ajax调用时,分页没有总页数和空页数组,并且它在前端不呈现任何内容。 两种用法的帖子都在那里,它们是Timber \ Post对象。 我转储了分页数组,这是其在标准存档页面中的内容:
Array
(
[current] => 1
[total] => 2
[pages] => Array
(
[0] => Array
(
[class] => page-number page-numbers current
[title] => 1
[text] => 1
[name] => 1
[current] => 1
)
[1] => Array
(
[class] => page-number page-numbers
[link] => http://localhost:8000/premi/cucina/page/2/
[title] => 2
[name] => 2
[current] =>
)
)
[next] => Array
(
[link] => http://localhost:8000/premi/cucina/page/2/
[class] => page-numbers next
)
[prev] =>
)
这里是调用ajax过滤时的分页数组
Array
(
[current] => 1
[total] => 0
[pages] => Array
(
)
[next] =>
[prev] =>
)
希望这有助于更好地了解我的问题。
在此先感谢对大家有帮助的人