Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get this shortcode working with custom fields WordPress

  • SOLVED

Hello,

I'm trying to get shortcodes to work with custom fields. My custom field's name is "DOI". Usually, to insert custom fields into shortcodes in the post template I use the following code:

<?php $var = get_post_meta($post->ID, 'DOI', true);
if ($var == '')
{ }
else { echo do_shortcode( '[shortcode example="' . $var . '"]'); } ?>


Now, this usually works. However, the shortcode I'm trying to use is this:

[cite source='doi']DOI[/cite]. For example, if DOI = <strong>10.1021/jf904082b</strong>,then the shortcode would be [cite source='doi']<strong>10.1021/jf904082b</strong>[/cite].

As you can see, the shortcode is in a strange format, and doesn't work with the code I first mentioned.

I tried this and other minor variations, but they all failed:

<?php $var = get_post_meta($post->ID, 'DOI', true);
if ($var == '')
{ }
else { echo do_shortcode( '[cite source='doi']' . $var . '[/cite]'); } ?>



Any ideas?

Answers (2)

2015-07-01

timDesain Nanang answers:

you should use double quote instead of single quote.

change this part:
echo do_shortcode( '[cite source='doi']' . $var . '[/cite]');
to
echo do_shortcode( '[cite source="doi"]' . $var . '[/cite]');


neliti comments:

It <em>almost</em> works, but the function of the shortcode still isn't working with that code. The shortcode only seems to work if it's placed in the post content body, not in the template alone. Is there any way it can be automatically inserted in the post body, prior to the rest of the content?

Thanks


timDesain Nanang comments:

yep, you can use the_content filter,
wait a minute, i'll write the code


timDesain Nanang comments:

yep, you can use the_content filter,
wait a minute, i'll write the code


timDesain Nanang comments:

put the following code into theme's functions.php
add_filter('the_content', 'wpq_cite_content');
function wpq_cite_content($content) {

$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
$content .= '<blockquote><cite class="doi">'.$value.'</cite></blockquote>';

return $content;
}


timDesain Nanang comments:

sorry, blockquote is not accepted inside code tag.

put the following code into theme's functions.php
add_filter('the_content', 'wpq_cite_content');
function wpq_cite_content($content) {

$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
$content .= '<cite class="doi">'.$value.'</cite>';

return $content;
}


neliti comments:

Unfortunately this just spits out the custom field value in italics in the post body.

Do I need to include anything in single.php or functions.php?

Thanks.


neliti comments:

The shortcode only ever seems to work if it's used in the post editor of the WP backend... not the template files.


timDesain Nanang comments:

put the following code into theme's functions.php

add_filter('the_content', 'wpq_cite_content');
function wpq_cite_content($content) {

if(!is_admin()){
$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
$content = do_shortcode( '[cite source="doi"]' . $value . '[/cite]') . $content;
}

return $content;
}


neliti comments:

Awesome. This code works, much like the other answer. I guess the money should be split.


neliti comments:

Oh. But what if I want it only for a specific single template? I only want it for the template file called single-doi.php


neliti comments:

And also, is there a way to use this code one div before the post content?


timDesain Nanang comments:

what is the conditional tag ([[LINK href="https://codex.wordpress.org/Conditional_Tags"]]https://codex.wordpress.org/Conditional_Tags[[/LINK]]) of single-doi.php?

<blockquote>And also, is there a way to use this code one div before the post content?</blockquote>

you can try this code:

add_filter('the_content', 'wpq_cite_content');
function wpq_cite_content($content) {

if(!is_admin() AND in_category('doi')){ // added the conditional tag
$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
$content = '<div class="here">'. do_shortcode( '[cite source="doi"]' . $value . '[/cite]') .'</div>'. $content;
}

return $content;
}


neliti comments:

single-doi.php is just a slightly altered single.php. It only shows when there is the tag "DOIhidden".

So

if (has_tag('DOIhidden'))

???


neliti comments:

Almost there. It's working. But it's still not a div above the post content. I'd like it above <div class="post-content">


timDesain Nanang comments:

i see,
since the mesocolumn theme has "do_action( 'bp_before_post_content' );" hook,
then here is the final code:

add_action('bp_before_post_content', 'wpq_cite_content');
function wpq_cite_content($content) {

if(!is_admin() AND has_tag('DOIhidden')){
$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
$content = '<div class="here">'. do_shortcode( '[cite source="doi"]' . $value . '[/cite]') .'</div>'. $content;
}

return $content;
}


and add_filter( 'the_content', 'x'); can not be applied.


timDesain Nanang comments:

sorry, I mean:
add_action('bp_before_post_content', 'wpq_cite_content');
function wpq_cite_content() {

if( has_tag('DOIhidden') ){
$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
echo '<div class="here">'. do_shortcode( '[cite source="doi"]' . $value . '[/cite]') .'</div>';
}
}


neliti comments:

so close. It's still not appearing above post content though...


neliti comments:

OK. It's above the post content, but the shortcode isn't working again!


timDesain Nanang comments:

it's strange.
I have created a simple cite shortcode, and the code working properly.


add_shortcode( 'cite', 'wpq_sc_cite' );
function wpq_sc_cite( $atts, $content = null ) {

extract(shortcode_atts(array(
'source' => 'doi',
), $atts ));

return 'Cite: <cite class="'.$source.'">'.$content.'</cite>';

}



add_action('bp_before_post_content', 'wpq_cite_content');
function wpq_cite_content() {
if( is_single() AND has_tag('DOIhidden') ){
$value = get_post_meta(get_the_ID(), 'DOI', true);
if(!empty($value))
echo '<div class="here-is-doi">'. do_shortcode( '[cite source="doi"]' . $value . '[/cite]') .'</div>';
}
}