2012-05-02 18 views
8

Tôi biết rằng đó có thể là sự cố trả về. Vì vậy, tôi chia các nội dung lên, một trong một chức năng gọi là thelist và một là một chức năng thực tế trả lại nó. Mã theo sau câu hỏi.Mã ngắn sắp ra ở đầu nội dung thay vì ở vị trí mà tôi yêu cầu nó

Mã vạch thực tế hoạt động, ngoại trừ nội dung xuất hiện ở trên cùng trước phần còn lại của nội dung. Tôi nghĩ rằng lợi nhuận now_include_post sẽ sửa chữa nó, tuy nhiên nó không. Ai có thể giúp được không?

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; 
} 

Trả lời

24

Bạn muốn trả lại tất cả văn bản thay vì xuất ra sau đó và ở đó khi bạn thoát PHP.

Vào đầu thelist của bạn() chức năng bắt đầu một bộ đệm đầu ra với

ob_start(); 

Sau đó, ở cuối đóng bộ đệm này và gửi lại nội dung của nó với

return ob_get_clean(); 

Điều đó sẽ trả về nội dung chứ không phải hơn echo nó ngay lập tức, đó là những gì bạn muốn làm trong trường hợp của một shortcode WP

PHP information on Output Buffering Functions

+1

Điều đó thực sự đặc biệt. Cảm ơn bạn rất nhiều vì sự giúp đỡ của bạn và liên kết đó! – user1368968

+0

Đó là năm 2014 và sau 2 năm câu trả lời của bạn đã cứu tôi :) Cảm ơn! –

+0

Nó làm việc cho tôi. Cảm ơn –

-1

Tôi có shortcode này và nó xuất hiện luôn ở đầu trang mặc dù tôi đặt shortcode ở cuối nội dung trang (trong wordpress), bất kỳ đề nghị nào.

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'); 
Các vấn đề liên quan