2016-06-24 5 views
0

의 상단에 나는 페이지에 사용자 정의 게시물을 표시하기 위해 사용하고있는 다음과 같은 단축 코드가있다 :워드 프레스 단축 코드는 항상 페이지

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

모든 것은 내가, 내가 내용을 설정할 수 있습니다 원하는 방식으로 작업한다 내 사용자 정의 게시물에 짧은 코드를 사용하여 해당 내용을 페이지로 가져옵니다.

짧은 코드로 가져온 콘텐츠는 항상 페이지의 맨 위에 있습니다. 한 행에 여러 개의 단축 코드를 사용하면 주문은 유지되지만 페이지의 다른 모든 내용은 하단에 표시됩니다 (모든 단축 코드 내용 아래에 있음).

나는 '에코'를 다른 stackoverflow 게시물에서 제안 된 것처럼 제거하려고했지만 내가 잘못하고있는 것을 발견하지 못하는 것 같습니다.

+1

ob_start()를 시작하지 않았습니다. 본문에서 시작 shortcode 함수의 맨 위에. – user5200704

답변

3

이 코드에 코드를 바꿉니다. 이 코드는 생성 된 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; 
    } 
} 
+0

그래서이 솔루션을 짧은 코드가 삽입 된 섹션에서 다시 시도했는데 단문 (텍스트 버전) 만 렌더링하지 않습니다. 'user5200704'대답을 시작하려면 ob_start(); 나를 위해 일해 왔습니다. 당신이 대답으로 대답한다면 그것을 받아 들일 것입니다. – mcneela86

+0

@ mcneela86 : 안녕하세요. 당신은 무엇이 나타나고 무엇이 나타나야하는지에 대한 스크린 샷을 제공 할 수 있습니까? 내 말은 ob_start()입니다. 문제를 해결할 수는 있지만 의무는 아닙니다. 다른 문제가있을 수 있습니다. 답장을 보내 주셔서 감사합니다 – Vidhi

+0

@ mcneela86 : 인터넷에서 shortcode의 예를 볼 수도 있습니다 ... 예 : https://www.sitepoint.com/custom-shortcodes-for-wordpress/. 이 링크에서는 ob_start()를 사용하지 않았습니다. – Vidhi

관련 문제