Wordpress短代码始终位于页面顶部
我使用以下短代码在页面上显示自定义帖子:Wordpress短代码始终位于页面顶部,wordpress,shortcode,Wordpress,Shortcode,我使用以下短代码在页面上显示自定义帖子: add_shortcode( 'page-section', 'page_section_shortcode' ); function page_section_shortcode( $atts ) { global $post; $post_slug=$post->post_name; $a = shortcode_atts( array( 'post-name' => 'qwerty',
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div style="background-color: <?php echo $a['bg-color']; ?>" id="<?php global $post; $post_slug=$post->post_name; echo $post_slug; ?>" class="page-section">
<div class="row">
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
add_-shortcode('page-section','page-section_-shortcode');
功能页\节\短代码($atts){
全球$员额;
$post\u slug=$post->post\u名称;
$a=短码_附件(阵列)(
“职位名称”=>“qwerty”,
“背景颜色”=>“白色”
)(港币),;
$post\u slug=$post->post\u名称;
$post_name=$a['post-name'];
$query=新的WP\U查询(数组(
“发布类型”=>“页面部分”,
'name'=>$post\u name,
) );
如果($query->have_posts()){?>
将您的代码替换为此代码。此代码返回生成的html,因此不会在页面顶部出现回音
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
$returnhtml = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
global $post;
$returnhtml .= '<div style="background-color: '. $a['bg-color'].'" id="'.$post->post_name.'" class="page-section">';
$returnhtml .= '<div class="row">'.get_the_content().'</div>';
$returnhtml .= '</div>';
endwhile;
wp_reset_postdata();
return $returnhtml;
}
}
add_-shortcode('page-section','page-section_-shortcode');
功能页\节\短代码($atts){
全球$员额;
$post\u slug=$post->post\u名称;
$a=短码_附件(阵列)(
“职位名称”=>“qwerty”,
“背景颜色”=>“白色”
)(港币),;
$post\u slug=$post->post\u名称;
$post_name=$a['post-name'];
$query=新的WP\U查询(数组(
“发布类型”=>“页面部分”,
'name'=>$post\u name,
) );
$returnhtml='';
如果($query->have_posts()){
而($query->have_posts()):$query->the_post();
全球$员额;
$returnhtml.='';
$returnhtml.=''。获取内容();
$returnhtml.='';
结束时;
wp_reset_postdata();
return$returnhtml;
}
}
将您的代码替换为此代码。此代码返回生成的html,因此不会在页面顶部回显
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
$returnhtml = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
global $post;
$returnhtml .= '<div style="background-color: '. $a['bg-color'].'" id="'.$post->post_name.'" class="page-section">';
$returnhtml .= '<div class="row">'.get_the_content().'</div>';
$returnhtml .= '</div>';
endwhile;
wp_reset_postdata();
return $returnhtml;
}
}
add_-shortcode('page-section','page-section_-shortcode');
功能页\节\短代码($atts){
全球$员额;
$post\u slug=$post->post\u名称;
$a=短码_附件(阵列)(
“职位名称”=>“qwerty”,
“背景颜色”=>“白色”
)(港币),;
$post\u slug=$post->post\u名称;
$post_name=$a['post-name'];
$query=新的WP\U查询(数组(
“发布类型”=>“页面部分”,
'name'=>$post\u name,
) );
$returnhtml='';
如果($query->have_posts()){
而($query->have_posts()):$query->the_post();
全球$员额;
$returnhtml.='';
$returnhtml.=''。获取内容();
$returnhtml.='';
结束时;
wp_reset_postdata();
return$returnhtml;
}
}
函数头_通知()
{
echo'任何html都会出现在这里;
}
添加动作(“工作包头”、“标题通知”);
函数头_通知()
{
echo'任何html都会出现在这里;
}
添加_action('wp_head','header_notification');
您没有在body中的start shortcode函数的顶部启动ob_start();也没有在body中的start shortcode函数的顶部启动ob_start()。因此,我再次尝试了这个解决方案,在一个嵌入了短代码的节上,它没有呈现短代码内容(只是它的文本版本)“user5200704”开始的答案ob_start();对我来说很有用-如果你把它作为答案放进去,我会接受它。@mcneela86:嗨,你能提供屏幕截图显示什么和应该显示什么吗?我的意思是ob_start();可以解决问题,但不是强制性的。可能还有其他问题。谢谢replying@mcneela86:您也可以在internet上看到短代码的示例…例如:。在此链接中,他们没有使用ob_start();因此我在带有嵌入式短代码的节上再次尝试了此解决方案,但它不会呈现短代码内容(只是一个文本版本。)“user5200704”开始ob_start()的答案对我来说很有效-如果你把它作为一个答案,我会接受它。@mcneela86:嗨,你能提供屏幕截图显示什么和应该显示什么吗?我的意思是ob_start();可以解决问题,但不是强制性的。可能还有其他问题。谢谢replying@mcneela86:您也可以在internet上看到短代码的示例…例如:。在此链接中,他们没有使用ob_start();