我在这里尝试了所有主题的解决方案,但没有找到解决方案。我为一个短代码制作了一个插件。我的问题是插件代码总是加载在内容之上。我已经发现它来自于echo
. 相反,我应该使用return
. 但只要我更换echo
插件停止工作;(
如果有人能帮助我,那就太好了。
非常感谢。
function sublime_video_playlist() {
wp_enqueue_script(\'sublime_video\', \'\' .plugins_url( \'/js/sublime_playlist.js\' , __FILE__ ). \'\');
global $post;
$attachments = get_children( array(
\'post_parent\' => $pageChild->ID,
\'post_type\' => \'attachment\',
\'numberposts\' => 1, // show all -1
\'post_status\' => \'inherit\',
\'post_mime_type\' => \'video\',
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ASC\'
) );
if ( $attachments ) {
echo \'<div class="sp shadow"><img src="/content/themes/maxx-wp/images/large-seperator.png"></div>\';
echo \'<h3>Videos</h3>\';
echo \'<div id="playlist1" class="sv_playlist">\';
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'video\',
\'numberposts\' => null,
\'post_status\' => null,
\'orderby\' => \'title\',
\'post_parent\' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo \'<div class="video_wrap">\';
echo \'<video width="568" height="320" id="video\';
echo $attachment->ID;
echo \'" data-settings="autoresize:fit" poster="\';
echo \'" preload="true">\';
echo \'<source src="\';
echo wp_get_attachment_url ($attachment->ID);
echo \'" /></video>\';
echo \'</div>\';
}
echo \'<ul class="thumbs">\';
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'video\',
\'numberposts\' => null,
\'post_status\' => null,
\'orderby\' => \'title\',
\'post_parent\' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo \'<li style="padding:0px;" id="thumbnail_video\';
echo $attachment->ID;
echo \'"> <a href=" "> <img style="height:100%" alt="" src="\';
echo wp_get_attachment_url( get_post_thumbnail_id($post->ID));
echo \'" width="144" height="71" /> <span class="play" /> </a> </li>\';
}
}
echo \'</ul></div>
<div style="margin-bottom: -19px; float:right">\';
if(function_exists(\'render_tnwsc_button\')) {
echo render_tnwsc_button();
}
echo \'</div>\';
}
}
}
add_shortcode(\'video_playlist\', \'sublime_video_playlist\'); ?>
SO网友:Chris_O
在您的情况下,最好保留echo并使用输出缓冲。
添加ob\\u start();到函数的第一行。
在函数末尾添加
$output = ob_get_clean();
return $output;
此外,您不能将脚本从短代码中排队。它运行到很晚,永远不会加载。只需创建一个添加到wp\\u enqueue\\u scripts挂钩的函数。如果不希望在每个页面上加载脚本,请添加一个条件,以检查找到的短代码。
function c3m_find_short_code( $shortcode ) {
if ( ! is_singular() )
return false;
$post_to_check = get_post( get_the_ID() );
if ( stripos( $post_to_check->post_content, \'[\' . $shortcode ) !== false )
return true;
return false;
}
function my_shortcode_enqueue() {
if ( c3m_find_short_code( \'shortcode_name_here\' );
wp_enqueue_script( \'blah\', \'/path_to_blah.js\' );
}
add_action( \'wp_enqueue_scripts\', \'my_shortcode_enqueue\' );