drt96856 2015-01-02 00:13
浏览 38

麻烦将wordpress图像标题和内容放在单个帖子页面上

Working on trying to get single post image and caption above post content in a custom theme. Can I get the image caption below the image and remaining content below a new div?

Current code:

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

    <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 class="news-sharing">
        <a class="socialite twitter-share" href="" target="_blank" data-via="" data-text="New Website Launch — " data-url="" data-count="horizontal">Share on Twitter</a>
        <a class="facebook-like socialite" data-href="" target="_blank" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false">Like on Facebook</a>
        </div>

        <?php the_excerpt(); ?> 

        <?php
        wp_link_pages( array(
            'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
            'after'  => '</div>',
        ) );
        ?>
    </div><!-- .entry-content -->
</article><!-- #post-## -->

This works in displaying the post image and excerpt but not the image with caption and not the full post text.

Replacing this:

<?php the_excerpt(); ?>

with:

 <?php the_content(); ?>

returns the full post content including the image again.

  • 写回答

1条回答 默认 最新

  • dtrhd2850 2015-01-02 01:00
    关注

    Try this, there is nothing special just getting the post object and then its values. post_excerpt are also known as captions.

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
        </header>
    
        <div class="entry-content news-item-copy">
            <?php
            if(has_post_thumbnail())
            {
                the_post_thumbnail();
                $thumbnail_id  = get_post_thumbnail_id(get_the_ID());
                $thumbnail_data = get_post($thumbnail_id);
                $caption = $thumbnail_data->post_excerpt;
                if(!empty($caption)){//If description is not empty show the div
                    echo '<div class="image-captions">' . $caption . '</div>';
                }
            }
            ?>
            <div class="news-sharing">
                <a class="socialite twitter-share" href="" target="_blank" data-via="" data-text="New Website Launch — " data-url="" data-count="horizontal">Share on Twitter</a>
                <a class="facebook-like socialite" data-href="" target="_blank" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false">Like on Facebook</a>
            </div>
    
            <?php
            echo the_content();
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
                'after'  => '</div>',
            ) );
            ?>
        </div><!-- .entry-content -->
    </article><!-- #post-## -->
    

    After this you don't need to add the featured/thumbnail image to the post content and it will not appear again in content. I hope this makes sense now.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀