Search code examples
javascriptphpwordpressmeta

get post custom values wp in javascript file


How can I get post meta in JS file

<?php $values = get_post_custom_values("Nalbums"); echo $values[0]; ?>

post meta chage by $post_id

my Js file

    var idurl = get_post_meta($post->ID, 'nalbums', true);
    var settings = $.extend({

        xmlPath: "http://localhost/wp/wp-content/plugins/myplayer/play.php?cat="+idurl+"",
        autoStart: true,
        shuffleStart: false,
        replayStart: false,
        playList: "hide",
        blurLevel: 10,
        colorRGBA:"168,24,58,0.3" 

    }, options );

Solution

  • You'll need to add a <script> tag to your PHP file (e.g. WordPress template file), to add the global variable into javascript.

    For example:

    <script type="text/javascript">
        var idurl  = <?php $values = get_post_custom_values("Nalbums"); echo $values[0]; ?>;
    </script>
    

    You can then use the JavaScript variable in your script.