Skip to content

Instantly share code, notes, and snippets.

@yuriy-yarvinen
Created March 28, 2019 08:38
Show Gist options
  • Save yuriy-yarvinen/73e8b70c19a41735193f0a931e5e32ec to your computer and use it in GitHub Desktop.
Save yuriy-yarvinen/73e8b70c19a41735193f0a931e5e32ec to your computer and use it in GitHub Desktop.
wp-ajax.php
add_action( 'wp_ajax_getPage', 'return_filds' ); // wp_ajax_{ЗНАЧЕНИЕ ПАРАМЕТРА ACTION!!}
add_action( 'wp_ajax_nopriv_getPage', 'return_filds' ); // wp_ajax_nopriv_{ЗНАЧЕНИЕ ACTION!!}
// первый хук для авторизованных, второй для не авторизованных пользователей
function return_filds(){
$args = array('post_status' => 'publish');
$pagesID = get_pages($args);
$id = array();
foreach( $pagesID as $page ){
if(get_page_template_slug( $page->ID ) == 'service.php'){
array_push($id, $page->ID);
}
}
$rand_keys = array_rand($id, 5);
$idRandom1= $id[$rand_keys[0]];
$idRandom2= $id[$rand_keys[1]];
$idRandom3= $id[$rand_keys[2]];
$idRandom4= $id[$rand_keys[3]];
$idRandom5= $id[$rand_keys[4]];
$args = array(
'include' => ''.$idRandom1.','.$idRandom2.','.$idRandom3.','.$idRandom4.','.$idRandom5,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
$post_id = $_POST['post_id'];
foreach( $pages as $page ){
if($page->ID != $post_id){
echo '<a href="' . get_page_link( $page->ID ) . '"><div style="background:url('.get_field('img_banner',$page->ID).') center no-repeat;min-height: 150px;background-size:cover;"></div><span>'. esc_html($page->post_title) .'</span></a>';
}
}
die; // даём понять, что обработчик закончил выполнение
}
////jquery
var post_id = $('#post_id').text();
$.ajax({
url: 'https://vita-medicine.ru/wp-admin/admin-ajax.php',
type: 'POST',
data: 'action=getPage&post_id='+post_id,
success: function( data ) {
$('.postToSeo').append(data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment