What Is Wrong With My Code? (PHP & JS)
-
I’m using PHP & JS code snippets to find the meta field values of posts that are displayed from the content views plugin. The JS works but every post says “undefined”. I used ChatGPT to help with the code.
<?php // Get the "time" meta value for each post displayed by the Content Views plugin $times = array(); $posts = get_posts(array('post_type' => 'post', 'posts_per_page' => -1)); foreach ($posts as $post) { $time = get_post_meta($post->ID, 'time', true); $times[$post->ID] = $time; } ?> <script> jQuery(document).ready(function($) { // Parse the "times" string as a JSON object var times = JSON.parse('<?php echo json_encode($times); ?>'); // Append the "time" meta value to each post $('.pt-cv-content-item').each(function() { var postId = $(this).find('a').attr('href').split('/').pop();
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘What Is Wrong With My Code? (PHP & JS)’ is closed to new replies.