在wordpress中显示页面上的多个类别帖子

时间:2011-12-02 12:15:36

标签: wordpress select post categories

我想显示多个类别的帖子。我使用以下代码创建了所有类别的MULTISELECT下拉列表:

<form method="get" action="<?php bloginfo('home');?>" >
    <select name="cat" multiple> 
     <option value=""><?php echo esc_attr(__('Please Select Your Choice')); ?></option> 
     <?php 
      $categories=  get_categories('show_count=0&orderby=name&echo=0&hierarchical=true&depth=1&taxonomy=category&exclude=1'); 
      foreach ($categories as $category) {

        $option = "<option value=$category->term_id>";
        $option .= ucfirst($category->cat_name);
        $option .= '</option>';
        echo $option;
      }
     ?>
    </select>
<input type="submit" id="searchsubmit" value="Search" name="submit"/>   
</form>

每当我选择多个类别时,我都会在网址中看到所有cat ID:http://abcd.com/?cat=7&cat=8&cat=9但它只显示使用archieve.php文件的最后一个类别帖子。我想在页面上显示所有选定的类别帖子。

提前致谢。

2 个答案:

答案 0 :(得分:4)

<?php 

$categoryids = array(3,6,8,19);

$args = array(
'numberposts'     => 20,
'category__in'    => $categoryids,
'orderby'         => 'post_date',
'order'           => 'DESC',
'post_type'       => 'photo',
'post_status'     => 'publish' );

$posts_array = get_posts( $args );

?>



<!-- The carousel -->
<div class="scrolimageview">
    <div class="prvdiv"><a href="javascript: void(0)" id="gallery-prev"><img src="<?php bloginfo( 'template_url' ); ?>/images/prev.png" width="40"/></a></div>
    <div id="gallery-wrap">
        <ul style="width: 2068px; left: -188px;" id="gallery">
            <?php foreach ($posts_array as $postd): ?>
            <?php
            $customd = get_post_custom($postd->ID);
            $attached_photo_filed = $customd["attached_photo"][0];
            $attached_photod = str_replace($postd->ID . '_', $postd->ID . '_thumb_',$attached_photo_filed);
            $imaged = $upload_dir['baseurl'] . '/photos/' . $attached_photod;
            ?>
            <li><a href="<?php get_bloginfo('url')?>/photo-details?id=<?php echo $postd->ID; ?>"><img src="<?php echo $imaged; ?>" alt=""></a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div class="prvdiv1" style="float:right"><a href="javascript: void(0)" id="gallery-next"><img src="<?php bloginfo( 'template_url' ); ?>/images/next.png"/></a></div>
</div>

此代码来自我的一个正在运行的项目。所以它100%正常工作,如果您遇到任何麻烦,请告诉我。

答案 1 :(得分:2)

<?php 
if(isset($_POST['submit']))
{
    $categories = $_POST['category'];

    print_r($categories);   
}
?>
<form action="" method="post">
    <select name="category[]" size="5" multiple="multiple">
        <option value="1">Red</option>
        <option value="2">Green</option>
        <option value="3">Blue</option>
        <option value="4">Yellow</option>
        <option value="5">Orange</option>
        <option value="6">Purple</option>
        <option value="7">Megento</option>
        <option value="8">Green</option>
        <option value="9">White</option>
        <option value="10">Black</option>
    </select>
    <input type="submit" name="submit" value="Submit" />
</form>

注意在这里使用[] -