why i can’t use admin-ajax.php

first i new to wordpress and stackexchange and my english is not good, so my problem is i build my own wordpress theme and want to use ajax in it, i’m using ajax for change post category in post loop, so when a person clicked button in a page it will change category in the loop code triggered by ajax

this is my .js file that contain ajax

function kategori(kategori2){
//alert(kategori2);
jQuery.ajax({
url: readmelater_ajax.ajax_url,
type: “POST”,
data: { action: “ganti”, kategori: kategori2},
success : function( data ) {
alert(kategori2);
}
});

}

this is my file that contain loop to retrieve data sent by ajax

admin_url(‘admin-ajax.php’)) );
}
/**
* Enqueues plugin-specific styles.
*/
function enqueue_rml_styles() {
wp_enqueue_style( ‘rml-style’ );
}

function ganti()
{
$kategori = $_POST[‘kategori’];
if ( get_query_var(‘paged’) ) {
$paged = get_query_var(‘paged’);
} elseif ( get_query_var(‘page’) ) { // ‘page’ is used instead of ‘paged’ on Static Front Page
$paged = get_query_var(‘page’);
} else {
$paged = 1;
}
$custom_query_args = array(
‘post_type’ => ‘post’,
‘posts_per_page’ => get_option(‘posts_per_page’),
‘paged’ => $paged,
‘post_status’ => ‘publish’,
‘ignore_sticky_posts’ => true,
‘category_name’ => $kategori,
‘order’ => ‘DESC’, // ‘ASC’
‘orderby’ => ‘date’ // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );

if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post();
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘large’ );
$content = wp_strip_all_tags( get_the_content() );
$trimmed_content = wp_trim_words( $content, 10, NULL );
$categories = get_the_category();
?>

<div class=”col-6 col-sm-3 p-lg-2 p-1″>
<div class=”shadow-baru card border-0 card-ku px-0 py-0″>
<div class=”text-left card-header bg-success px-2 py-1 text-white”>
<strong><?php echo $categories[0]->name; ?></strong>
</div>
<img src=”<?php echo $image[0];?>” class=”card-img rounded-0″ alt=”…”>
<div class=”card-body px-lg-3 px-1 py-2″>
<div class=”card-title my-1 my-lg-2″><strong><?php the_title(); ?></strong></div>
<div class=”card-text”><small><?php echo $trimmed_content ?></small></div>
</div>
<div class=”card-footer text-muted text-right py-2 py-lg-3″>
<?php the_time( ‘j M Y’ ); ?>
</div>
</div>
</div>

<?php
endwhile;
endif;

}

Please help me, i really stuck, i had tried hard to find the solution in the internet but still can’t fix my problem

Read more here:: why i can’t use admin-ajax.php

Leave a Reply

Your email address will not be published. Required fields are marked *