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


Display archive (in dropdown) and category (in dropdown) inside a page

我有一个问题:

我有一个存档模板页面,在那里我想显示两个下拉菜单:存档和类别下拉菜单,当我点击例如存档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中的数据。

我认为错误在querystring的某个地方。。。只有类别下拉列表不显示帖子。谢谢你的帮助。


谢谢,但归档下拉菜单运行良好。。。然而,当我点击类别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
                });

            }

        });

    });

});

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

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