在帖子内容中添加或添加图像

时间:2017-11-07 作者:ricardorios

我一直在研究儿童主题和功能。php文件,用于向具有特定标记的帖子添加图像(Videos 在代码中)。我试图创建该功能,但我有一些问题,其中一个问题是,在站点中,当我加载带有该标记的帖子时,它崩溃了,看起来它停留在一个循环中,然后我必须手动从数据库中的帖子中删除所有重复的图像(我只想要一个)(PhpMyAdmin)

你能帮我解决这个问题,并有工作代码吗?

Important Update: 我使用的是RSS提要,并且我要求该图像也在提要中(我的意思是在posts表数据库中)

这是我的密码functions.php:

/**
 * Prepend image to post if it has a specific tag
 *
 * @param String $content - WP Post Content for display
 *
 * @return String $content - WP Post Content for display
*/ 
function theme_videos_append_image( $content ) {

    global $post;

    // It would be easier if you got this URL from Attachment ID
    $upload_dir_arr     = wp_upload_dir();  // Get upload directory array ( https://developer.wordpress.org/reference/functions/wp_upload_dir/#user-contributed-notes )
    $static_image_url   = $upload_dir_arr[\'baseurl\'] . \'/2017/11/upliftingscroll.jpg\';

    // Ensure we are viewing a Post, and it has the Videos tag
    if( has_tag( \'Videos\', $post->ID ) ) {

        $has_image = get_post_meta( $post->ID, \'_video_image_added\', true );    // Check if our postmeta exists

        // If is does not have our postmeta - add it
        if( empty( $has_image ) ) {

            $image      = sprintf( \'<p><img src="%1$s" alt="" class="img-responsive" /></p>\', $static_image_url );  // Create image
            $content    = $image . $content;    // prepend image

            // Update post so we don\'t need to add the image again
            $success = wp_update_post( array(
                \'ID\'            => $post->ID,
                \'post_content\'  => $image . $post->post_content,
            ) );

            // If the post updated, create postmeta letting us know later it has the image
            if( false !== $success && !is_wp_error( $success ) ) {
                update_post_meta( $post->ID, \'_video_image_added\', true );
            }

        }

    }

    return $content;

}
add_filter( \'the_content\', \'theme_videos_append_image\' );

2 个回复
SO网友:GDY

下面是一个更简单的函数版本:

<?php

    add_filter( \'the_content\', \'theme_videos_append_image\' );

    function theme_videos_append_image( $content ) {

        global $post;

        $upload_dir_arr     = wp_upload_dir();
        $static_image_url   = $upload_dir_arr[\'baseurl\'] . \'/2017/11/upliftingscroll.jpg\';
        $tag = \'<p><img src="\' . $static_image_url . \'" alt="" class="img-responsive" /></p>\';

        return has_tag( \'Videos\', $post->ID ) ? $tag . $content : $content;

    }

?>
检查这是否是获取图像URL的最优雅的解决方案。我觉得有点奇怪。

您提到的循环可能存在,因为wp_update_post 这个the_content 也可以应用过滤器。因此,如果您想坚持您的解决方案,请尝试先添加帖子元,然后更新帖子内容。。。但我认为这没有必要。

SO网友:Skatox

扩展@GDY解决方案。你还必须the_excerpt_rss 通过执行以下操作进行筛选:

add_filter(\'the_excerpt_rss\', \'theme_videos_append_image\');

add_filter( \'the_content\', \'theme_videos_append_image\' );

function theme_videos_append_image( $content ) {

        global $post;

        $upload_dir_arr     = wp_upload_dir();
        $static_image_url   = $upload_dir_arr[\'baseurl\'] . \'/2017/11/upliftingscroll.jpg\';
        $tag = \'<p><img src="\' . $static_image_url . \'" alt="" class="img-responsive" /></p>\';

        return has_tag( \'Videos\', $post->ID ) ? $tag . $content : $content;

}

结束

相关推荐

Using $_GET in Functions.php

我在函数中不断遇到此代码的未定义索引错误。我做错了什么?function hire_more(){ $hire = $_GET[\'hire_more\']; write_log($hire); } add_action(\'wp_loaded\',\'hire_more\');