2017-11-10 4 views
0

다음은 사용중인 코드입니다. 나는 몇 가지 시도했지만 그들 중 누구도 일하지 않았다. 어떤 아이디어?루프 외부의 특정 ID에서 콘텐츠 및 메타 데이터를로드하는 방법

<?php 
$id = 29; 
$post = get_post($id); 
$title = apply_filters('get_the_title', $post->post_title); 
$content = apply_filters('the_content', $post->post_content); 
$meta = get_post_meta($id, 'example_meta', true); 
echo "<h4 class='col-12'>", $title, "</h4>";     
echo "<h2 class='col-12 col-md-11 col-lg-8 py-5'>", get_the_subtitle($post), "</h2>"; 
echo $content; 
echo $meta; 
?> 
+0

무엇이 문제입니까, 무엇을하려하십니까? – madalinivascu

+0

"get_the_subtitle"이 WorPress의 핵심 기능인지 확실하지 않습니다. 당신이 그것을 만들지 않는 한. –

+0

@ FazleElahee 당신 말이 맞아요. 플러그인을 사용하고 있습니다. "WP 자막"이라고합니다. https://wordpress.org/plugins/wp-subtitle/ – neongrain

답변

0

이 코드는 테스트하지 않았으므로 작동 할 것입니다.

$id = 29; 
$post = get_post($id); 

if($post) { 
    $title = get_the_title($post->ID); 
    $content = do_shortcode($post->post_content); 
    $meta = get_post_meta($id, 'example_meta', true); // to make sure meta_key already exists for this posts. 
    echo "<h4 class='col-12'>", $title, "</h4>"; 
    echo "<h2 class='col-12 col-md-11 col-lg-8 py-5'>", get_the_subtitle($post), "</h2>"; 
    echo $content; 
    echo $meta; 
} 
+0

이 중 하나가 작동하지 않았다. meta_key가 맞는지 확인했지만 잘못되었습니다. – neongrain

1

원하는 게시물을 사용하여 게시물 데이터를 설정 한 다음 게시물 데이터를 재설정 할 수 있습니다.

<?php 
$id = 29; 
$post = get_post($id); 
setup_postdata($post); 
$title = apply_filters('get_the_title', $post->post_title); 
$content = apply_filters('the_content', $post->post_content); 
$meta = get_post_meta($id, 'example_meta', true); 
echo "<h4 class='col-12'>", $title, "</h4>";     
echo "<h2 class='col-12 col-md-11 col-lg-8 py-5'>", get_the_subtitle($post), "</h2>"; 
echo $content; 
echo $meta; 
wp_reset_postdata(); 
?> 
+0

그게 효과가 있습니다. –

+0

빠른 응답을 보내 주셔서 감사합니다. 슬프게도 그것은 나를 위해 일하지 않았다. 어떤 대안? – neongrain

+0

또한 "global $ post;"를 추가하십시오. before $ id = 29; – sticksu