Wordpress PHP 短代码,get_the_content by id 不会显示自定义 post 类型的内容

Wordpress PHP shortcode, get_the_content by id won't display custom post type content

目前正在进行一个项目,我想通过短代码显示员工的名片,例如 [person id="1"] 或 [person name="bob"]

我为员工创建了一个单独的 post 类型,其中 title=name,content=contact info 和 thumbnail=image。

问题是标题和缩略图显示了正确的员工数据,但是get_the_content($post_id)显示了我第一个post "Welcome to ...! This is your first post...".

的内容
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) 显示自定义 post 内容?

您的代码似乎有点错误。

// 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/

首先:get_the_content() 不接受 Post-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' );

此代码示例尚未准备好使用。