短代码出现在内容的顶部而不是我需要的地方

时间:2012-05-02 01:45:14

标签: wordpress shortcode

我知道这可能是一个回归问题。所以我把内容分开了,一个叫做thelist的函数,另一个是返回它的实际函数。该代码遵循这个问题。

实际的短代码有效,但内容显示在其他内容之前的顶部。我认为now_include_post返回会修复它,但事实并非如此。有人可以帮忙吗?

function thelist() {
if (have_posts()) : while (have_posts()) : the_post();
?>  
        <div id="post-<?php the_ID(); ?>"  <?php post_class('thumb'); ?>>
            <a href="<?php the_permalink() ?>" class="thumb-link">
            <?php
    if (!post_password_required())  {
                    if (has_post_thumbnail()) {
                        the_post_thumbnail();
                    }
                } else {
                    ?>
                    <img src="<?php bloginfo('template_url') ?>/img/locked.png"  />
        <?php } ?>
            </a>
            <h2>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
            </h2>
        </div>
<?php /* end post */ ?>
<?php
    endwhile;
    endif;
    wp_reset_query();
    }
    ?>
    <?php

function now_include_post($atts) {
$thepostid = intval($atts[id]);
query_posts("p=$thepostid");
$output .= thelist();
return $output;
}

2 个答案:

答案 0 :(得分:25)

您希望返回所有文本,而不是在转义PHP时输出它。

在thelist()函数开始时,使用

启动输出缓冲区
ob_start();

然后在结束时关闭此缓冲区并使用

返回其内容
return ob_get_clean();

这将返回内容而不是直接回复它,这是你想要在WP短代码中做的事情

PHP information on Output Buffering Functions

答案 1 :(得分:-1)

我有这个短代码,即使我将短代码放在页面内容的末尾(在wordpress中),它总是出现在页面顶部,请提出任何建议。

function ss_framework_services_sc( $atts, $content = null ) {

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

global $post;

    $args = array(  'name' => esc_attr( $id ),
                    'post_type' => 'services',
                    'posts_per_page' => '1'

                 );

     query_posts( $args );


if( have_posts() ) while ( have_posts() ) : the_post(); ?>

<div class="services-tabs">
    <div class="board">


          <div class="idTabs">
                <div class="tabs-images">

                      <ul> 

                                  <?php $postslist = get_posts('post_type=services&numberposts=6&order=DESC'); foreach ($postslist as $post) : setup_postdata($post); ?>

                                              <li>
                                                    <a href="#<?php the_ID();?>">


                                                                 <img src="<?php bloginfo('template_directory'); ?>/js/cache/timthumb.php?src=<?php $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full"); echo $imgsrc[0]; ?>&w=120&h=120"alt="<?php the_title(); ?>" class="footer-thumb"    />

                                                                <div class="circle">
                                                                    <p class="service-title"><?php the_title() ?></p>
                                                                </div> 


                                                    </a>
                                              </li>

                                  <?php endforeach; ?>


                      </ul> 
                </div>
          </div>


          <div class="inner" > 

                  <?php $postslist = get_posts('post_type=services&numberposts=6&order=DESC'); foreach ($postslist as $post) : setup_postdata($post); ?>

                  <div class="result" id="<?php the_ID();?>">

                       <?php the_content(); ?>

                  </div>   
                  <?php endforeach; ?>
          </div><!--inner-->



</div><!--board-->
</div>
<?php

endwhile;

wp_reset_query();


return $output;

}

add_shortcode('services', 'ss_framework_services_sc');
相关问题