简体   繁体   中英

Submitting a Form to Display Specific WordPress Posts in a Page

I am currently displaying Posts that feature in my Team category using the code inside Page.php .

I'm also listing the sub-categories of 'Team' in my Sidebar.php , also listed below.

Is it possible in my Sidebar that I can have a form that lets the user choose which sub-categories to show posts from?

Visiting /?page_id=9&team=3,4 directly will show Posts from Team 3 & 4 , but I wonder if I can use a Form to let the user choose which posts to display.

Many thanks for any pointers with this.

Page.php :

<?php
    $type = 'team';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1,
      'cat'=> 3
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>

                <h2>Team Name: <?php the_title(); ?></h2>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
?>

Sidebar.php :

<?php
    $categories=  get_categories('child_of=2'); 
      foreach ($categories as $category) {
        $option = '<li><label>'.$category->cat_name.'</label><input type="checkbox" name="team" value="'.$category->term_id.'" style="float:right" /></li>';
        echo $option;
    }
?>

You can add a select box to the sidebar and change the content of a div in your page.php;

Like:

Page.php:

<div id="content"></div>

Subpage.php:

<select id="category">
    <option value="">Select your team</option>
    <option value="Team 1">Team 1</option>
    <option value="Team 2">Team 2</option>
    <option value="Team 3">Team 3</option>
</select>
<script>
$('#category').change(function() {
        if(this.value) $('#content').html(this.value);
});
 </script>  

Instead of adding a text to your div you could use an ajax request to get your post. Maybe read the post from RSS, see; http://wordpress.org/support/topic/rss-feed-of-post-category

Creating a plugin and widget will be a more stable solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM