Wordpress - 高级自定义字段 - 类别图像错误的小工具
问题描述:
我使用高级自定义字段返回类别图像的URL位置。这将允许我在帖子标题之前添加类别图片。我在我的functions.php文件中有一个函数insert_image_before_title,它被标题过滤器引用(见下文)。所以这似乎适用于我的主要博客页面或帖子的详细信息页面上的帖子。由于该类别的正确图像出现在帖子的标题前面。然而,它不适用于小部件。例如,“最新评论”或“最近发布的帖子”小部件将在标题前显示相同的类别图像,即使类别图像可能不是与该帖子关联的正确图像(它看起来使用它遇到的第一个类别图像对于特定的部件并显示在前端的部件相同的图像,如果任何类别的图像是联系在一起的文章。Wordpress - 高级自定义字段 - 类别图像错误的小工具
function insert_image_before_title($title, $id = null)
{
// load all 'category' terms for the post
$terms = get_the_terms(get_the_ID(), 'category');
// we will use the first term to load ACF data from
if(!empty($terms))
{
$term = array_pop($terms);
$category_icon_url = get_field('category_icon', $term);
// do something with $custom_field
if($category_icon_url)
{
$title = '<img src="'. $category_icon_url .'" />' . $title;
}
}
return $title;
}
add_filter('the_title', 'insert_image_before_title', 10, 2);
答
Try this:
<?php
$args = array(
'taxonomy' => 'post_tag',
'hide_empty' => true,
);
$terms = get_terms($args);
foreach ($terms as $term):
$thumbnail = get_field('field_name', $term->taxonomy . '_' . $term->term_id);
?>
<!--If an array is returned in ACF-->
<img src="<?php echo $thumbnail['url'];?>" alt="<?php echo $thumbnail['title'];?>">
<!--If an url is returned in ACF-->
<img src="<?php echo $thumbnail;?>" alt="#">
<?php endforeach; ?>
感谢您的答复。我是新来的WordPress所以我不t知道这件事很多,所以你能告诉我哪里(哪个文件)我会把你的代码放在上面吗? – truk
给我一个链接,你想这样做 –
如果你拿这个页面http:// bassandbrands .COM /博客/声音 - 检查 - jamaica- chronixx /例如,如果您转到右侧侧边栏并向下滚动到“最新评论”窗口小部件。所有标题在标题名称前都有一个耳机图标作为类别图像。当只有第一个应该有耳机作为分类图像图标,其余的应该有一个录制图标。所以有些东西被搞砸了 – truk