Search
Close this search box.

wordpress 接口筛选PHP代码

<?php
if ( ! defined( 'WP_DEBUG' ) ) {
	die( 'Direct access forbidden.' );
}
add_action( 'wp_enqueue_scripts', function () {
	wp_enqueue_style( 'iconfont', get_stylesheet_directory_uri() . '/assets/fonts/iconfont.css' );
	wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
	wp_enqueue_style( 'shopeo-child', get_stylesheet_uri() );
	wp_enqueue_script( 'shopeo-child-app', get_stylesheet_directory_uri() . '/app.js', array( 'jquery' ) );
	wp_enqueue_style( 'ala-fa', get_stylesheet_directory_uri() . '/fontawesome/css/fontawesome.min.css' );
} );

add_filter( 'add_image_size', create_function('','return 1;') );
 
add_action("wp_ajax_vote_prodct_lists", "add_votes_products");   
add_action("wp_ajax_nopriv_vote_prodct_lists", "add_votes_products");

function add_votes_products(){
    if(isset($_POST['action']) && $_POST['action'] == 'vote_prodct_lists'){
        $cid = $_POST['cid'];
        $fm  = $_POST['fm'];
        $sm  = $_POST['sm'];
        $offset  = $_POST['offset'];
        $isshowes  = $_POST['ishow'];
        $SpecificationCate = get_terms('specification',array(
           'orderby'    => 'date',
           'hide_empty' => 0
        ));
        // print_r("<pre>");
        // print_r($SpecificationCate);
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 6,
            'offset'  => $offset,
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => array($cid),
                    'operator' => 'IN',
                ),
            )
        ); 
        $lists = new WP_Query($args);
        foreach ($lists->posts as $k => $a){
                 $thumbnail_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($a->ID),'thumbnail');
                 $a->thumbnail=$thumbnail_image_url[0];
                 $a->links = get_permalink($a->ID);
                 $terms = wp_get_post_terms($a->ID,'specification',true);//得到每篇文章的属性数组
                 $a->terms = $terms;
                 $isshow = 1;
                 foreach ($terms as $q => $r){
                     if($r->name >= $fm && $r->name <= $sm){
                         $isshow = 2;
                     }
                 }
                 $a->isshow = $isshow;
            }
            $arrList = array();
            foreach ($lists->posts as $b => $v){
                if($v->isshow == 2){
                     $cs = '';
                     foreach ($v->terms as $t){
                         $cs.=$t->name. ',' ; 
                     }
                     $cs = substr($cs,0,strlen($cs)-1);
                     $v->cs = $cs;
                     array_push($arrList,$v);
                 }
            }
        // print_r("<pre>");
        // print_r($arrList);
        if($isshowes == 1){
            $listsArr= $arrList;
        }else if($isshowes == 2){
            $listsArr= $lists->posts;
        }
        wp_send_json($listsArr);
    }
}






add_action("wp_ajax_vote_case_lists", "add_votes_case");   
add_action("wp_ajax_nopriv_vote_case_lists", "add_votes_case");


function add_votes_case(){
    $cid = $_POST['cid'];
    $childArr = get_term_children($cid,'case-category');
    foreach($childArr as $ca){
        $chilArr = get_term($ca);
        $chilArr->name = str_replace("&amp;","&",$chilArr->name);
        $cAr[] = get_term($chilArr);
    }
    wp_send_json($cAr);
}




add_action("wp_ajax_get_case_lists", "get_case_lists");   
add_action("wp_ajax_nopriv_get_case_lists", "get_case_lists");


function get_case_lists(){
    $sid = $_POST['sid'];
    $offset = $_POST['offset'];
    $postsPerPage = 6;
    $args = array(
        'post_type' => 'case',
        'posts_per_page' => $postsPerPage,
        'offset'  => $offset
    ); 
    if($sid || !empty($sid)){
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'case-category',
                'terms' => $sid
            )
        );
    }
    $lists = new WP_Query($args);
    // print_r("<pre>");
    // print_r($lists);
    foreach ($lists->posts as $p){
        $p->imgurl = wp_get_attachment_url(get_post_thumbnail_id($p->ID));
        $p->links = get_permalink($p->ID);
    }
    wp_send_json($lists->posts);
}

add_action("wp_ajax_get_post_type_lists", "get_post_type_lists");   
add_action("wp_ajax_nopriv_get_post_type_lists", "get_post_type_lists");


function get_post_type_lists(){
    $timeArr=get_terms('religion-categories', array(
			'orderby' => 'count',
			'hide_empty' => 0
	));
	$timeArr1=get_terms('country-categories', array(
			'orderby' => 'count',
			'hide_empty' => 0
	)); 
	$timeArr2=get_terms('job-categories', array(
			'orderby' => 'count',
			'hide_empty' => 0
	));    
    wp_send_json(array("religion"=>$timeArr,"country"=>$timeArr1,"job"=>$timeArr2));
}

add_action("wp_ajax_get_post_art_lists", "get_post_art_lists");   
add_action("wp_ajax_nopriv_get_post_art_lists", "get_post_art_lists");


function get_post_art_lists(){
    $getdate = json_decode(str_replace("\\","",$_POST['datas']));
    $arr = array();
    
    if(!empty($getdate->religion)){
		    array_push($arr,array(
				'taxonomy'         => 'religion-categories',
				'terms'            => $getdate->religion
			));
	}
	if(!empty($getdate->country)){
		    array_push($arr,array(
				'taxonomy'         => 'country-categories',
				'terms'            => $getdate->country
			));
	}
	if(!empty($getdate->job)){
		    array_push($arr,array(
				'taxonomy'         => 'job-categories',
				'terms'            => $getdate->job
			));
	}
    $args = array(
			'post_type' => 'careers',
			'orderby'=>'date',
			'order'=>'DESC',
			'posts_per_page' => -1
		);
		if(count($arr)>0){
		    $args['tax_query'] = $arr;
		}
	$result = new WP_Query($args);
	foreach ($result->posts as $key){
	    $key->religion = wp_get_post_terms( $key->ID, 'religion-categories', true )[0]->name;
	    $key->country = wp_get_post_terms( $key->ID, 'country-categories', true )[0]->name;
	    $key->job = wp_get_post_terms( $key->ID, 'job-categories', true )[0]->name;
	}
    wp_send_json($result->posts);
}










发表评论

Optimized by WPJAM Basic