2016-07-06 94 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> 
相关问题