Get page slug and assign a variable within functions.php

I have created a lazy loading script using AJAX and JSON for my blog. It works perfectly. However I have 4 different blogs on the same site. These are created using custom post types.

I need to get the AJAX to do lazy loading for all of these blogs. I can get it to work for each one by manually changing the name from post to boutiques for example.

Read More

So I created a variable called $postname and want to assign this a different value depending on the slug of the page you are on when you click load more posts.

How can i check what page slug is and then assign my variable a name. Here is my code but I need to know how to call the slug of the page im on and assign that to $slug.

if ( $slug == 'boutiques'  ) {

    $postname = 'boutiques';

} elseif ( $slug == 'tourist' ) {

    $postname = 'tourist';
}
else {
    $postname = 'post';
}

Related posts

Leave a Reply

1 comment

  1. assuming you are properly adding the AJAX script to your theme via wp_enqueue_script, you can pass data to the script via wp_localize_script, which you should already be using to pass the path to admin-ajax.php if you are using AJAX correctly within your plugin or theme.

    see the Codex page on AJAX for more info. this tutorial linked at the bottom of that page has all the examples you’ll need.

    a purely javascript solution (which I don’t recommend) would be to use the location object to determine context.