skip to Main Content

I want to populate a calculated form field with a post title, which is a number, and use that field in other calculations.
The post title is dynamic (daily exchange rate) so one day is 4.9112, another is 4.8492 and so on.

Here’s what I’ve tried:

For example, when I use this script (with a static value):

<script> rate =4.9197 </script> 

it works to use the rate value for a calculation:

`(function(){
var v = fieldname2*((fieldname16 == 'Euro') ? rate: 1);

if(v <= 450000) return 0;
return PREC((v-450000)*.03, 0);
})()`

But I need something like this (where the rate value is a post_id title):

<script> rate =<?php echo get_the_title( $post_id ); ?></script> 

In my case the post_id is 8331.

Does anyone know how I can write the script, so the rate value is the 8331 post_id’s title?

2

Answers


  1. If you need the posttitle you can find them in the Developer Tools of Chrome or Firefox in the DOM.

    Inspect your code and find the selector.

    The following selector is just a example. You have to inspect your DOM.

    var title = $('#post-8331').text();
    console.log('Your title - ', title);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <article id="post-8331">Post Title 4494</article>

    You are in WordPress. I’m not so familiar with wordpress. I think you have to add this in the functions.php file.

    <?php
    /* Inline script printed out in the header */
    add_action('wp_head', 'my_script_wp_head');
    function my_script_wp_head() {
        ?>
            <script>
                console.log("I'm an inline script tag added to the header.");
            </script>
        <?php
    }
    

    or if you like to outsource it to a special file. Create the file custom.js in the folder scripts and place your javascript code there.

    /* Custom script with no dependencies, enqueued in the header */
    add_action('wp_enqueue_scripts', 'my_script_enqueue_custom_js');
    function my_script_enqueue_custom_js() {
        wp_enqueue_script('custom', get_stylesheet_directory_uri().'/scripts/custom.js');
    }
    

    See this tutorial how to include custom js to wordpress:
    https://webdesign.tutsplus.com/tutorials/how-to-add-custom-javascript-to-your-wordpress-site–cms-34368

    Login or Signup to reply.
  2. Your snippet looks like it could work, assuming the post_title is purely a number like you said: 4.9197 or 4.8492.

    You could wrap the PHP portion with parseFloat()

    <script> var rate = parseFloat(<?php echo get_the_title( $post_id ); ?>);</script> 
    

    EDIT:

    What I see:

    Screenshot.png

    The snippet of code I first gave needs to be added via PHP in something like the functions.php file of your WordPress Child Theme. If that is not possible, then you can try this in pure javascript:

    <script>
        var rate = parseFloat(document.querySelector('meta[property="og:title"]').content);
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search