Excerpts · Custom Field insertions incl. w. shortcodes and from remote posts
$ query = new WP_Query( array( 'p' => XXXXX ) );
$ query->the_post();
The magic Code/Shortcode [ xyz-ics snippet="id"], although invisible, will force the values to be retrieved from the designated remote post
The images are defined in the page's ACF custom fields | Sourced from: "typography4" | ID: "36347"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.▷2
Careful with
1/ conflict betw. 2 types of shortcodes in Functions.php (Beaver with the variable "featured" and the new one below, added on Sept2)
2/ Issues with sas_spoilers and image in acf_wys4. Had to revert to Su_spoiler
Note: See new shortcodes acf_wys4 (but w. sas_spoiler issues) DISABLED
[ acf_wys4][ acf_wys4] DISABLED
Code examplesIncl. code to shortcode
NotOK with shortcodes in remote posts ACF
[ acf_wys1] in functions.php
function wys1_shortcode (){
global $ post;
$ acf_wys1 = get_post_meta( $ post->ID, 'acf_wys1', true );
return $ acf_wys1;
}
add_shortcode( ' acf_wys1', ' wys1_shortcode' );
//WP recommend never to use query_posts
// Shortcodes enabled in Custom Fields: functions.php (field by field) echo do_shortcode(get_post_meta(get_the_ID(), "MyCustomField", true));
//Check > do_action_ref_array( 'pre_get_posts', WP_Query $query )
//Note: a text field is plain text, therefore it can be directly utilized in php, which is what get_field() is for, rather than the_field() that wraps the data into its own echo function so it’s simply do_shortcode( get_field('name') );
of course the best practice in case there’s no data and if there’s extra html around it is to check if it exists first. ACF docs
if ( get_field('name') ) {
echo do_shortcode( get_field('name') ); }
DRAFTS from doc examples TBD?
get_fields([ $post_id], [ $format_value]);
$post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.
$format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true.
[ xyz-ics snippet="addacf" acf_name"acf_txt1"] ··· [ xyz-ics snippet="addacf" acf_name"acf_txt1"]
[ xyz-ics snippet='addacf' acf_name'acf_excerpt'] ··· [ xyz-ics snippet="addacf" acf_name"acf_txt1"]
PHP code to get a custom field from a post
function related_post_acf_shortcode( $atts ) {
// extract attributs
extract( shortcode_atts( array(
'field' => '',
'related_field' => '',
'post_id' => false,
'format_value' => true
), $atts ) );
// get unformatted value of post object field
$related_post_id = get_field($field, $post_id, false);
// get value from the related post and return int
$value = get_field( $related_field, $related_post_id, $format_value);
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
// return
return $value;
}
add_shortcode('related_post_acf', 'related_post_acf_shortcode');
/* shortcode
[related_post_acf field="post_object_field_name" related_field="field_name_on_other_post"]
*/
Excerpts | Featured | Tests for Beaver Builder PHP, custom shortcode [ featured id=” my_field=”] in functions.php
Test results: the solution works fine for html, but fails on shortcodes contained in the source posts’ fields. Only the “html” is called, NOT the content generated through shortcodes within each origin post.
Custom BB featured shortcode display|featured id=54313 my_field=featured|
%%%% [ xyz-ics snippet="id" _id="36347"] %%%%
Using HTML only: Shows the expected result
Article1 id=54313
Article1In this “all HTML” version, using no shortcodes, the title, text, id, image, are direct-coded in the field “Featured” within the post Article1. They all display correctly.
Article1 | acf_wys2 metafield.
Using meta field called in local shortcode | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Illustration: Articles 1 & 2 side by side Why is it OK here, in Excerpts or Front page, and not in /posts/acf-documentation-functions/?
The shortcode inserted into this post calls the ACF fields from posts Article1 & Article2 with the following values. Both show that the versions using shortcodes now pull data from their origin post.
In this “all HTML” version, using no shortcodes, the title, text, id, image, are direct-coded in the field “ACF_Excerpt” within the post Article1. They all display correctly.
Using shortcodes: Shows error cases when applicable
Article1 | acf_wys2 metafield.
Using meta field called in local shortcode | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Article2In this “all HTML” version, using no shortcodes, the title, text, id, image, are direct-coded in the field “Featured” within the post Article2. They all display correctly.
Using shortcodes: Shows error cases when applicable
FAILS in page renderings when user is unsubscribed!50955Edit sc cardlet1
sc name=”Cardlet1
EXCERPTS FROM OTHER POSTS USING CONTENT VIEWS PRO | Displays ACF_excerpts field from Posts (eg Article1, Article2 etc.) via the Content View Pro plugin Note: Advanced Custom Fields (ACF) are now displayed correctly when a filter is applied thanks to this .JS snippet in CV settings
CONTENT VIEWS “PINTEREST” GRID | With filters (Limit 12)
[ pt_view id=”9dcf61fmzf”] REACTIVATE THIS ONE