最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Correct way to use a form to to filter custom posts by taxonomy terms?

programmeradmin1浏览0评论

This is a continuation of this post.

I've built my form and managed to get the correct taxonomy terms listed as a dropdown, but I'm wondering what action I should be using in my form and how I can recall the options selected in the same form on the results page?

This is the code I've written so far which sets query_posts() so that it fetches the custom posts 'ftevent' that have the taxonomy terms 'type', 'period' and 'duration' set to something the user chooses:

Pastie link

And this is the function osu_list_terms() that you'll see in that code which tries to recall the options selected in each dropdown:

function osu_list_terms($osu_tax, $osu_recall) {
    // list terms in fttype taxonomy
    $taxonomy = $osu_tax;
    $tax_terms = get_terms($taxonomy);
    foreach ($tax_terms as $tax_term) {
        echo '<option' . $osu_recall . '>' . $tax_term->name . '</option>';
    }
}

Now, this is a work in progress, so I'm sure there are a few other problems I'll come up against (such as ordering the custom posts by the custom field 'StartEventDate'), but at this point, I'm trying to work out how Wordpress works with something like this form.

I'm used to using

<?php echo $_SERVER['PHP_SELF']; ?>

for forms so that the form submits to the same page. I was hoping I could do the same with this, but Wordpress redirects to a URL like this and uses the archive.php template instead of the current page template I've inserted this form into:

/?fttype=Group+walks&ftperiod=Weekday&ftduration=Half-day&showfilter=true

Is there a 'correct' way to do this? I'm a little confused as to how I can style a template for the search results as well as the results are displayed using the archive page (which is also used by tag-cloud results).

Any tips you can offer?

Thanks,

osu

This is a continuation of this post.

I've built my form and managed to get the correct taxonomy terms listed as a dropdown, but I'm wondering what action I should be using in my form and how I can recall the options selected in the same form on the results page?

This is the code I've written so far which sets query_posts() so that it fetches the custom posts 'ftevent' that have the taxonomy terms 'type', 'period' and 'duration' set to something the user chooses:

Pastie link

And this is the function osu_list_terms() that you'll see in that code which tries to recall the options selected in each dropdown:

function osu_list_terms($osu_tax, $osu_recall) {
    // list terms in fttype taxonomy
    $taxonomy = $osu_tax;
    $tax_terms = get_terms($taxonomy);
    foreach ($tax_terms as $tax_term) {
        echo '<option' . $osu_recall . '>' . $tax_term->name . '</option>';
    }
}

Now, this is a work in progress, so I'm sure there are a few other problems I'll come up against (such as ordering the custom posts by the custom field 'StartEventDate'), but at this point, I'm trying to work out how Wordpress works with something like this form.

I'm used to using

<?php echo $_SERVER['PHP_SELF']; ?>

for forms so that the form submits to the same page. I was hoping I could do the same with this, but Wordpress redirects to a URL like this and uses the archive.php template instead of the current page template I've inserted this form into:

/?fttype=Group+walks&ftperiod=Weekday&ftduration=Half-day&showfilter=true

Is there a 'correct' way to do this? I'm a little confused as to how I can style a template for the search results as well as the results are displayed using the archive page (which is also used by tag-cloud results).

Any tips you can offer?

Thanks,

osu

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 16, 2011 at 18:58 OsuOsu 1,4526 gold badges25 silver badges44 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If you leave the action attribute empty, the form will post back to the current URL.

However, if your method attribute is get, the form will post back to current URL, minus the current query string.

If you want to keep the current query string, you can use hidden inputs to replicate the current parameters.

And never trust PHP_SELF!

发布评论

评论列表(0)

  1. 暂无评论