在表单提交中将条件存储在数组中并返回相应的帖子 [英] Storing terms in post array and returning corresponding posts on form submit

查看:134
本文介绍了在表单提交中将条件存储在数组中并返回相应的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分类术语列表,复选框以表单上的数组形式发布,并提交给名为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')));?>方法= POST >

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

< input type =checkboxname =terms []value =<?php echo $ term-> name?> /><?php echo $ term->
名称?> < br />

<?php}?>

< input type =submitvalue =Submit/>
< / form>

当我在Post List页面(post-list.php)中打印数组时,返回的条款,所以我可以确认它是否有效

 <?php 
/ *
模板名称:帖子列表
* /
?>

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

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

如何将返回的条款数组与自定义帖子类型的存储条款进行比较,然后根据选择和提交的条款返回帖子?谢谢你的帮助。 / exclude-posts-containing-a-special-tagrel =nofollow> http://wordpress.org/support/topic/exclude-posts-containing-a-particular-tag

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

所以你需要传递你在邮件列表模板上使用的逗号分隔术语。你可以使用implode函数来获得像这样的逗号分隔列表。

  $ var ='.implode(',' ,$ _ POST [ '条款']) '。 
echo $ var; // OUTPUT - 'term1','term2','term3','term4'


I have a list of taxonomy terms with checkboxes that post as an array on form submit to a page template called Post List. My goal here is to list taxonomy terms with checkboxes, then on form submit, display the corresponding posts that have the checked terms assigned to them e.g.

<?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>

When I print the array within Post List page (post-list.php), I do get the returned terms, so I can confirm that it works

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

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

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

How could I compare the returned terms array to the stored terms of the custom post type, and return posts based off of which terms were selected and submitted? Thanks for your help.

解决方案

Read the line of stiwdio's question from this link http://wordpress.org/support/topic/exclude-posts-containing-a-particular-tag in which he wrote

$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' 

这篇关于在表单提交中将条件存储在数组中并返回相应的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆