EDIT: Up to date code, only thing left to fix is the subcategory query.
我正在努力让我的公文包页面的过滤器工作。我对WordPress有很好的了解,似乎不能。。。
目标:
创建一个仅包含作为指定类别子类别的类别的筛选器。
使用子类别过滤器中的所选选项,将所选过滤器的相关帖子Ajax显示到视图中。
因此,请参见相关代码:
成功从“我的投资组合”类别中提取帖子的“我的投资组合”页面:
<div class="portfolio-filters">
<?php
$filtercategory = get_template_directory() . "/template-parts/category-filter.php";
include_once($filtercategory);
?>
</div>
<div class="portfolio-pieces">
<div class="portfolio-pieces-inner">
<div id="response">
<!-- DEFAULT PORTFOLIO PAGE DISPLAY -->
<?php
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'portfolio\',
\'posts_per_page\' => \'-1\',
\'orderby\' => \'post_date\',
\'order\' => \'DESC\'
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="portfolio-piece" <?php if(has_post_thumbnail()) : ?>style="background-image: url(<?php echo get_the_post_thumbnail_url(); ?>);"<?php endif; ?>>
<a href="<?php the_permalink(); ?>" class="box-link" target="_blank"></a>
<div class="portfolio-piece-hover">
</div>
<div class="portfolio-piece-inner">
<h4><?php the_title(); ?></h4>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>
在上面的代码片段中,我调用了我的过滤器文件。创建响应区域并载入完整的公文包片段列表。
我的类别筛选器文件如下所示:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<?php
$args = array(
\'taxonomy\' => \'category\',
\'category_name\' => \'portfolio-category\',
\'orderby\' => \'name\',
\'order\' => \'DESC\',
\'parent\' => 0
);
if( $terms = get_terms( $args ) ) :
echo \'<select name="categoryfilter"><option>Select category...</option>\';
foreach ( $terms as $term ) :
echo \'<option value="\' . $term->term_id . \'">\' . $term->name . \'</option>\';
endforeach;
echo \'</select>\';
endif;
?>
<button>Apply filters</button>
<input type="hidden" name="action" value="customfilter">
</form>
<script>
jQuery(function($) {
$(\'#filter\').submit(function(){
var filter = $(\'#filter\');
$.ajax({
url:filter.attr(\'action\'),
data:filter.serialize(), // form data
type:filter.attr(\'method\'), // POST
beforeSend:function(xhr){
filter.find(\'button\').text(\'Applying Filters...\');
},
success:function(data){
filter.find(\'button\').text(\'Apply filters\');
$(\'#response\').html(data);
}
});
return false;
});
});
</script>
其中,上面的代码片段“试图”创建一个具有指向管理ajax的操作的表单。php文件在我的wp admin文件夹中(就在那里)。
然后循环使用我的get\\u terms参数,将我想要的子类别显示在下拉列表中,并使用apply按钮。
最后一个代码段处理了这一切。根据其状态更改按钮文本,并将my response div作为返回位置。
我的函数文件如下:
/* Filter Post Results */
function catfilter_filter_function(){
$args = array(
\'orderby\' => \'date\', // we will sort posts by date
\'order\' => $_POST[\'date\'] // ASC or DESC
);
// for taxonomies / categories
if( isset( $_POST[\'categoryfilter\'] ) )
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'id\',
\'terms\' => $_POST[\'categoryfilter\']
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo "<div class=\\"portfolio-piece\\" style=\\"background-image: url(" . get_the_post_thumbnail_url() . ");\\">";
echo "<a href=\\"" . the_permalink() . "\\" class=\\"box-link\\" target=\\"_blank\\"></a>";
echo "<div class=\\"portfolio-piece-hover\\">";
echo "</div>";
echo "<div class=\\"portfolio-piece-inner\\">";
echo "<h4>" . the_title() . "</h4>";
echo "</div>";
echo "</div>";
endwhile;
wp_reset_postdata();
else :
echo \'No posts found\';
endif;
die();
}
add_action(\'wp_ajax_customfilter\', \'catfilter_filter_function\');
add_action(\'wp_ajax_nopriv_customfilter\', \'catfilter_filter_function\');
/* END Filter Post Results */
函数文件脚本工作时,会将过滤器中声明的帖子拉过。
Can someone please help me narrow down my category filter to only have the relevant sub-categories in it? - They are sub-categories of the \'Portfolio Categories\' category that has the slug \'portfolio-category\'
我能够显示类别的完整列表,或者只是基本父类别,而不是子类别。。。
我的类别设置如下:
— Portfolio Piece
— — Portfolio Category
— — — Automation
— — — Design
— — — Digital
— — — Exhibitions
— — — PR / Social
— — — Strategy
— — — Tech Insights
— — Sector
— — — Construction
— — — Manufacturing
— — — Oil & Gas
— — — Science
我从未尝试过50多篇不同的文章,而且我一生都无法缩小这个列表。
非常感谢!