How would you recommend adding an image as a custom field in WordPress?

你离开我真会死。 提交于 2019-12-19 04:55:07

问题


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.

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.

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.

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


回答1:


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

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.




回答2:


The template I have uses a manually-entered custom field for the splash image of each post. When I'm done writing my article, I upload an image, copy its URL from the upload tool, never insert it into my post, and then paste that URL into the "Image" custom field. Simple as pie and takes only seconds. Insignificant compared to the amount of time it takes me to write an article.




回答3:


You can use the custom key value fields on posts as well. let's say you always give your images the key 'thumb'. you can then use this code to output them in your post as a thumbnail:

<?php 
   $values = get_post_custom_values("thumb");  
   echo “<img src=\”$values[0]\” class=\”thumb\”></a>”;  ?>



回答4:


Consider using Flutter it's a bit tricky to figure out at first, and has many really useful featured, including EIP (edit in place), and image handling.

After installing the plugin create a new "Write Panel", you'll figure it out from there. The plugin provides you with a rather intuitive GUI, which includes an image uploader. The template tags are very easy to use, I believe it's something like

<?php echo get_image('name_of_field'); ?>

I just had to build a site for a client that needed the same feature, I ended up using Flutter.



来源:https://stackoverflow.com/questions/682926/how-would-you-recommend-adding-an-image-as-a-custom-field-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!