0

我有一个带有复选框的分类术语列表,这些复选框在表单上作为数组发布到名为 Post List 的页面模板。我的目标是列出带有复选框的分类术语,然后在提交表单时,显示已分配给它们的已检查术语的相应帖子,例如

<?php
$terms = get_terms("the_taxonomy");
$count = count($terms);
if ( $count > 0 ){?>
?>

<form action="<?php echo esc_url( get_permalink( get_page_by_title( 'Post List' ) ) ); ?>" method="post">

 <?php foreach ( $terms as $term ) {?>

 <input type="checkbox" name="terms[]" value="<?php echo $term->name ?>" /><?php echo $term->       
 name ?> <br />

 <?php }?>

 <input type="submit" value="Submit" />
</form>

当我在帖子列表页面 (post-list.php) 中打印数组时,我确实得到了返回的术语,所以我可以确认它是否有效

<?php
/*
Template Name: Post List 
*/
?>

<?php print_r($_POST['terms']);?>

<!---OUTPUT--->
Array ( [0] => term1 [1] => term2 [2] => term3 [3] => term4 )

如何将返回的术语数组与自定义帖子类型的存储术语进行比较,并根据选择和提交的术语返回帖子?谢谢你的帮助。

4

1 回答 1

1

从他写的这个链接http://wordpress.org/support/topic/exclude-posts- contains-a-particular-tag中阅读stiwdio的问题行

$term = get_term_by('slug','helen', 'post_tag');

so you need to pass comma separated terms like this which you are getting on post list template. You can use implode function to get comma separated list like this

$var = "'".implode("','",$_POST['terms'])."'";
echo $var; //OUTPUT - 'term1','term2','term3','term4' 
于 2012-08-20T10:21:31.260 回答