【问题标题】:get post excerpt in WordPress在 WordPress 中获取文章摘录
【发布时间】:2016-07-06 18:15:52
【问题描述】:

我正在尝试让这个 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();

?>

【问题讨论】:

    标签: php wordpress


    【解决方案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);
    

    【讨论】:

    • 谢谢。这有效,只是它给出了给定字数的摘录。我希望用户能够通过该帖子的 CMS 在内容正文中手动包含“”链接,从那里该模板将显示一个摘录,该摘录在他们放置更多标签的任何地方结束。
    【解决方案2】:

    这是一个非常巧妙的解决方案,可以为您解决问题!

        <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>
    

    【讨论】:

      【解决方案3】:

      好的,这就是我想出的。可能更好的解决方案,但它的工作原理!

      function get_excerpt(){
      
          $page_object = get_page( $post->ID );
      
          $content = explode('<!--more-->', $page_object->post_content);
      
          return $content[0];
      
      }
      

      然后这样称呼它:

      <?php echo get_excerpt(); ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-03
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        • 2011-12-22
        • 2014-09-08
        相关资源
        最近更新 更多