2016-10-12 25 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)不接受Post-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)); 

它應該是這樣的(基於這樣的假設,即$ ATT以元素從短碼參數取):

// 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'); 

此代碼示例不準備使用。

相關問題