在 WordPress 中获取帖子摘录

use*_*706 2 php wordpress

我试图让这个 WordPress 页面模板显示特定帖子的摘录。创建帖子后,我确定将链接插入到我想要的位置。我能够获取标题、缩略图、永久链接等......但出于某种原因我无法获取摘录。我努力了:

the_excerpt();
get_the_excerpt();
the_content('',FALSE);
get_the_content('', FALSE, '');
get_the_content('', TRUE);
Run Code Online (Sandbox Code Playgroud)

除其他事项外。当我尝试时,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();
Run Code Online (Sandbox Code Playgroud)

?>

Vin*_*mes 5

尝试将其添加到您的functions.php并通过帖子ID调用摘录:

\n\n
//get excerpt by id\nfunction get_excerpt_by_id($post_id){\n    $the_post = get_post($post_id); //Gets post ID\n    $the_excerpt = ($the_post ? $the_post->post_content : null); //Gets post_content to be used as a basis for the excerpt\n    $excerpt_length = 35; //Sets excerpt length by word count\n    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images\n    $words = explode(' ', $the_excerpt, $excerpt_length + 1);\n\n    if(count($words) > $excerpt_length) :\n        array_pop($words);\n        array_push($words, '\xe2\x80\xa6');\n        $the_excerpt = implode(' ', $words);\n    endif;\n\n    return $the_excerpt;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后在模板中这样调用它:

\n\n
get_excerpt_by_id($post->ID);\n
Run Code Online (Sandbox Code Playgroud)\n


All*_*ney 5

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

    <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>
Run Code Online (Sandbox Code Playgroud)