我在Wordpress中加载页面时如何显示所有帖子?

时间:2019-05-24 17:41:05

标签: php wordpress

我有一个产品页面,我在下拉菜单中显示了帖子的类别,因此我想在加载产品页面时默认显示所有产品.ajax调用仅在从中选择一个类别时触发下拉菜单。是否有办法显示我加载页面后的所有帖子?

enter image description here

该下拉菜单有3个选项,分别是默认类别,食品和招待项目

-前端代码

<select class="event-dropdown"> 
    <option value=""><?php echo esc_attr(__('Select category')); ?></option> 
    <?php 
        $categories = get_categories(); 
        foreach ($categories as $category) {
            $option .= '<option value="'.$category->slug.'">';
            $option .= $category->cat_name;
            $option .= '</option>';
        }
        echo $option;
    ?>
</select>
</div>     
<div class="test"></div><!---test --->

<script>
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
$('.event-dropdown').on('change', function()
  {
      var cat_slug= this.value;
    $.ajax({
    type:"post",
    url: ajaxurl,
    data:{
      'action': 'my_action',
      'whatever': cat_slug,
    },
    success: function( data ) {
      $(".test").html(data);
    }
    })
  });
</script>

Backend-Funtions.php

     add_action( 'wp_ajax_my_action', 'my_action' );
      function my_action() {
        global $wpdb; // this is how you get access to the database

        $whatever = $_POST['whatever'];

          $args = array(      
                  'category_name' =>$whatever,  
                  'post_type' => 'ProductsNew',
                  'posts_per_page' => get_option('posts_per_page'),
                  'order'=>'ASC'
                        );
             $query = new WP_Query( $args );

                        // The Query
                        $the_query = new WP_Query( $args );




                        $total = $the_query->post_count;

                        if ( $i == 0 ) echo '<div class="row">';

                        // The Loop
                        if ( $the_query->have_posts() ) {

                          while ( $the_query->have_posts() ) {
                            $the_query->the_post();?>

          <div class="col-sm-4 ">
            <div class="reduce">
             <div class="card">
              <?php the_post_thumbnail('',array('class'=>'img-responsive'));?>

                <h3><?php the_field('product_title')?></h3>
                <p class="price"><?php the_field('product_price');?> </p>
                <div class="discription">  
                    <?php the_content(); ?>
                </div>     
             </div>
          </div>
        </div>
       <?php $i++;
         if($i == $total){
        echo '</div>';
       } else  {
            if( $i % 3 ==0){ echo'</div><div class="row">';  }

               }
                          }
                          /* Restore original Post Data */
                          wp_reset_postdata();
                          wp_die();
                        } else {
                          // no posts found
                          wp_die();
                        }  

} 

当我打开产品页面时,它不会显示所有帖子,只有选择了类别后,它才会显示帖子。然后,如果我选择“选择类别”选项,则会显示所有帖子。

1 个答案:

答案 0 :(得分:0)

一种显示所有帖子的简单方法类似于您的WP_Query($args)。尝试使用相同的$args变量,并将其与Wordpress函数get_posts()一起使用。然后,您将遍历foreach循环返回的内容。这是视觉效果:

$args = array(      
 'category_name' =>$whatever,  
 'post_type' => 'ProductsNew',
 'posts_per_page' => get_option('posts_per_page'),
 'order'=>'ASC'
);

$product_array = get_posts($args);

foreach($product_array as $product) {
 // display your product cards
}

随时随地随意扔球,可能在.col-sm-4之后或.event-dropdown之后。

get_posts docs