【问题标题】:Insert featured image into post content将特色图片插入帖子内容
【发布时间】:2014-03-25 05:28:00
【问题描述】:

我正在为我的网站使用 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!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试将特色图片移动到内容中的原因是什么? Wordpress 法典以以下内容为例:

    // 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();只是显示为该帖子或页面输入到所见即所得的内容。

    如果您尝试这样做以使其与设计或布局相匹配,您可能需要考虑将附加标记和样式应用于该区域。如果您需要特色图片前后的文字,您应该查看自定义字段。

    【讨论】:

    • 就像你提到的那样,我希望在特色图片之前和之后有文字,这样访问者在帖子中看到的第一件事就是他们最初点击到达那里的图片不同.我将如何使用自定义字段来实现这一目标?我不希望添加需要自定义字段的其他图像。我希望将现有的特色图片“放入”内容中。请原谅我的无知,感谢您的回复。
    • 我听说过一个名为 Advanced Custom Fields 的插件的好消息。本质上,您通过插件的仪表板 UI 设置字段及其逻辑,然后在您的模板文件中,您将拥有类似于以下内容的内容:the_content(); if ( has_post_thumbnail() ) { the_post_thumbnail(); } the_field('额外内容');
    • 谢谢!我必须将帖子内容添加到自定义字段还是使用默认的 WP 内容编辑器?我正在尝试简化我的流程,而不是添加任何额外的步骤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    相关资源
    最近更新 更多