WordPress repeating image input

By: on April 06, 2016 Categories: Web Development

Many custom WordPress themes will require some custom meta box where you can add a picture and, let’s say, its title and description. You may use a great number of plugins available, and most of them have a great amount of functionality. Which is not always good. I like simple code, which does no more and no less than what you need. Therefore, I’ll cover three simple ways to solve this problem.

The first method I propose to you is a straightforward single file plugin I found on stackoverflow, and improved. It will save the image id, so you can use wp_get_attachment_image with a custom image size, for example. You can also define on which post types and on which page templates the meta box will appear.

Here’s the code, just add it to your plugins directory:

You have full control of how the images are displayed in your theme. This is another thing I really like, as most WordPress gallery plugins need a lot of overwriting. Here’s an example of how easy you can output the images and their description in your theme:

Add your custom HTML and CSS and you’re set.

In the next post, I’ll continue with an even simpler method to add a few custom image boxes.

Da un share daca ti-a placut articolul

Share on Facebook Share Share on Twitter Share Share on Linkedin Share