WORDPRESS: Is Custom Fields the solution for my po

2019-03-04 02:32发布

问题:

Here's a brief explanation of what I'm trying to do:

My homepage will have about 12 thumbnail images with title, short description, maybe web url (arranged 3 x 4 or whatever). I want each of these entries to link to my single.php page which will show more details about each project, with multiple larger image files that will display with captions to the right of the project description.

I'm thinking the best way to approach this would be to use the 'featured image' for the thumbnail display on the homepage which seems to be working now, but I've been trying to figure out Custom Fields to use for my other images (image1, image2, image3). I can't figure it out. I want to be able to enter all the content and images for a new portfolio piece from one screen. Possible?

Is this doable? If so, how? The Custom Fields are confusing me really badly at this point, even after reading tons of articles online about it.

I'd really appreciate some input! I can't seem to crack this and it's getting a bit frustrating. :P

回答1:

Custom fields are per post key-value pairs. So you could certainly use them for this purpose. For instance, you could have custom fields named exactly as suggested: image1, image2, etc. Note that the generic nature of custom fields does make this at least a little awkward, but it is probably still your best option.

Those keywords must be processed somewhere, and it sounds as if your single.php is the place to do it. Essentially your single.php is a custom post template, so you can extract the custom field data and render it as you wish. You will need to write PHP code to do this, and be comfortable in reading the WordPress function reference.

The get_post_meta function is probably the most relevant one, but see the others on the main reference.

What you want to do isn't difficult, but is hard to do the first time. That's because you will be learning all sorts of little nuances of WordPress along the way.



回答2:

I really understand your situation. In fact i have already done the same plugin before where you have to attach several images in one post and in one custom field. Here is my solution:

$images = trim(get_post_meta($post->ID,'images',true)); 
$images_array = explode(',',$images);
foreach($images_array as $i){
   echo '<img src="'.$i.'"/>';
}

Let me know if that helps.