admin管理员组

文章数量:1316363

Can anyone please help me? please see the screenshot. it is a JS file. here I want to get permalink of my custom posts. But I don't understand how get permalink into a JS fils. So, please help to out this solution.

Can anyone please help me? please see the screenshot. it is a JS file. here I want to get permalink of my custom posts. But I don't understand how get permalink into a JS fils. So, please help to out this solution.

Share Improve this question edited Nov 28, 2016 at 8:08 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Nov 23, 2016 at 4:18 Shakib HasanShakib Hasan 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 1

To get a permalink you need to use get_permalink(). Just pass the ID of the post or leave it blank to get the current page in the Loop.

wp_localize_script () will render PHP variables on the page that are consumable by JS through a global object that you define.

PHP

// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );

// Localize the script with new data
$translation_array = array(
    'some_string' => __( 'Some string to translate', 'plugin-domain' ),
    'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

JS

<script>
// alerts 'Some string to translate'
alert( object_name.some_string);
</script> 

The REST-API is also always available for you to extend how you see fit: http://v2.wp-api/extending/adding/

本文标签: jqueryhow to get permalink