Before I start, if you need to pass variables between template parts, you have to check out this post. There are exceptional answers (specially from @kaiser and @gmazzap) on how to achieve this, specially without having to use globals 😉
There are a ton of stuff that you would need to do first as we really need to avoid bugs and fatal errors due ACF beign deactivated or due to something that does not exist.
A FEW NOTES
-
The following code is untested and may contain bugs. Be sure to test this locally first. Also note that the code requires at least PHP 5.4
-
I have never worked with ACF, I have quickly read the relevant documentation and implemented my understanding of the docs in my code
-
Use, abuse and modify the code as needed. The code is very static, but you can make it dynamic. As I do not know exaactly how you would want to use it, it might be an idea to extend this to incorporate your links and feed into the function itself, but this you need to do yourself.
-
I have commented the code for easy following and understanding
THE CODE
/** Function to return either the title or content from an ACF field choice
*
* @param (string) $to_return Value are 'title' or 'content'
* @return (string) $output
*/
function get_single_variable( $to_return = '' )
{
/**
* First get our current post object. This section is something I'm experimenting
* with get a reliable way to pass the current post object as $post is not reliable.
* NOTE: query_posts will break get_queried_object(), this is one reason you should
* never ever use query_posts
*/
if ( is_singular() ) {
$current_post = get_queried_object();
} else {
$current_post = get_post();
}
// Make sure we have a valid vale for $to_return, if not return an empty string
if ( 'title' !== $to_return
&& 'content' !== $to_return
)
return '';
// Make sure ACF is activated and that get_fields are available. If not, bail out and return an empty strin
if ( !function_exists( 'get_fields' ) )
return '';
// Now that we now get_fields exists and we will not recieve fatal errors, lets safely continue
// We will now get all the fields belonging to the post at once
$fields = get_fields( $current_post->ID );
// ?><pre><?php var_dump( $fields ); ?></pre><?php // For debugging purposes, just uncomment it to dump the value of $fields
//Set our variables to avoid bugs like undefined variables
$cleaned_title="";
$cleaned_content="";
// Setup our conditional statements
$social_title = $fields['social_title'];
if ( !empty( $social_title ) ) {
// Use wp_strip_all_tags to use native functions (which is filterable), stip_tags are still valid though
$cleaned_title = wp_strip_all_tags( $fields['social_title'] );
$cleaned_content = wp_strip_all_tags( $fields['social_content'] );
} else {
$cleaned_title = wp_strip_all_tags( $current_post->post_title );
// Get our content from another field now
$cleaned_social_content = wp_strip_all_tags( $fields['content'] );
// Use mb_substr() and mb_strlen() which is multibyte safe
if ( 136 < mb_strlen( $cleaned_social_content ) ) {
$excerpt_more = __( '…' );
$cleaned_content = mb_substr( $cleaned_social_content, 0, 137 ) . $excerpt_more;
} else {
$cleaned_content = $cleaned_social_content;
}
}
if ( 'title' === $to_return ) {
return $cleaned_title;
} else {
return $cleaned_content;
}
}
You can now use it anywhere to display the title or content as follow
echo get_single_variable( 'title' );
or
echo get_single_variable( 'content' );