Digital experiences for all disciplines
New Landing › How can we help? › Themeforest Theme Support › Dante › Shortcode with variables?
New Landing › How can we help? › Themeforest Theme Support › Dante › Shortcode with variables?
- This topic has 9 replies, 3 voices, and was last updated 9 years by Rui Guerreiro – SUPPORT.
-
Posted in: Dante
-
February 28, 2015 at 11:13 pm #154045
Hey all,
Quick question. I’m trying to print a shortcode and it’s behaving oddly.
My code:
<?php $ba_score = the_field('beer_advocate_score'); echo do_shortcode('[chart percentage="'.$ba_score.'" size="170" barcolour="" trackcolour="" content="'.$ba_score.' / 100" align="center"]'); ?>
The $ba_score is just a numerical value, 84 for example.
What I get when the function runs can be seen in the screenshot.
Basically, it breaks the short code and prints the values first occurrence and skips the second entirely.Thoughts?
(I’m ok, printing the variables into the html the shortcode generates but the animation seems to break when I do that).
Thanks!
Attachments:
You must be logged in to view attached files.March 2, 2015 at 6:55 am #154188Hi,
Where are you inserting this code?
Thanks
MohammadMarch 2, 2015 at 4:42 pm #154451This reply has been marked as private.March 2, 2015 at 4:51 pm #154457Hi,
Try this format instead.
<?php $ba_score = the_field('beer_advocate_score'); $ba_score += '/ 100'; echo do_shortcode('[chart percentage="'.$ba_score.'" size="170" barcolour="" trackcolour="" content="'.$ba_score.'" align="center"]'); ?>
-Rui
March 2, 2015 at 9:11 pm #154509I’ve implemented the code you provided and it seems to be a solid step in the right direction, however I’m still getting raw data output and no animation. I’ve left the code in the page so you can see the output.
Thanks!
March 3, 2015 at 6:02 am #154568Hi,
Your data percentage is zero. So animation is not working. Your $ba_score have zero value.
Thanks
MohammadMarch 13, 2015 at 5:07 pm #157231Hey Mohammad,
I’ve double checked the code, and the value is being output above the circle. You can see little numbers. These are the printed output of the first time the variable is called (in the shortcode) the second time it’s called (what would be the printed text inside the circle) is coming back blank, so 0.
It’s like the code is printing the variable value outside of the theme’s shortcode then failing…
thanks!
March 13, 2015 at 5:13 pm #157235This code enables both the circle label animation and the text in the circle to work:
<?php $ba_score = 42; // $ba_score = the_field('beer_advocate_score'); echo "Test".$ba_score; //$ba_score += '/ 100'; echo do_shortcode('[chart percentage="'.$ba_score.'" size="170" barcolour="" trackcolour="" content="'.$ba_score.' / 100" align="center"]'); ?>
I’m investigating the the_field(”) function from Advanced Custom Fields.
Thoughts?
March 13, 2015 at 5:27 pm #157240Looks like I just needed to take a step back.
From Advanced Custom Fields Documentation:
the_field()
Displays the value of the specified field. (this is the same as “echo get_field($field_name)”)get_field()
Returns the value of the specified field.This is a very versatile function. You can use it to store a value, echo a value and interact with a value. Please note the type of variable returned is relative to the field type. Eg: A repeater will return a multidimensional array.
So the code:
<?php //$ba_score = 42; $ba_score = get_field('beer_advocate_score'); echo "Test".$ba_score; //$ba_score += '/ 100'; echo do_shortcode('[chart percentage="'.$ba_score.'" size="170" barcolour="" trackcolour="" content="'.$ba_score.' / 100" align="center"]'); ?>
Works.
Thanks!
March 13, 2015 at 6:03 pm #157252you probably have to use the other function that also get’s the post id.
something like the line below where 37 is the id of the post where you saved that specific custom field beer_advocate_score.
$ba_score = the_field('beer_advocate_score', 37);
-Rui
-
Posted in: Dante
You must be logged in and have valid license to reply to this topic.