I have created a custom shortcode and can get the information to output, however it does not show up where I have placed it in the content hierarchy - it always prints at the top of the post/page. Any clue as to why this may be happening?
in my 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");
Since you are using the_field method, I assume you use ACF plugin.
You should use get_field
instead of the_field
since the_field
will output the specified field.
function sc_pdf($atts, $content = null) {
$pdfname = get_field('pdf_title');
... etc
To move your shortcode around, don't use echo. if you place the shortcode in your doc in the first example it will always float to the top. in the second if I place it at the bottom it will appear at the bottom.
My shortcode is [showpod]
CODE THAT YOU CANT PLACE ANYWHERE
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 REVISED CODE YOU CAN PLACE ANYWHERE: -
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');
Always use "return" instead echo.
You'll able to get data in proper location.