How to display post by category in shortcode WordPress

Question

Hello Guys plz help me i try to show post by category but post not show in frontend with category

My shortcode is

[am_post_grid posts_per_page="6" paginate="yes"]

and i try to display post by category show how to add category in shortcode like this

[am_post_grid posts_per_page="6" paginate="yes" category="fishing,hiking"]

My custom code this

//shortcode function
function asrafp_shortcode_mapper( $atts, $content = null ) { 
$pppInit = ( get_option( 'posts_per_page', true ) ) ? get_option( 'posts_per_page', true ) : 9;


$shortcode_atts = shortcode_atts(
    array(              
        'show_filter' => "yes",
        'btn_all' => "yes",
        'initial' => "-1",
        'layout' => '1',
        'post_type' => 'referenzen',
        'posts_per_page' => $pppInit,
        'category' => '',
        'paginate' => 'no',
    ), 
    $atts
);

  // Params extraction
  extract($shortcode_atts);

ob_start();
$taxonomy = 'post_tag';
$terms = get_terms($taxonomy);
//$terms = get_tags(array('get'=>'all'));

?>
  <div class="am_ajax_post_grid_wrap" data-am_ajax_post_grid='<?php echo json_encode($shortcode_atts);?>'>

   <?php if ( $show_filter == "yes" && $terms && !is_wp_error( $terms ) ){ ?>
    <div class="asr-filter-div" data-layout="<?php echo $layout; ?>"><ul>
        <?php if($btn_all != "no"): ?>
             <li class="asr_texonomy all_post active" data_id="-1"><?php echo esc_html('All','am_post_grid'); ?></li>
        <?php endif; ?>
        <?php foreach( $terms as $term ) { ?>
                <li class="asr_texonomy <?php echo $term->name; ?>" data_id="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></li>
        <?php } ?>
    </ul></div>
<?php } ?>

<div class="asr-ajax-container">
    <div class="asr-loader">
        <div class="lds-dual-ring"></div>
    </div>
    <div class="asrafp-filter-result"></div>
   </div>
 </div>

 <?php return ob_get_clean();
 }
    add_shortcode('asr_ajax','asrafp_shortcode_mapper');
     add_shortcode('am_post_grid','asrafp_shortcode_mapper');

  //ajax actions
  add_action('wp_ajax_asr_filter_posts', 'asrafp_ajax_functions');
  add_action('wp_ajax_nopriv_asr_filter_posts', 'asrafp_ajax_functions');

  //ajax main function
function asrafp_ajax_functions(){
  // Verify nonce
    if( !isset( $_POST['asr_ajax_nonce'] ) || !wp_verify_nonce( $_POST['asr_ajax_nonce'], 
'asr_ajax_nonce' ) )
  die('Permission denied');

 $term_ID = sanitize_text_field( intval($_POST['term_ID']) );
 $layout = intval($_POST['layout']);

 // Pagination
if( $_POST['paged'] ) {
$dataPaged = intval($_POST['paged']);
 } else {
  $dataPaged = get_query_var('paged') ? get_query_var('paged') : 1;
 }   

  $jsonData = json_decode( str_replace('', '', $_POST['jsonData']), true ); 

$data = array(
      'post_type' => 'referenzen',
   'post_status' => 'publish',
    //'categoryN' => $category,
       'paged' => $dataPaged,
    );
    // $data = array(   
    //  'category_name' => $category,
   // );
  if( $jsonData ){
   if( $jsonData['posts_per_page'] ){
    $data['posts_per_page'] = intval( $jsonData['posts_per_page'] );
  }
  }

if( $term_ID != -1 ){
    $data['tax_query'] = array(
       array(
        'taxonomy' => 'post_tag',
        'field' => 'term_id',
        'terms' => $term_ID,
      )
   );
}

//post query
   $query = new WP_Query($data);
ob_start();

   if( $query->have_posts() ): 
        echo "<div class='am_post_grid am__col-3 am_layout_{$layout}'>";
         while( $query->have_posts()): $query->the_post(); ?>

    <?php if($layout == 1){ ?>
    <div class="am_grid_col">
        <div class="am_single_grid">
            <div class="am_thumbs">
                <?php //the_post_thumbnail('full'); ?>
                 <div class="slider-inner">
                 <div id="owl-demo" class="owl-carousel owl-theme" style="display: block;">

    <?php if( get_field('post_image') ): ?>
     <div class="item">   
    <a class="thumbnail" href="<?php the_field('post_image');?>"><img src="<?php 
  the_field('post_image');?>" style="width:100%"></a>  
    </div><?php endif; ?>
0
Dinesh Thakur 3 months 0 Answers 13 views 0

Leave an answer