Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1420264

    Is there a way of creating text that updates dynamically? For example “I am 21 years old” where 21 is generated by a date in code or plugin.
    Thanks
    Rob

    #1420370

    Hey fanlokbun,

    Thank you for the inquiry.

    Yes, this should be possible with a custom field and shortcodes. Please check the following documentation for more info.

    // https://codex.wordpress.org/Shortcode_API
    // https://wordpress.org/documentation/article/assign-custom-fields/

    Example:

    add_shortcode('avs_iam_shortcode', 'avs_iam_shortcode_cb');
    
    function avs_iam_shortcode_cb($atts){
           extract(shortcode_atts(array(
                      'age' => 21,
           ), $atts));
           if(!isset($atts[0])) return;
           $field = esc_attr($atts[0]);
           global $post;
           $post_id = (NULL === $post_id) ? $post->ID : $post_id;
           return  "I am ". get_post_meta($post_id, $field, true) ." years old";
    }

    Shortcodes:

    [avs_iam_shortcode age=88]
    

    You have to add the “age” custom field in the post.

    Best regards,
    Ismael

    #1420434

    Many thanks Ismael. I’m having fun playing with this.

    #1420459

    Hi,

    Great, I’m glad that Ismael could help you out. Please let us know if you should need any further help on the topic, or if we can close it.

    Best regards,
    Rikard

    #1420677

    Thanks Rikard you can close the topic.

    #1420686

    Hi fanlokbun,

    Thanks for using Enfold and have a great day!

    Best regards,
    Nikko

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Dynamic text’ is closed to new replies.