将精选图片插入帖子内容

时间:2014-03-24 21:11:55

标签: php wordpress

我正在为我的网站使用wordpress主题,我正在尝试更改php,以便在帖子内容中显示特色图片,而不是之前或之后(帖子的顶部或底部)

这是原始代码:

<?php if ( ( has_post_thumbnail( $post_id ) || '' != get_post_meta( $post_id, 'Thumbnail', true ) ) && 'on' == et_get_option( 'origin_thumbnails' ) ) { ?>
    <div class="post-thumbnail">
    <?php
        if ( has_post_thumbnail( $post_id ) ) the_post_thumbnail( 'full' );
        else printf( '<img src="%1$s" alt="%2$s" />', esc_attr( get_post_meta( $post_id, 'Thumbnail', true ) ), the_title_attribute( array( 'echo' => 0 ) ) );
    ?>
    </div>  <!-- end .post-thumbnail -->
<?php } ?>

    <?php the_content(); ?>

以下是我在儿童主题中调整它的方法:

<?php the_content(); ?>

    <?php if ( ( has_post_thumbnail( $post_id ) || '' != get_post_meta( $post_id, 'Thumbnail', true ) ) && 'on' == et_get_option( 'origin_thumbnails' ) ) { ?>
    <div class="post-thumbnail">
    <?php
        if ( has_post_thumbnail( $post_id ) ) the_post_thumbnail( 'full' );
        else printf( '<img src="%1$s" alt="%2$s" />', esc_attr( get_post_meta( $post_id, 'Thumbnail', true ) ), the_title_attribute( array( 'echo' => 0 ) ) );
    ?>
    </div>

我的php不是很流利,所有完成的工作都是将精选图片移到帖子的底部: - /。我发布到我的主题论坛,并给出了以下代码似乎没有做任何事情。

add_filter( 'the_content', insert_featured_image, 20 );

function insert_featured_image( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
return $content;
}

有人能指出我正确的方向吗?

TIA!

1 个答案:

答案 0 :(得分:-1)

尝试将精选图片移动到内容中的原因是什么? Wordpress codex以下面的例子为例:

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
    the_post_thumbnail();
} 
the_content();

基本上,the_post_thumbnail();将显示在the_content()时选择为特色图像的图像;只是显示输入到该帖子或页面的所见即所得内容。

如果您尝试执行此操作以使其与设计或布局匹配,则可能需要查看在此区域应用添加标记和样式。如果在特色图像之前和之后需要文本,则应查看自定义字段。