Community Forums › Forums › Archived Forums › Design Tips and Tricks › can't get shortcodes in functions.php to work
Tagged: Custom fields, do_shortcode, shortcode
- This topic has 9 replies, 3 voices, and was last updated 7 years, 10 months ago by
Brad Dalton.
-
AuthorPosts
-
March 22, 2017 at 8:17 am #203650
jlknauff
MemberI'm stuck...this outputs the shortcode text rather than the rendered result. What am I doing wrong?
function practice_area_people() { if ( is_page( 'antitrust-trade-regulation' ) ) { echo '<div class="practice-area-people">'; genesis_custom_field('antitrust-trade-regulation-people'); echo '</div>'; } } add_action( 'genesis_entry_footer', 'practice_area_people' );
March 22, 2017 at 8:59 am #203654Brad Dalton
ParticipantGot stuck on this sometime ago myself and fixed it using this solution.
March 22, 2017 at 9:08 am #203657Victor Font
ModeratorIs the value of the antitrust-trade-regulation-people custom field a shortcode? If so, wrap the custom field output in do_shortcode().
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 22, 2017 at 9:21 am #203658jlknauff
MemberBrad, nope...I've tried using do_shortcode as well with no luck. Even modified to use your example with no success.
function practice_area_people() { if ( is_page( 'antitrust-trade-regulation' ) ) { echo '<div class="practice-area-people">'; $custom_field = genesis_custom_field('antitrust-trade-regulation-people'); if ( $custom_field ) { echo do_shortcode( $custom_field ); } echo '</div>'; } } add_action( 'genesis_before_content', 'practice_area_people' );
March 22, 2017 at 9:24 am #203661jlknauff
MemberHey Victor, that's one of the things I've been trying, but no dice.
March 22, 2017 at 9:32 am #203665jlknauff
MemberHere's the odd thing...
I'm using a shortcode that I know works for testing. (CF7)
This simply prints the shortcode on the screen:
$custom_field = genesis_custom_field('antitrust-trade-regulation-people'); echo do_shortcode( $custom_field );
However, this outputs the results:
$custom_field = genesis_custom_field('antitrust-trade-regulation-people'); echo do_shortcode( '[contact-form-7 id="1667" title="Contact form 1"]' );
Does that make any sense?
March 22, 2017 at 9:37 am #203666Victor Font
ModeratorTry genesis_get_custom_field instead of genesis_custom_field. The first returns the value of the field. The second writes it to HTML. It could be the difference.
I also suggest using the Kint debugger so you can see the values returned in the function. https://wordpress.org/plugins/kint-php-debugger/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 22, 2017 at 9:47 am #203668Brad Dalton
ParticipantYes, genesis_get_custom_field is the correct function for genesis which is why your code isn't working.
I think its also better to use get_post_meta
March 22, 2017 at 10:09 am #203671jlknauff
MemberWhen I use
if ( is_page( 'antitrust-trade-regulation' ) ) { echo '<div class="practice-area-people">'; $custom_field = genesis_get_custom_field('antitrust-trade-regulation-people'); echo do_shortcode( $custom_field ); echo '</div>'; }
it returns nothing. 🙁
I've also tried:
echo do_shortcode( get_post_meta( 223, $key = 'antitrust-trade-regulation-people', $single = true ) );
which also returns no results.
March 22, 2017 at 10:46 am #203673Brad Dalton
ParticipantThis works
add_action( 'genesis_entry_footer', 'genesis_custom_field_with_shortcode' ); function genesis_custom_field_with_shortcode() { $custom_field = genesis_get_custom_field('antitrust-trade-regulation-people'); if ( $custom_field ) { echo do_shortcode( $custom_field ); } }
Assumes you have created a custom field named antitrust-trade-regulation-people and added your shortcode to the value field for that key.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.