0

I've got a problem again. I scripted a little JS function, that alters content of a post. To pass the content to the function I used this:

function register_and_enqueue_script()
{
    wp_register_script( 'js_script', plugin_dir_url(__FILE__).'js/script.js', array(), '1.8.5' );
    wp_enqueue_script('js_script');

    if('myPostType' == get_post_type()) {
        $myCustomValue= nl2br(get_post_meta(get_the_ID(), 'custom_value', true));    
        wp_localize_script('js_script', 'myCustomValue', $myCustomValue);
    }
}
add_action('wp_enqueue_scripts','register_and_enqueue_script');

This work perfectly fine, as long as there is only one post displayed. If there is more then one post, only the first post works, but the others dont. I also checked it with "Console.Log(someInnerHTML)" and it always passes the content of the first post to the script.

What do I need to do, so that each post passes it's own content to the function?

EDIT: Thanks for the answer. I was able to save content of each post. Thank you very much for that. But now I have the next problem:

function someunfction()
{
    return '<div>
                <div>
                    <input type="input" onkeyup="doFunction();" "/>
                </div>
            </div>' ;
}

How do I make sure, that only the content of the actual post is changing?

I'm sorry for my many questions :/

4
  • your js should handle an array of contents, and the array initialised by looping through all posts. why do you need js to alter it, though? is it done dynamically on the client side? – inarilo Jun 3 '17 at 13:59
  • Yeah, it's just for the client How can i tell it to loop through all contents? I have no idea :/ – TheOnionMaster Jun 3 '17 at 14:02
  • codex.wordpress.org/The%20Loop, also look at codex.wordpress.org/The%20Loop#Multiple_Loops on the same page, since the loop will be used again when displaying the posts – inarilo Jun 3 '17 at 14:13
  • You should ask the new question rather than edit this one. Otherwise, it can become closed as too broad. – Max Yudin Jun 3 '17 at 15:14
0

Try:

function register_and_enqueue_script()
{
    if('myPostType' == get_post_type() && have_posts()) {

        wp_register_script( 'js_script', plugin_dir_url(__FILE__).'js/script.js', array(), '1.8.5' );
        wp_enqueue_script('js_script');

        $myCustomValue = array();
        while(have_posts()) {
            the_post();
            $mypostid = get_the_ID();
            $myCustomValue[''.$mypostid] = nl2br(get_post_meta($mypostid, 'custom_value', true));
        } // end while
        rewind_posts();

        wp_localize_script('js_script', 'myCustomValue', $myCustomValue);

    } // end if
}
2
  • Thank you very much. You really helped me with saving the values. I'm going to select your answer as solution, when my other problem is solved as well (Just edited my post) :) – TheOnionMaster Jun 3 '17 at 15:08
  • i just realised there is a bug! i meant to concatenate and used + as in js instead of a . as in php in the array index, so sorry! – inarilo Jun 3 '17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.