如何在WordPress主题模板中将data.id赋值给PHP变量
Hey there, let's break down why your PHP attempts aren't working and how to fix this.
The Core Issue
That {{data.id}} is a Handlebars/Underscores JS template variable—it runs in the browser after all your PHP code has already executed on the server. PHP has no access to this data object because it doesn't exist when PHP is processing the page.
Your attempts to use $data->id or $data['id'] are trying to access a PHP variable that doesn't exist here—this template is a mix of pre-executed PHP (the do_action hooks) and client-side JS template syntax (the {{ }} bits).
Solutions Based on What You Need to Do
Let's cover the common scenarios:
1. You need to use the ID in client-side JS (not PHP)
If you just need the ID for some JS logic inside or around this template, use the Handlebars syntax directly. For example:
<# // Inside the Handlebars template block #> <script> var listingId = {{data.id}}; // Do something with listingId, like trigger a custom action console.log('Listing ID:', listingId); </script>
2. You need to use the ID in PHP (e.g., inside those do_action hooks)
Since PHP runs before the JS template renders, you can't grab data.id directly here. Instead:
- Modify the backend data feed: Listify populates this
dataobject from a JSON response generated by PHP. Look for hooks likelistify_listing_datain the theme codebase to add pre-processed values tied to the listing ID before it's sent to the frontend. - Use AJAX for dynamic PHP logic: If you need PHP-generated content tied to the listing ID, use JS to send an AJAX request to a WordPress endpoint, passing
{{data.id}}as a parameter, then inject the result into the template.
3. If you mistakenly expected server-side rendering (unlikely for this template)
This specific tmpl-listingCard template is designed for client-side rendering, but if you were trying to render it server-side, you'd need to pass the PHP post ID to the template context. For example, in the PHP code loading the template:
$template_context = array( 'id' => get_the_ID(), // Add other required data fields ); // Pass this context to your template renderer (depends on the theme's setup)
Quick AJAX Example (for PHP logic tied to the ID)
- Add this to your theme's
functions.phpto register an AJAX endpoint:
add_action('wp_ajax_get_listing_custom_data', 'get_listing_custom_data'); add_action('wp_ajax_nopriv_get_listing_custom_data', 'get_listing_custom_data'); function get_listing_custom_data() { $listing_id = intval($_POST['listing_id']); // Run your PHP logic here, e.g., fetch custom meta $custom_data = get_post_meta($listing_id, 'your_custom_meta_key', true); wp_send_json_success($custom_data); }
- Add this to your JS template to fetch and display the data:
<# // Inside the Handlebars template #> <div class="listing-custom-data"></div> <script> jQuery(document).ready(function($) { $('.job_listing').each(function() { var $listing = $(this); var listingId = $listing.attr('id').replace('listing-', ''); $.post(ajaxurl, { action: 'get_listing_custom_data', listing_id: listingId }, function(response) { if (response.success) { $listing.find('.listing-custom-data').text(response.data); } }); }); }); </script>
Recap
You can't mix client-side Handlebars variables with server-side PHP directly—they run at completely different times. Pick the approach that matches whether your logic needs to run on the server (modify the backend data feed) or the client (use JS/AJAX).
内容的提问来源于stack exchange,提问作者Bobby Kumar

