Home › Forums › Front-end Issues › wp localize script and jquery
Hello,
I created field and working fine in post and showing where I want for product:
<div class="class_field">
<?php if( get_field('custom_field') ): ?>
<h4>Somme text: <?php the_field('custom_filed'); ?></h4>
<?php endif; ?>
</div>
An now I want this custom field showing with jQuery on different part of page, because I can’t access to print formated %1$s, %2$s, %3$s with php.
In child theme function I have wp_localized_script
function pw_load_scripts() {
wp_enqueue_script('pw-script', plugin_dir_url( __FILE__ ) . 'js/custom_field.js');
wp_localize_script('pw-script', 'pw_php_vars', array(
'custom_filed' => custom_filed
)
);
}
add_action('wp_enqueue_scripts', 'pw_load_scripts');
and in custom_field.js
;(function($){
$('.class_to_add_field').html(pw_php_vars.custom_field);
})(jQuery);
and it doesn’t work. Can you help me with some directions?
Thanks for help,
A
try change order of commands. This is my working example:
function enqueue_scripts()
$params = array ( 'someparam' => get_field('field_name', 'options') );
wp_register_script('scripts', 'js/scripts.js', array('jquery'), false, false);
wp_localize_script( 'scripts', 'params', $params );
wp_enqueue_script('scripts');
}
add_action('wp_enqueue_scripts', 'enqueue_scripts');
Hello,
thanks for answer,
I try now:
function pw_load_scripts() {
wp_register_script('pw-script', get_template_directory_uri() . '/assets/public/js/custom_filed.js', array('jquery'));
wp_localize_script('pw-script', 'pw_php_vars', array(
'custom_filed' => custom_filed
)
);
wp_enqueue_script('pw-script');
}
add_action('wp_enqueue_scripts', 'pw_load_scripts');
and still getting text custom_field instead of value. Is maybe problem with jquery?
I try with append and html
Thanks for help,
A
Perhaps this is not the case. The first message shows that you created a field for a specific post. If you are trying to access this field from another template, then you need to use additionally the id of this post.
in post
<h4>Somme text: <?php the_field('custom_filed'); ?></h4>
outside this post
<h4>Somme text: <?php the_field('custom_filed', $post_id); ?></h4>
e.g.
<h4>Somme text: <?php the_field('custom_filed', 10); ?></h4>
where 10 is ID of post
Hello,
thanks but I create date and another custom field which works in product:
http://wp.swlabs.co/exploore/cruises/alaska/
and now I want show this field also in grid: http://wp.swlabs.co/exploore/cruises/
Post type for cruises is only one in ACF: sizexploore_crusie.
I add manualy to product template:
<div class="class_field">
<?php if( get_field('custom_field') ): ?>
<h4>Somme text: <?php the_field('custom_filed'); ?></h4>
<?php endif; ?>
</div>
But can’t add it on grid template, because of printf formated %1$s, %2$s, %3$s which don’t accept php because is html array, so I try to add this field to price with jQuery …
I don’t understand what you want to do )))) and why you try use %1$s, %2$s, %3$s
Hello, I’m yust saying that I can’t access to grid template (because of html array) as I can on product/single cruise template so I must access with jquery to add this field to grid template and I don’t know why doesn’t work…
The topic ‘wp localize script and jquery’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.