在页面内显示存档(在下拉列表中)和类别(在下拉列表中)

时间:2012-07-15 15:43:19

标签: php templates drop-down-menu archive

我有一个问题:

我有一个存档模板页面,在那里我想显示两个下拉菜单:存档和类别下拉菜单,当我点击时,例如存档11月,所以所有项目将从11月侧面显示,它也有效...但是,如果您选择菜单项,则将显示所有项目,而不仅仅是此类别的帖子。

以下是代码:

在archives.php中出现了这段代码(这里都是正确的......我想):

<div id="archive-browser">
    <div>
        <h4>Month</h4>
        <select id="month-choice">
            <option val="no-choice"> &mdash; </option>
            <?php wp_get_archives(array(
                'type'    => 'monthly',
                'format'  => 'option'
            )); ?>
        </select>
    </div>
    <div>
        <h4>Category</h4>
        <?php 
            wp_dropdown_categories('show_option_none= -- ');?> 
    </div>
</div>
<div id="archive-wrapper">
<div id="archive-pot">
</div></div>     

在另一个名为Archives-getter.php的模板中出现了这段代码,这里出现了错误:

<?php
    /*
        Template Name: Archives Getter
    */
    $year = htmlspecialchars(trim($_POST));
    $month = htmlspecialchars(trim($_POST));
    $cat = htmlspecialchars(trim($_POST));
    $querystring = "year=$year&monthnum=$month&cat=$cat&posts_per_page=-1";
    query_posts($querystring); 
?>
<?php if (($year == '') && ($month == '') && ($cat == '-1')) { ?>
    <table id="archives-table"><tr><td style='text-align: center; font-size: 15px; padding: 5px;'>Please choose from above.</td></tr></table>
<?php } else { ?>
    <table id="archives-table">
        <?php    
            if (have_posts()) : while (have_posts()) : the_post(); ?>
                <tr>
                    <td><img src="<?php echo get_post_meta($post->ID, 'PostThumb', true); ?>" alt="" style="width: 35px;" /></td>
                    <td><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></td>
                    <td><?php comments_popup_link(' ', '1 Comment', '% Comments'); ?></td>
                    <td><?php the_date('m/j/Y'); ?></td>
                </tr>
        <?php 
            endwhile; else:
                echo "<tr><td style='text-align: center; font-size: 15px; padding: 5px;'>Nothing found.</td></tr>";
            endif; 
        ?>
    </table>
<?php } ?>

这两个连接在一起,所以当我运行档案模板时,它会从getter调用数据。

我认为这个错误在某个地方被查询字符串...只有类别下拉列表显示的不是帖子。谢谢你的帮助。


谢谢,但归档下拉列表工作正常...但是当我点击类别XY时,类别下拉列表不显示她的类别上的帖子。这是Jquery代码,因为它连接了this和post字段名称:

我更改了帖子字段名称:

$year = htmlspecialchars(trim($_POST['year_y']));
    $month = htmlspecialchars(trim($_POST['month_m']));
    $cat = htmlspecialchars(trim($_POST['cat_c']));



jQuery(function() {

    jQuery("#archive-wrapper").height(jQuery("#archive-pot").height());

    jQuery("#month-choice").change(function() {

        // Update category choice dynamically

        // This is much harder, needs another intermediary getter

    });

    jQuery("#archive-browser select").change(function() {

        jQuery("#archive-pot")
            .empty()
            .html("<div style='text-align: center; padding: 30px;'><img src='' /></div>");



        var dateArray = jQuery("#month-choice").val().split("/");

        var y = dateArray[3];

        var m = dateArray[4];

        var c = jQuery("#cat").val();



        jQuery.ajax({



            url: "/archive-getter/", 

            dataType: "html",

            type: "POST",

            data: ({

                "year_y": y,

                "month_m" : m,

                "cat_c" : c


            }),

            success: function(data) {

                jQuery("#archive-pot").html(data);



                jQuery("#archive-wrapper").animate({

                    height: jQuery("#archives-table tr").length * 50

                });



            }



        });



    });



});

1 个答案:

答案 0 :(得分:0)

您需要在此处指定帖子字段名称:

$year = htmlspecialchars(trim($_POST));
$month = htmlspecialchars(trim($_POST));
$cat = htmlspecialchars(trim($_POST));

应该是这样的(名称取决于您的表单字段

$year = htmlspecialchars(trim($_POST['year_y']));
$month = htmlspecialchars(trim($_POST['month_m']));
$cat = htmlspecialchars(trim($_POST['cat_c']));

这种表单输入应该有名称:

<select id="month-choice">
像这样

<select id="month-choice" name="month">