2012-04-17 29 views
1

我已经创建了自定义短代码并可以获取要输出的信息,但是它没有显示我将它放在内容层次结构中的位置 - 它总是在帖子的顶部打印/页。任何线索,为什么这可能会发生?自定义短代码总是出现在内容顶部

在我的functions.php:

function sc_pdf($atts, $content = null) { 
    $pdfname = the_field('pdf_title'); 
    $pdfimage = the_field('pdf_file'); 
    $pdflink = the_field('pdf_thumbnail'); 
    return '<p>'.$pdfname.'</p><p>'.$pdfimage.'</p><p>'.$pdflink.'</p>'; 
} 
add_shortcode("peedeef", "sc_pdf"); 
+1

请添加说明输出实际位置的HTML输出。 – hakre 2012-04-17 09:30:10

+0

**注意mods **:这个问题*可能*更适合WPSE。 – 2012-04-18 18:14:35

回答

3

由于您使用the_field方法,我假设你使用ACF插件。

您应该使用get_field而不是the_field,因为the_field将输出指定的字段。

function sc_pdf($atts, $content = null) { 
    $pdfname = get_field('pdf_title'); 
    ... etc 
+0

Ahhhh我明白了 - 完美。现在正在工作。 – Mike 2012-04-17 11:40:25

+0

我已更新原始代码以反映您的建议,@soju。 – Mike 2012-04-17 11:43:31

+1

除了:使用该编辑,后来的观众不会看到有问题的代码,而只是*校正的*代码 - 因此,不会理解问题。 – 2012-04-18 18:14:01

2

移动你的短代码,请不要使用回声。 如果您在第一个示例中将简码放入文档中,它将始终浮动到顶部。如果我把它放在底部,它会出现在底部。

我的简码为[showpod]

CODE,你不能在任何场合

function makepod($atts) { 
echo "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>"; 
$args = array('numberposts' => '6'); 
$recent_posts = wp_get_recent_posts($args); 
foreach($recent_posts as $recent){ 
echo '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>'; 
} 
echo "</div>"; 
} 
add_shortcode('showpod', 'makepod'); 

AND NOW修改后的代码可以随时随地的地方: -

function makepod($atts) { 
$cat = "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>"; 
$args = array('numberposts' => '6'); 
$recent_posts = wp_get_recent_posts($args); 
foreach($recent_posts as $recent){ 
$cat.= '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>'; 
} 
$cat .= "</div>"; 
return $cat; 
} 
add_shortcode('showpod', 'makepod'); 
2

始终使用“返回”而不是回声。

您将能够在正确的位置获取数据。

相关问题