将自定义字段图像用作特色图像 [英] Make a custom field image be used as featured image

查看:41
本文介绍了将自定义字段图像用作特色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户旧主题没有使用要显示在页面顶部的特色图片.相反,他们使用的自定义字段与特色图片的工作方式相同.

My client old theme wasn't using the featured image to be displayed on the top of the page. Instead, they are using a custom field that works the same way as a featured image.

问题是他们需要添加一个插件来制作网格,但由于特色图片字段为空,因此无法显示每个帖子的图片.

The problem is that they needed to add a plugin that makes a grid and its not showing the image of each post because the featured image field is empty.

有超过 600 个帖子,手动填充精选图片太费时间了.

There are over 600 posts and manually filling the featured images is too much time.

有没有办法开始使用自定义字段作为技术上的特色图片,以便它显示特色图片将显示的位置?

Is there a way to start using the custom field as technically the featured image so that it shows where an ever a featured image would show?

这是在单个帖子页面上调用图像的 PHP:

That's the PHP that calls the image on the single post page:

                        <?php if (!empty($post->news_image)): ?>
                            <img class="news-image" src="<?php

                            echo $post->news_image;

                            ?>" alt="News Thumb" />
                        <?php endif ?>

Thats the code at the beguinign of the single post php file:

    $theme_url = get_bloginfo('stylesheet_directory').'/';
$base_url = get_bloginfo('url').'/';
$current_lang = pll_current_language();
$current_URI = $_SERVER["REQUEST_URI"];


// get related news
$news_args = array(
    'posts_per_page' => 3,
    'post_type' => 'news',
    'category__not_in' => pll_get_term(171),
    'exclude' => $post->ID,
    'orderby' => 'meta_value',
    'meta_key' => 'news_date',
    'order' => 'DESC',
);
$other_news = get_posts($news_args);


// find Prev & Next news
$news_args["posts_per_page"] = -1;
unset($news_args["exclude"]);

$all_news = get_posts($news_args);

foreach ($all_news as $k => $item) {
    if ($item->ID == $post->ID) {
        $prev_page = isset($all_news[$k-1]) ? $all_news[$k-1] : null;
        $next_page = isset($all_news[$k+1]) ? $all_news[$k+1] : null;
    }
}

$closebuttonlink = $base_url;

if (strpos($_SERVER['HTTP_REFERER'], 'investors/news') !== false) {
    $closebuttonlink = $base_url."investors/news/";
}
else if (preg_match('/\/investors/', wp_get_referer())) {
        $closebuttonlink = $base_url."investors/";
}
else
{
        $closebuttonlink = $base_url;
}



function get_ir_news_permalink($post_object)
{
    $base_url = get_bloginfo('url').'/';
    return $base_url.'investors/news_view/'.$post_object->post_name;
}
if(in_category(141) && preg_match('/media\/news_view/', $current_URI)) {
    wp_redirect(get_ir_news_permalink($post));
}
if(in_category(272) && preg_match('/media\/news_view/', $current_URI)) {
    wp_redirect(get_ir_news_permalink($post));
}


set_query_var( 'section', 2 );

set_query_var( 'newsdetails', 1 );
// select Investor section menu

// load header
get_header();

推荐答案

那么,获取图片字段的方法有以下三种.

Well, there are three ways to get image field as below.

1.返回值 = 图像对象(屏幕截图:https://screencast.com/t/gTdgeU0Y)

注意:请添加您的 id 而不是图片"

Note: please add your id instead of 'image'

<?php 
    $image = get_field('image');
    if( !empty($image) ):
?>
    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>

2.返回值 = 图片网址

<?php if( get_field('image') ): ?>
     <img src="<?php the_field('image'); ?>" />
<?php endif; ?>

3.返回值 = 图片 ID

<?php 
$image = get_field('image');
$size = 'full'; // (thumbnail, medium, large, full or custom size)

if( $image )
{
    echo wp_get_attachment_image( $image, $size );
}
?>

请将上面的代码放在您想要获取图像的位置.希望这对你有用:)

Please place the above code where you want to get an image. Hope this will work for you :)

这篇关于将自定义字段图像用作特色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆