Yep, you’re looking for wp_localize_script()
. Do away with the global.
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_scripts' );
function wpse_enqueue_scripts() {
wp_enqueue_script( 'wpse-main', get_template_directory_uri() . '/path/to/script.js', array(), false, true );
wp_localize_script( 'wpse-main', 'wpseVars', array(
'postsLoop1' => json_encode( /* first post array */ ),
'postsLoop2' => json_encode( /* second post array */ ),
'postsLoop3' => json_encode( /* third post array */ )
) );
}
Then in your script, you can access the variables by grabbing wpseVars.postsLoop1
, wpseVars.postsLoop2
, etc.
Related Posts:
- Handling an Ajax form submit
- Populate dropdown with Child Pages based on Parent Page chosen
- Open post-content in archive page in a Modal box with bootstrap
- Post repeating with infinite scroll
- Redirect to another page using contact form 7? [closed]
- Get html data with javascript to php array and store to wordpress database
- Load Posts on Click via Ajax into a DIV
- Only show first image in foreach loop
- Incrementing PHP variable onclick to display posts by month
- Get the id of all images in a post
- how to make an array of post id’s in is_single
- array_rand not working correctly?
- Update value of a associative array with update_post_meta
- duplicate posts with ajax load more wordpress
- Post form – AJAX won’t upload featured image – Plugin development
- Display one random image from Media Library
- Show the subcategory name based on current product
- Make sticky post with FacetWP
- make an array in wordpress cusmization api?
- How can i iterate through this shortcode array?
- How to get values from wordpress listings and use them in javascript array?
- link to page_for_posts
- How to pass php array to external js file in WordPress
- using woocommerce_template_single_add_to_cart in shop-loop – javascript issues [closed]
- Uncaught TypeError: Cannot read property ‘firstChild’ of null after upgrading to WordPress 5.5
- Can an array be used as a meta_query value?
- Display notification only once
- Access/update database with jQuery
- How to display something in a div when the user clicks on a text in another div – no page refresh [closed]
- Use WordPress’s Media Uploader/ Manager in non WordPress php application
- Listing and displaying WooCommerce Shipping Zones in the frontend? [closed]
- Automatically generate Post/Page from searched Database item?
- Get/Set wp.customize.previewer.previewUrl
- How do I dynamically generate args for wp_query?
- List of posts by day of the week
- How to migrate the posts from an old custom legacy blog to a new WordPress website?
- How is WordPress manipulating the posts like there are folders?
- javaScript in section of WP API
- Get_the_author doesn’t return author name
- Styling images coming from another blog
- Check if term object is in array
- admin-ajax.php responding with 0
- Using wp_localize_script to store the template url into a variable to use in JS
- How can the plugin directory path be returned into ?
- How can I add tinymce editor in WordPress with jQuery?
- How to speed up admin-ajax.php in wordpress
- Get taxonomy terms only of the WP_Query current posts
- JQuery not working on WordPress Admin page [closed]
- Customizer Show/Hide
- Page Reloads Before AJAX Request Finishes
- Must Use Plugin Causing Query Error
- Exclude posts from homepage having a specified tag
- Colorbox ajax loading of outside HTML content works perfect on localhost but not on server
- ‘orderby’ => ‘rand’ alternative for better performance?
- How to make custom total price reactive in navigation
- Adding tawk.to code just before body tag on functions.php file
- How to convert objects into arrays
- How do I fetch all comments per post via WP REST API?
- How to get 2 or multiple custom post types in wordpress functions.php
- Deregistering a script in WordPress seems impossible
- Slide in Panel WordPress Post
- Sending jsPDF documents to the server
- “before delete post” action fire when the post is updated?
- How to prevent those PHP variables from being cached on WordPress?
- Ajax return code 400
- Parsing php string in jquery [closed]
- Pass a php string to a javascript variable
- Displaying a button on each post
- How can I add an alert (like the old javascript alerts) to my WP page?
- Trying to make php run in a post
- Hide post if matches current month and year
- Pass PHP variable to JavaScript without inline JS
- Admin Options page. Save as Array
- Show post in slider
- How can I output a php value into a JS file within WordPress?
- Creating bulk posts with Youtube videos
- Using shipped version of jquery
- Putting PHP variables into javascript [duplicate]
- Bridging TinyMCE js and WordPress PHP?
- Remove posts after a given amount of time
- PHP get the first post separately from array returned by wpdb->get_results()
- Working with query_posts ( arrays and query strings)
- Automatically add custom CSS to new posts using a category template
- Trying to implement Ajax comments in WordPress, getting WP error
- How to test nonce with AJAX – Plugin development
- how can i show only the parents in owl-carousel?
- Create shortcodes within foreach loop (using array)
- How do I display a user’s previous orders as a select box option on a product?
- Simple Plugin with custom javascript wont work – no console error
- How Display Posts on category
- Hide wordpress field if data is empty in post!
- How does this WordPress Plugin (Thrive Comments) apply their custom comment sort? [closed]
- How can I use a modal window to display the current post in loops featured image?
- How can I use wp_query to show all product data using just the products ID?
- js file in root loading without
- Query all posts of a custom taxonomy term
- class click counter save number
- Retrieving Author ID in wp-admin area
- Using a variable in is_page(array())
- Trigger popup in a php if/else statement