您如何建议在WordPress中将图像添加为自定义字段? [英] How would you recommend adding an image as a custom field in WordPress?

查看:150
本文介绍了您如何建议在WordPress中将图像添加为自定义字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在WordPress中为每个页面添加一个图像。
我不想使用WYSIWYG编辑器插入它,我只需要将url作为自定义字段,我稍后会在模板中使用。

I need to add an image to each page in WordPress. I don't wish to insert it using the WYSIWYG editor, I just need the url as a custom field, which I later use in the template.

我尝试使用CFI插件(自定义字段图像),我试图让它与其余的插件一起工作,但后来我将网站移动到生产服务器,CFI因某些原因无法正常工作。

I tried using the CFI plugin (Custom Field Images), and I hacked my way into getting it to work with the rest of my plugins, but then I moved the site to the production server and CFI just didn't work for some reason.

我似乎无法找到任何其他插件,只允许您从库中选择图像并将其添加为自定义字段。

I can't seem to find any other plugin that just lets you pick an image from the library and add it as a custom field.

我已经降级到我愿意手动输入每个页面的所有URL的程度。但在此之前,我想我会在这里查看。

I've downgraded to the point where I'm willing to manually enter all the URLs into each and every page. but before I do, I thought I'd check here.

有谁能告诉我在WordPress中添加图片作为自定义字段的最简单,最好的方法是什么(2.7.1如果重要的话)?

Can anyone tell me what's the easiest, best way to add images as custom fields in WordPress (2.7.1 if it matters)?

推荐答案

在我们的WordPress模板中,每个帖子通常只有一个图像'附加',在外面显示帖子'(文字)内容。

In our WordPress template, each post generally only has one image 'attached', which is displayed outside the posts' (textual) content.

我只是使用编辑帖子的媒体上传器上传文件,永远不要像上面的JoshJordan那样插入帖子,然后检索图像在我的模板文件中使用正确位置的一些代码。

I simply upload the file using the edit posts' media uploader, never insert it into the post like JoshJordan above, then retrieve the image using a bit of code in the right place in my template file.

如果您在帖子中使用多个图像,这也可以。在您的帖子内容中。只要您将用作主要帖子图像的图像保留为第一张图像(请记住,您可以通过上下拖动来重新排序帖子图像库中的图像),您可以轻松地在任何地方调用它在你的模板文件中使用类似的东西:

This would also work if you're using more than one image in your post, eg. in your post content. As long as you keep the image used as the 'main' post image as the first image (remember you can re-order images in your posts' image library by dragging them up and down), you're easily able to call it anywhere in your template file by using something like this:

<?php
$img_size = 'thumbnail'; // use thumbnail, medium, large, original

$img_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/gif') and post_type = 'attachment'");

$img_array = wp_get_attachment_image_src($img_id,$img_size,false);

echo '<img src="'.$img_array[0].'"' title="'.get_the_title().'" />';
?>

无需复制和粘贴图片网址。

No need for copying and pasting image urls.

这篇关于您如何建议在WordPress中将图像添加为自定义字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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