특정 페이지의 컨텐츠 가져 오기 (ID 별)


14

다음과 같은 첫 페이지 템플릿을 만들었습니다.

여기에 이미지 설명을 입력하십시오

큰 Lorem Ipsum 블록 대신 해당 상자를 채우기 위해 특정 페이지에서 "발췌"(일부 문자 수)를 표시해야합니다.

페이지 내용을 문자열 형식으로 가져 와서 반향하여 특정 문자 수로 다듬을 수있는 방법은 무엇입니까?

답변:


22
<?php

// would echo post 7's content up until the <!--more--> tag
$post_7 = get_post(7); 
$excerpt = $post_7->post_excerpt;
echo $excerpt;

// would get post 12's entire content after which you
// can manipulate it with your own trimming preferences
$post_12 = get_post(12); 
$trim_me = $post_12->post_content;
my_trim_function( $trim_me );

?>

21

여기 있습니다!

<?php
$my_id = 5369;
$post_id_5369 = get_post($my_id);
$content = $post_id_5369->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>

4
코드의 기능과 질문에 어떻게 대답하는지 설명하십시오. 일부 사용자는 약간의 설명없이 코드를 이해하지 못할 수 있습니다.
— cybmeta

the_content필터 를 추가 한 방식이 정말 마음에 듭니다 . +1입니다.
— Mohammad Mursaleen

아름다운 작품!
— Charles Xavier

2

이 코드를 사용할 수 있습니다. 페이지 번호로 page_id = 19를 변경하십시오.

<?php $the_query = new WP_Query( 'page_id=19' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post();  ?>

                       <?php the_excerpt(); ?>


     <?php endwhile;?>

1
이 사이트에 오신 것을 환영합니다. 이것이 첫 번째 답변 인 것 같습니다. 답변이 문제를 해결하는 이유와 방법에 대한 설명은 항상 좋습니다.
— cybmeta


0

당신이 루프에 있다면 이것을하십시오 :

<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page

또는 ID가있는 경우 게시물을 얻은 후 post_excerpt 멤버를 고소하십시오.

예 :

$post = get_post( $post_id );
echo $post->post_excerpt;

0

이 코드를 시도하고 변경하십시오 page_id.

<?php $my_query = new WP_Query('page_id=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
 <h3><?php the_title(); ?></h3>
    <div class="text">

        <?php echo wp_trim_words( get_the_content(), 15, '...' ); ?>
 <a href="<?php echo get_page_link(); ?>" class="read-more">Read More</a>
    </div>

 <?php endwhile; ?>

0

나 같은 라이너 중독자. 페이지 ID로 69를 변경하십시오.

<?= apply_filters('the_content', get_post(69)->post_content); ?>
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.