Wordpress: How to create a dropdown for users to sort posts by date, title, or custom field
I have a site for a client and one page displays posts from a custom post type. Its a record label and I created a 开发者_如何学编程custom post type for "releases".
A page displays all releases. I need to create a dropdown menu though that allows people to alter the display of the posts by Title, Date (post's date serves as "release date"), or Artist (custom field).
How would I code the dropdown?
You would create a <select>
input directly in your template for that page, assuming you have one (create one if you don't). You can then either use a <form action="" method="POST">
wrapping the dropdown or some JavaScript to submit the form to itself (that is, action="?sort"
or similar to submit back into your template). Both of those are fairly simple to implement, but just say so if you're unsure.
Then, in the same template file, before your WP_Query
or query_posts
(or get_posts
, etc), grab the POST variable: $_POST['dropdown_name']
and use an if elseif else
or switch()
statement to create your post query depending on that POST variable.
This is the easiest approach. Not sure what your level of experience is, say so if you got lost.
精彩评论