【问题标题】:Wordpress - get post content in new divWordpress - 在新 div 中获取帖子内容
【发布时间】:2026-02-01 15:30:02
【问题描述】:

我正在尝试在一个 div 中使用帖子图像/缩略图,并在自定义布局上使用新 div 中的内容。在显示图像后尝试获取内容时遇到问题。

这可以使我的帖子标题和图像进入所需的布局,但不显示图像标题–

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
    <?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
</header><!-- .entry-header -->

<div class="news-images">
<div id="news-swiper" class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide" data-caption="">
            <div class="entry-content news-item-copy">
                <?php
                $get_description = get_post(get_post_thumbnail_id())->post_excerpt;
                the_post_thumbnail();
                if(!empty($get_description)){//If description is not empty show the div
                echo '<div class="image-captions">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
                }
                ?>
            </div>
        </div>
    </div>
</div>
</div>

<div class="news-sharing">
    <?php wpsocialite_markup(); ?>
</div>

这是我试图获取剩余帖子内容并遇到问题的地方。

<div class="news-item-copy">    
<?php the_excerpt(); ?> 
    <?php
        wp_link_pages( array(
            'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
            'after'  => '</div>',
        ) );
    ?>
</div>

</article>

我使用&lt;?php the_excerpt(); ?&gt; 只是将内容放置在所需的位置,但显然因为它是摘录,它没有显示完整的内容。想法是将带有标题的特色图片/缩略图放在顶部,社交分享放在中间,内容放在最后。

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    我想你想用 the_content() 替换 the_excerpt()

    http://codex.wordpress.org/Function_Reference/the_content

    【讨论】:

    • 使用the_content() 检索该帖子的全部内容,包括我不想要的图像,因为我已经有一个调用上面所需图像的函数。感谢您的意见。
    • 我会考虑使用自定义字段插件。您可以将自定义字段中的图像和其余内容分开。 wordpress.org/plugins/advanced-custom-fields
    【解决方案2】:

    您在 get_post 中传递缩略图 id,您必须在该函数中传递帖子 id 而不是缩略图 id

    $get_description = get_post(get_post_thumbnail_id())->post_excerpt;

    或者简单地回显the_content();它会打印帖子内容&如果你想显示一些有限的内容 然后使用

    substr(the_content(),0,150) 只会显示 150 个字符

    【讨论】: