- This topic has 11 replies, 2 voices, and was last updated 3 years, 12 months ago by
Elvin.
-
AuthorPosts
-
September 15, 2021 at 1:48 pm #1931907
George
I am assigning an ACF custom number field to appear in a footer element which does fine on the backend but then I am not able to display it through the dynamic headline element. I even try to create a shortcode for it but no luck.
function display_years_in_business() { $years_in_business = get_field( "years_in_business" ); ob_start(); if( $years_in_business ) { echo $years_in_business; } return ob_get_clean(); } add_shortcode( 'years_in_business', 'display_years_in_business' );
September 15, 2021 at 8:54 pm #1932102Elvin
StaffCustomer SupportHi George,
The shortcode itself doesnt have
is_user_logged_in()
condition so its likely not your code.To clarify: believe the Block Element that contains this shows up and it’s only this part that doesn’t?
Can you link me to a sample page in question (perhaps provide temporary backend access as well to check)?
Let us know. 😀
Note: I’ll try clearing page cache if your site has one (CDN, server-side, plugin)
September 16, 2021 at 8:54 am #1932817George
Hi Elvin. Steps to reproduce:
1) Install ACF
2) Create a new ACF group called Company and inside it a number field calledyears_in_business
. Assign a default value to the field, eg25
.
3) Assign the group to appear under the post-typeElement
.
4) Create a footer block and assign it to the entire site.
5) Inside the footer block, create a headline block. Click its dynamic options, select Post meta, and enteryears_in_business
in the Meta field name input field.
6) Preview the site. The dynamic number field is not visible in the footer.September 16, 2021 at 8:47 pm #1933276Elvin
StaffCustomer SupportAh I see what seems to be the issue here.
I think its step #4.
The dynamic value for post meta only pulls from the
$source
in GP Premium’s class-block-elements.php and this$source
is whatever you’ve assigned to the display rule location.And since you cant use
gp_element
as its own source on the display rule, it displays nothing.I think you’ll need a different approach for this. An approach where
years_in_business
is a global value instead of something you have to assign on an ACF.example:
Making it global.
function footer_global_vars() { global $footerValues; $footerValues = array( 'years_in_business' => '25', ); } add_action( 'parse_query', 'footer_global_vars' );
And then create shortcode for it so you can place it on a Block.
add_shortcode( 'years_in_business', function() { ob_start(); global $footerValues; echo '<span class="years-in-business">'.$footerValues['years_in_business'].'</span>'; return ob_get_clean(); } );
September 17, 2021 at 12:30 pm #1934110George
Perfect, it works great!
I would like that number to increase by one after a certain date (14th March 2022) and then increase by one a year after that date and so on. For example, on 14th March 2022, it would increase to
26
, 14th March 2023 to27
, etc.Can you provide me with some pointers?
September 18, 2021 at 9:55 am #1934880Elvin
StaffCustomer SupportI would like that number to increase by one after a certain date (14th March 2022) and then increase by one a year after that date and so on. For example, on 14th March 2022, it would increase to 26, 14th March 2023 to 27, etc.
I think you should have a
$year_started
variable and then do a value difference variable between the $year_started date and thedate()
which is the current date.This way, the “years_in_business” value is dynamic. 😀
Ponder on this example:
Say, for example, the year started value is “January 31, 1995” which is
1995-01-31
inY-m-d
format.We can do something like this.
function footer_global_vars() { $year_started = new DateTime('1995-01-31' )); $current_date = new DateTime(date('Y-m-d')); $years_count = $post_date->diff($current_date); $years_in_business = $years_count->y; global $footerValues; $footerValues = array( 'years_in_business' => $years_in_business, ); } add_action( 'parse_query', 'footer_global_vars' );
This snippet basically compares the value between the year started date and the current date the visitor checked the site. It then counts the year difference between the 2 values and then assigned the year difference as a global value. 😀
September 18, 2021 at 11:29 am #1934960George
The code here:
$years_in_business = $years_count->y;
What is the
y
refer to?In any case the code produces an error:
function footer_global_vars() { $year_started = new DateTime('1996-03-14' ); $current_date = new DateTime(date('Y-m-d')); $years_count = $post_date->diff($current_date); $years_in_business = $years_count->y; global $footerValues; $footerValues = array( 'years_in_business' => $years_in_business, ); } add_action( 'parse_query', 'footer_global_vars' ); add_shortcode( 'years_in_business', function() { ob_start(); global $footerValues; echo $footerValues['years_in_business']; return ob_get_clean(); } );
September 18, 2021 at 11:35 am #1934969Elvin
StaffCustomer SupportAh my bad. I forgot to change this line
$years_count = $post_date->diff($current_date);
to this line$years_count = $year_started->diff($current_date);
The code here:
$years_in_business = $years_count->y;What is the y refer to?
It’s the differences between the 2 dates in “years”.
September 18, 2021 at 11:42 am #1934980George
This is amazing, it works great my man!
Here is the full code:
function footer_global_vars() { $year_started = new DateTime('1996-03-14' ); $current_date = new DateTime(date('Y-m-d')); $years_count = $year_started->diff($current_date); $years_in_business = $years_count->y; global $footerValues; $footerValues = array( 'years_in_business' => $years_in_business, ); } add_action( 'parse_query', 'footer_global_vars' ); add_shortcode( 'years_in_business', function() { ob_start(); global $footerValues; echo $footerValues['years_in_business']; return ob_get_clean(); } );
Thanks again!
September 18, 2021 at 11:51 am #1934985Elvin
StaffCustomer SupportNice one. Glad to be of any help. No problem.
To add: you can do a
var_dump($years_count)
to see the complete date difference incase you want to fetch more (months, weeks, days, hours, minutes, seconds.). I think you’ll get the things you need(or in case you need it) once you see the whole array. 😀Also, if you want to style it specifically, you can add element+selector on the shortcode.
you can modify this line :
echo $footerValues['years_in_business'];
to something like this.echo '<span class="years-in-business">.'$footerValues['years_in_business'].'</span>';
so you can style it with
.years-in-business { //your css here }
September 18, 2021 at 11:56 am #1934986George
Perfect. In my case, I just use the shortcode into a headline element as it can accept shortcodes. I, then, style the headline element, it works great.
September 18, 2021 at 12:00 pm #1934988Elvin
StaffCustomer SupportPerfect. In my case, I just use the shortcode into a headline element as it can accept shortcodes. I, then, style the headline element, it works great.
Ah yeah that makes sense. Glad you got it sorted. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.