2018-03-23 14:28
浏览 73


I have three files for this. My main php file, functions.php, then my js file.

How do you pass this php variable to js? Here is my code for my MAIN PHP FILE

function ccss_show_tag_recipes() {

global $post;
global $wp;

$html = '';

$url = home_url( $wp->request );
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$currentPage = end($pathFragments); //I WANT TO PASS THIS TO MY JS FILE

$term_tags = get_terms('cp_recipe_tags'); 
$term_cats = get_terms('cp_recipe_category');

$recipetype = array();
$recipecat = array();

foreach($term_tags as $term) {
    $recipetype[] = $term->slug;

foreach($term_cats as $term) {
    $recipecat[] = $term->slug;

if ( in_array( $currentPage , $recipecat  ) ) {

        $html = ccss_load_phases($html,$currentPage);



Here is the function for for css_load_phases();

function ccss_load_phases($html = '',$currentPage){
     //CODE HERE etc

    $html .= '<div id="btn-wrapper-'.$termid.'" class="btn-wrapper"><a id="'.$termid.'" class="load-more-btn" href="#">Load more recipes</a></div>';    


That code above is to trigger the Ajax call, All I can get is the button id.

Here is the AJAX file code:

(function($) {
    $(document).on( 'click', 'a.load-more-btn', function( event ) {

    var a = $(this).attr('id'); // I CAN ONLY GET THE ID SINCE IT WAS TRIGGERED BY BUTTON CLICK, how to include $currentPage?

    var button = $(this),
            data = {
            'action': 'ajax_loadmore',
            'termID': a


            url: ajaxloadmore.ajaxurl,
            data: data, 
            type: 'post',
            beforeSend : function () {
                button.text('Loading...'); // change the button text, you can also add a preloader image
            success: function( result ) {


                $('#cooked-recipe-list-'+a+' ').append(result).fadeIn();
                //alert( result );

            complete: function() {
                button.remove(); // if no data, remove the button as well


How Do I pass that $currentPage php variable to my Ajax/Js file? so I can use it here:

add_action( 'wp_ajax_nopriv_ajax_loadmore', 'my_ajax_loadmore' );
add_action( 'wp_ajax_ajax_loadmore', 'my_ajax_loadmore' );

function my_ajax_loadmore() {



2条回答 默认 最新

相关推荐 更多相似问题