display post by $_POST[date] in wordpress

拈花ヽ惹草 提交于 2020-01-06 15:00:30

问题


I really need help for this problem in wordpress about displaying and filtering datas. I have html in this form, so first i have two drop down list(DDL), which should be populated related to the date of the posts. First DDL it should contains all the years of the posts, and second DDL should contains all the months of the posts.

<select name="month">
    <option value="2001"> 2001</option>                                                                        
    <option value="2002"> 2002</option>                                                                        
    <option value="2003"> 2003</option>                                                                      
    <option value="2004"> 2004</option>                                                                          
    <option value="2005"> 2005</option>                                                                      
    <option value="2006"> 2006</option>                                                                        
    <option value="2008"> 2008</option>                                                                            
   <option value="2000"> 2000</option>                                             
</select>



<select name="month">
        <option value="January"> 01 </option>                                                                        
        <option value="Feburary"> 02 </option>                                                                        
        <option value="March"> 03 </option>                                                                      
        <option value="April"> 04 </option>                                                                          
        <option value="June"> 06 </option>                                                                      
        <option value="July"> 07 </option>                                                                        
        <option value="August"> 08 </option>                                                                            
       <option value="December"> 12 </option>                                             
    </select>

Now I need to display the posts by selecting in DDL, month and year. I'm using while loop to display all the posts. Something like this:

<?php
    $posts_per_row = 3;
    $posts_per_page = 6;
    $category_name = 'music';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
         'category_name' => $category_name,
         'posts_per_page' => $posts_per_page,
         'paged' => $paged,
    );
    query_posts($args);
    if (have_posts()) {
       while (have_posts()) {
       the_post();
       ....
    }
} else {
    ....                    
}
?>          

The template also has the pagination function in functions.php


回答1:


You can display posts based on the posted month/year by using the year and monthnum arguments for query_posts().

// you have "month" in your HTML instead of "year"
$year = $_POST['year'];   
$month = $_POST['month']; 
$args = array(
     'category_name' => $category_name,
     'posts_per_page' => $posts_per_page,
     'year' => $year,
     'monthnum' => $month,
     'paged' => $paged,
);



回答2:


You can use year and monthnum attribute in query arguments.

Like this:

$posts_per_row = 3;
$posts_per_page = 6;
$category_name = 'music';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
     'category_name' => $category_name,
     'posts_per_page' => $posts_per_page,
     'paged' => $paged
);
if(isset($_POST['year'])){
   $args['year'] = $_POST['year'];
}
if(isset($_POST['month'])){
   $args['monthnum']= $_POST['month'];
}

query_posts($args);
if (have_posts()) {
   while (have_posts()) {
   the_post();
   ....
}

And your values in select "month" change to:

<select name="month">
        <option value="01"> 01 </option>                                                                        
        <option value="02"> 02 </option>                                                                                                                                            
         ...                                                                           
        <option value="12"> 12 </option>                                             
</select>

More info here: http://codex.wordpress.org/Function_Reference/query_posts



来源:https://stackoverflow.com/questions/24845456/display-post-by-postdate-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!