I'm trying to output the values of two custom fields created by ACF plugin in a custom post type. I actually need this to be displayed in a popup window. wordpress popup plugins don't support php code, only shortcodes can be called from the content editor. So I'm trying to create a shortcode to be used in the editor of the popup to display the values of the custom fields.
I know that we can generate shortcode [cite]
using this code in theme functions.php
function cite_shortcode() {
}
add_shortcode( 'cite', 'cite_shortcode' );
But i couldn't figure out how to add the php code to that code. I tried to do something like:
function cite_shortcode() {
<div>
<?php
$object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );
if ( $object_terms ) {
$res = '';
foreach ( $object_terms as $term ) {
if ( $term->parent ) {
$res .= $term->name . ', ';
}
}
echo rtrim($res,' ,');
}
?>), pp: <?php the_field('first_page'); ?>-<?php the_field('last_page'); ?>
</div>
}
add_shortcode( 'cite', 'cite_shortcode' );
But it didn't work. it shows:
Parse error: syntax error, unexpected
So, my questions are:
Best regards
I'm trying to output the values of two custom fields created by ACF plugin in a custom post type. I actually need this to be displayed in a popup window. wordpress popup plugins don't support php code, only shortcodes can be called from the content editor. So I'm trying to create a shortcode to be used in the editor of the popup to display the values of the custom fields.
I know that we can generate shortcode [cite]
using this code in theme functions.php
function cite_shortcode() {
}
add_shortcode( 'cite', 'cite_shortcode' );
But i couldn't figure out how to add the php code to that code. I tried to do something like:
function cite_shortcode() {
<div>
<?php
$object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );
if ( $object_terms ) {
$res = '';
foreach ( $object_terms as $term ) {
if ( $term->parent ) {
$res .= $term->name . ', ';
}
}
echo rtrim($res,' ,');
}
?>), pp: <?php the_field('first_page'); ?>-<?php the_field('last_page'); ?>
</div>
}
add_shortcode( 'cite', 'cite_shortcode' );
But it didn't work. it shows:
Parse error: syntax error, unexpected
So, my questions are:
Best regards
We've built shortcodes in the past using concatenating variables.
To paraphrase the link above, you should output your PHP as such.
$output = '<p>';
$output .= '<strong>' . $content . '</strong>';
$output .= '</p>';
return $output;
Note, see the .=
variable concatenation.
Just in case anyone will search for this in the future...
The proper way to output php value through a shortcode is this one:
add_shortcode('shortcode_name', 'shortcode_callback');
function shortcode_callback( $atts = array(), $content = null ) {
$output = "Add your PHP here!!!";
return $output;
}
Greetings from sunny Greece!!!
- how can i make that code work?
function cite_shortcode($atts) {
$output = '<div>';
$object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );
if ( $object_terms ) {
$res = '';
foreach ( $object_terms as $term ) {
if ( $term->parent ) {
$res .= $term->name . ', ';
}
}
$output .= rtrim($res,' ,');
}
$output .= 'pp: '.get_the_field('first_page') . '-' . get_the_field('last_page');
$output .= '</div>';
return $output ;
}
add_shortcode( 'cite', 'cite_shortcode' );
- is it possible to put the php code in cite.php file and output its values via a shortcode generated in functions.php? and how to do that?
In this case, you can inclde your php file within short code, something similar to this code
function cite_shortcode($atts) {
$output = '';
ob_start();
include "yourphpfile.php"; // Replace with exact location of php file
$output .= ob_get_clean();
return $output ;
}
add_shortcode( 'cite', 'cite_shortcode' );
I hope this helps.