ACF / PHP in Content Area

Pro 2.3.8

I tried to use <p><?php the_field('my_field', 457); ?></p> in a Content Area element to display a field from a post with the ID 457, but the value doesn’t show on the front end.

If I use the ACF shortcode [acf field="my_field" post_id="457"], however, the value does show up.

Is it normal that PHP code from a Content Area element does not execute?

Hello Patrick,

Thanks for writing in!

Please be advised that any element, like Content Area element, within Cornerstone and even in the default WP editor, it will not run PHP code. It will only accept plain text and html codes.

You can use {{acf: my_field}} instead.

For more details on how to use ACF Pro, please check this out:

Okay, thanks. Good to know.

How can I use {{acf: my_field}} to get the field from a specific post?

And how is this a better solution to just using ACF’s native shortcodes?

Hi,

Getting a field with a specific post is not possible with the current integration.

We made it that way as to prevent any conflicts with other shortcodes.

You can create your own shortcode, for example add the code below in your child theme’s functions.php file

  function my_acf_shortcode($atts) {
      extract( shortcode_atts( array(
        'id'        => '',
        'field'     => '',
       
      ), $atts ) );
      
      $data = get_field($field, $id);
      return $data;
}
add_shortcode('my_acf', 'my_acf_shortcode');

You can then use [my_acf field="my_field" id="457"] to display your field value.

Please note that the code provided above serves as a guide only and is to help you in getting started. Further customization from here would be getting into custom development, which is outside the scope of support we can offer.
If you need more in depth changes, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities.

Thank you for understanding

Thank you.

So, I built my own shortcode and I’m sharing it here in case anybody is interested in it. I’m sure it’s not the cleanest code ever written as I’m not very experienced in PHP.

This shortcut takes the values of two of my ACF fields (project title, a plain text field, and project type, a checkbox field which can contain multiple values) of a specific post defined with the post ID in the shortcode. In addition, it requests the post’s permalink and its featured image URL using built-in WordPress functions. Then it uses these 4 values to create the HTML code I wanted to include in a Content Area element in the PRO page builder.

function fd_portfolio_item_shortcode( $atts ) {

	// Shortcode attribute to enter the post ID
	$atts = shortcode_atts( array(
	        'id' => '',
	), $atts );

	// Variables
	$permalink 			= get_permalink( $atts['id'] );                       // the post's permalink
	$featured_image_url = get_the_post_thumbnail_url( $atts['id'], 'large' ); // the post's featured image url
	$acf_project_title 	= get_field( 'homepage_title', $atts['id'] );         // the post's ACF project title field value
	$acf_project_types	= get_field( 'project_type', $atts['id'] );           // the post's ACF project type field value

	// Generating the content
	if( $featured_image_url ): ?> <!-- HTML output if there's a featured image -->
		<a class="pwi-container" href="<?php echo $permalink; ?>">
  			<div class="pwi-image" style="background-image: url('<?php echo $featured_image_url; ?>');"></div>
  			<div class="pwi-text-container">
				<p class="pwi-secondary-text"><?php echo implode ( ", ", $acf_project_types ); ?></p>
				<p class="pwi-primary-text"><?php echo $acf_project_title; ?></p>
			</div>
		</a>
	<?php else: ?> <!-- HTML output if there's no featured image -->
		<a class="pni-container" href="<?php echo $permalink; ?>">
  			<p class="pni-secondary-text"><?php echo implode ( ", ", $acf_project_types ); ?></p>
  			<p class="pni-primary-text"><?php echo $acf_project_title; ?></p>
		</a>
	<?php endif;

}
// Register the Shortcode
add_shortcode( 'fd-portfolio-item', 'fd_portfolio_item_shortcode' );

Now I can just use the shortcode [fd-portfolio-item id="123"] (replace 123 with the portfolio post ID) in a Content Area element to show my content. It works very well, but feel free to make suggestions on how to improve my code.

Hi Patrick,

All shortcodes should return the processed value and it’s Wordpress standard https://codex.wordpress.org/Shortcode_API.

Please change it to this

function fd_portfolio_item_shortcode( $atts ) {

	// Shortcode attribute to enter the post ID
	$atts = shortcode_atts( array(
	        'id' => '',
	), $atts );

	// Variables
	$permalink 			= get_permalink( $atts['id'] );                       // the post's permalink
	$featured_image_url = get_the_post_thumbnail_url( $atts['id'], 'large' ); // the post's featured image url
	$acf_project_title 	= get_field( 'homepage_title', $atts['id'] );         // the post's ACF project title field value
	$acf_project_types	= get_field( 'project_type', $atts['id'] );           // the post's ACF project type field value

ob_start();

	// Generating the content
	if( $featured_image_url ): ?> <!-- HTML output if there's a featured image -->
		<a class="pwi-container" href="<?php echo $permalink; ?>">
  			<div class="pwi-image" style="background-image: url('<?php echo $featured_image_url; ?>');"></div>
  			<div class="pwi-text-container">
				<p class="pwi-secondary-text"><?php echo implode ( ", ", $acf_project_types ); ?></p>
				<p class="pwi-primary-text"><?php echo $acf_project_title; ?></p>
			</div>
		</a>
	<?php else: ?> <!-- HTML output if there's no featured image -->
		<a class="pni-container" href="<?php echo $permalink; ?>">
  			<p class="pni-secondary-text"><?php echo implode ( ", ", $acf_project_types ); ?></p>
  			<p class="pni-primary-text"><?php echo $acf_project_title; ?></p>
		</a>
	<?php endif;

return ob_get_clean();

}
// Register the Shortcode
add_shortcode( 'fd-portfolio-item', 'fd_portfolio_item_shortcode' );

Returning the value makes sure that the caller of the shortcode will still able to catch or capture the processed data instead of directly outputting it.

Cheers!

Thanks for the tip

You’re welcome! :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.