【问题标题】:Make a custom field image be used as featured image将自定义字段图像用作特色图像
【发布时间】:2019-03-25 06:27:46
【问题描述】:

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

问题是他们需要添加一个制作网格的插件,并且由于特色图像字段为空,因此它不显示每个帖子的图像。

有超过 600 个帖子,手动填充特色图片太费时间了。

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

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

                        <?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();

【问题讨论】:

  • 请您在您的单个帖子页面中发布 $post 的 wordpress 循环或代码。
  • 我刚刚编辑了帖子
  • 您有 2 个选项。 1)您将替换您的代码自定义字段(图像)而不是特色图像代码 2)创建 SQL 查询以将自定义字段路径复制到特色图像。但这完全取决于字段类型。
  • 你能帮我完成选项 1 吗? @贾吉尔巴赫什
  • 请看我的回答如下。

标签: php wordpress


【解决方案1】:

嗯,获取图像字段的方式有以下三种。

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

注意:请添加您的 id 而不是 '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 );
}
?>

请把上面的代码放在你想获取图片的地方。希望这对你有用:)

【讨论】:

  • 哦,我想我需要的是 MySQL 解决方案,因为该插件只拍摄特征图像
  • 可能是functions.php文件中的一个循环,每个帖子都从任一自定义字段中获取特色图片
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2019-11-07
  • 2015-05-12
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多