2017-12-09 6 views
0

, 나는 사용자 정의 단축 코드에 대해 다음 만들었습니다 Shortcode 출력에 사용자 정의 필드를 어떻게 포함합니까? 다른 개발자의 도움으로

add_shortcode('children' , 'display_custom_post_type_v1'); 

function display_custom_post_type_v1($atts) { 

$atts = shortcode_atts(array(
    'category' => '' 
), $atts); 

$categories = explode(',' , $atts['category']); 

$args = array(
     'post_type'  => 'children', 
     'post_status' => 'publish', 
     'orderby'  => 'title', 
     'order'   => 'ASC', 
     'posts_per_page'=> -1, 
     'tax_query'  => array(array(
          'taxonomy' => 'category', 
          'field'  => 'term_id', 
          'operator' => 'AND', 
          'terms'  => $categories 
         )) 
    ); 

    $string = ''; 
    $query = new WP_Query($args); 

    if(! $query->have_posts()) { 
     return false; 
    } 

    while($query->have_posts()){ 
     $query->the_post(); 
     $string .= '<div id="childWrapper"><div id="childImage"><a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a></div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>'; 
    } 
    wp_reset_postdata(); 

    return $string; 
} 

는 지금까지 내 응용 프로그램에 대해 정말 잘 작동하지만, 내가하고 싶으 하나 개의 추가 항목이 있습니다. 이러한 맞춤 게시물 유형에는 메타 박스 (맞춤 입력란)가 포함되어 있으며 출력에이 유형을 포함하고 싶습니다. 일반적으로 나는 다음과 같은 것을 사용합니다 :

<?php echo get_post_meta($post->ID, '_cd_birthdate', true); ?> 

그러나 shortcode 출력에 어떻게 포함 시킬지 생각할 수 없습니다. 이 줄을 시도했지만 div 영역 만 표시됩니다. 내용은 없습니다.

<div>' . get_post_meta($post->ID, '_cd_birthdate', true) . '</div> 

모든 의견을 크게 기뻐할 것입니다.

답변

0

'$ post-> ID'는 WP 루프에서만 작동합니다. 대신 'get_the_ID()'를 시도하십시오.

처럼 ...

<div>' . get_post_meta(get_the_ID(), '_cd_birthdate', true) . '</div> 

참조 this

관련 문제