2016-10-12 17 views
2

現在、私は従業員の名刺を短いコードで表示したいプロジェクトに取り組んでいます。例えば、[person id="1"]または[person name="bob"]WordpressのPHPショートコード、idによるget_the_contentはカスタムポストタイプのコンテンツを表示しません

従業員のために、title = name、content =連絡先情報、およびthumbnail = imageという別の投稿タイプを作成しました。

問題は、タイトルとサムネイルが正しい従業員データを示していますが、get_the_content($post_id)は私の最初の投稿の内容を表示しています。「ようこそ!これはあなたの最初の投稿です...」

function display_person($atts, $content){ 

    extract(shortcode_atts(array(
     'posts_per_page' => '1', 
     'post_type' => 'person', 
     'post_id' => null, 
     'caller_get_posts' => 1) 
    , $atts)); 

    global $post; 

    $posts = new WP_Query($atts); 
    $output = ''; 
    if ($posts->have_posts()) 
     while ($posts->have_posts()): 
      $posts->the_post(); 
      $out = '<div class="col-lg-6 col-md-12 col-sm-12 col-xs-12"> 
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 person-container margin-top"> 
    <div class="col-lg-6 col-md-6 col-sm-6 hidden-xs person-thumbnail no-padding"> 
      '.get_the_post_thumbnail($post_id, 'thumbnail-person').' 
     </div> 
     <div class="hidden-lg hidden-md hidden-sm col-xs-4 person-thumbnail no-padding"> 
      '.get_the_post_thumbnail($post_id, 'thumbnail-person-small').' 
     </div> 
     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-8 person no-padding"> 
      <h3>'.get_the_title($post_id).'</h3> 
      '.get_the_content($post_id).' 
     </div> 
    </div> 
</div>'; 
      $out .='</div>'; 
    endwhile; 
    else 
    return; 

    wp_reset_query(); 
    return html_entity_decode($out); 
} 
add_shortcode('person', 'display_person'); 

にはどうすればカスタムポストコンテンツを表示するget_the_content($post_id)を得るのですか?

+0

['get_the_content()'](https://codex.wordpress.org/Function_Reference/get_the_content)は投稿IDを引数として受け付けません –

答えて

1

コードに問題があるようです。

// this is your shortcode 
extract(shortcode_atts(array(
    'posts_per_page' => '1', 
    'post_type' => 'person', 
    'post_id' => null, 
    'caller_get_posts' => 1) 
, $atts)); 

それは($ ATTS'要素がショートパラメータから取られていることを、仮定に基づいて)次のようになります。

// you extract the parameters into variable names 
extract(shortcode_atts(array(
    'posts_per_page' => 'posts_per_page_var', 
    'post_type'  => 'post_type_var', 
    'post_id'   => 'post_id_var', 
    'caller_get_posts' => 'caller_get_posts_var') 
, $atts)); 

// creating the array of arguments for the query 
$new_atts = array(
    'posts_per_page' => $posts_per_page_var, 
    'post_type'  => $post_type_var, 
    'post_id'   => $post_id_var, 
    'caller_get_posts' => $caller_get_posts_var 
); 
$posts = new WP_Query($new_atts); 
// and go on with your code... 

それは、これはあなたの問題を解決すべきだと思います。

長いチュートリアル:まず

http://www.webdesignerdepot.com/2013/06/how-to-create-your-own-wordpress-shortcodes/

+0

ありがとうございます!完璧に働いた! – hogan

+0

$ content = apply_filters( 'the_content'、$ post-> post_content)も追加されました。 – hogan

+0

... $ post = get_post($ post_id)の直後。 – hogan

0

get_the_content()引数としてポストIDを受け付けません。

あなたのショートがちょうど一人のためであるならば、あなたはこのようにそれを行うことができます。

function display_person($atts) { 
    $atts = shortcode_atts(array(
     'id' => false, 
     'name' => false 
    ), $atts, 'person'); 


    if ($atts['id']) { 
     $person = get_post($atts['id']); 
    } else if ($atts['name']) { 
     // todo 
    } 

    $image = get_the_post_thumbnail($person->ID, 'medium'); 
    $name = $person->post_title; 
    $content = wpautop($person->post_content); 

    $output = ""; // todo 


    return $output; 
} 
add_shortcode('person', 'display_person'); 

このコード例では、使用する準備ができていません。

関連する問題