Archived topic
ACF custom fields in elements
11 replies · Started by George on September 15, 2021
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' );
Hi 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. :D
Note: I'll try clearing page cache if your site has one (CDN, server-side, plugin)
Hi Elvin. Steps to reproduce:
1) Install ACF
2) Create a new ACF group called Company and inside it a number field called years_in_business. Assign a default value to the field, eg 25.
3) Assign the group to appear under the post-type Element.
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 enter years_in_business in the Meta field name input field.
6) Preview the site. The dynamic number field is not visible in the footer.
Ah 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();
} );
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 to 27, etc.
Can you provide me with some pointers?
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 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 the date() which is the current date.
This way, the "years_in_business" value is dynamic. :D
Ponder on this example:
Say, for example, the year started value is "January 31, 1995" which is 1995-01-31 in Y-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. :D
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();
} );
Ah 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".
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!
Nice 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. :D
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 }
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.
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.
Ah yeah that makes sense. Glad you got it sorted. :D