2016-07-06 2 views
1

나는이 특정 WordPress 페이지 템플릿에서 특정 게시물의 발췌문을 표시하려고합니다. 게시물이 만들어지면 내가 원하는 장소에 링크를 삽입해야했습니다. 제목, 미리보기 이미지, 퍼머 링크 등을 가져올 수 있지만 어떤 이유에서 건 발췌를 얻을 수 없습니다. 나는 시도했다 :WordPress에서 게시물 발췌문을 가져 오기

the_excerpt(); 
get_the_excerpt(); 
the_content('',FALSE); 
get_the_content('', FALSE, ''); 
get_the_content('', TRUE); 

다른 것들 중에서. 내가 시도 할 때 get_the_content('', TRUE) 그것은 나에게 링크 후 모든 것에서 내용을 준다. 그러나 나는 링크 앞에 무엇이 필요하다.

아이디어가 있으십니까?

<?php 
     $query = 'cat=23&posts_per_page=1'; 
     $queryObject = new WP_Query($query); 
    ?> 

    <?php if($queryObject->have_posts()) : ?> 

     <div> 

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

       <div> 

        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 

        <br> 

        <?php the_post_thumbnail() ?> 

        <?php #the_excerpt(); ?> 

        <div> 

         <a href="<?php the_permalink(); ?>">Read More</a> 

        </div> 

       </div> 

      <?php endwhile ?> 

     </div> 

    <?php endif; wp_reset_query(); 

>

답변

1

에 한번 당신의 functions.php이를 추가하고 포스트 ID로 발췌 전화 : 같은 템플릿에 전화를 다음

//get excerpt by id 
function get_excerpt_by_id($post_id){ 
    $the_post = get_post($post_id); //Gets post ID 
    $the_excerpt = ($the_post ? $the_post->post_content : null); //Gets post_content to be used as a basis for the excerpt 
    $excerpt_length = 35; //Sets excerpt length by word count 
    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images 
    $words = explode(' ', $the_excerpt, $excerpt_length + 1); 

    if(count($words) > $excerpt_length) : 
     array_pop($words); 
     array_push($words, '…'); 
     $the_excerpt = implode(' ', $words); 
    endif; 

    return $the_excerpt; 
} 

을 :

get_excerpt_by_id($post->ID); 
+0

감사합니다. 이것은 주어진 단어 수와 함께 발췌 한 것을 제외하고는 효과가 있습니다. 사용자가 해당 게시물의 CMS를 통해 콘텐츠 본문에 ""링크를 수동으로 포함 할 수있게하려는 경우이 템플릿은 더 많은 태그를 삽입 할 때마다 끝나는 발췌 부분을 표시합니다. – user2623706

1

여기에 내가 생각해 낸 것이 있습니다. 아마 더 나은 솔루션 일 수도 있지만 작동합니다!

function get_excerpt(){ 

    $page_object = get_page($post->ID); 

    $content = explode('<!--more-->', $page_object->post_content); 

    return $content[0]; 

} 

다음과 같이 호출 :

<?php echo get_excerpt(); ?> 
1

여기 당신을 위해 트릭을 할 것입니다 매우 깔끔한 솔루션입니다!

<div class="post"> 
     <h3 class="title"><?php echo $post->post_title ?></h3> 
     <? 
     // Making an excerpt of the blog post content 
     $excerpt = strip_tags($post->post_content); 
     if (strlen($excerpt) > 100) { 
      $excerpt = substr($excerpt, 0, 100); 
      $excerpt = substr($excerpt, 0, strrpos($excerpt, ' ')); 
      $excerpt .= '...'; 
     } 
     ?> 
     <p class="excerpt"><?php echo $excerpt ?></p> 
     <a class="more-link" href="<?php echo get_post_permalink($post->ID); ?>">Read more</a> 
    </div> 
관련 문제