use same image in wordpress gallery without upload it many times - wordpress

I know this sounds strange but I need to make a gallery with repeated images.
I need to know if is possible to use the same image in the gallery more than one time without having to add it again and again. It's very annoying to have the same image repeated many times among the uploaded images.
Thanks in advance.

No, not with with orginal wordpress code. Even manually putting several times the same media-id will result in only one image being rendered. you can, however, create your own gallery shortcode: code from here
//Init hook
add_action('init', 'override_gallery');
//Override function
function override_gallery()
{
remove_shortcode('gallery');
add_shortcode('gallery', 'my_gallery_shortcode');
}
//Custom gallery shortcode
function my_gallery_shortcode($atts, $content) {
//Default parameters to
extract( shortcode_atts( array(
'ids' => '',
'orderby' => 'post__in',
'columns' => '3',
'link' => 'file' //file | link | <empty string> (for linking to attachment page)
), $atts ));
//Your own presentational code here...
}
But you still can't choose the same image several times in the image selector. But you could multiply it in your shortcode. Probably it's better to create your own shortcode using another name though.

Related

ACF field to create sets of pages when saved

I have an odd request, i'm not sure if this is even possible. But i'll try to work out the process below, and if anyone can help me work this out that would be amazing!
Ideally the process is as follows:
Admin goes to parent options page, within the options page there is a repeater field, called add new company. This will just be a field with a title.
Admin fills in field and presses save. This will generate a sub options page with that name, within the options field, there will be a set of fields like logo, a colour picker and some text fields (these could be a set of fields from within ACF if thats possible).
Also when this original Repeater Field is made/saved a set of pages is generated from a set of templates. Essentially using the name from the repeater field to be the main page title for the top level page and all the sub pages below are just dynamically generated. They don't need to have anything different about them, they just need to generate from a set of page templates. It needs to be able to associate with the newly generated company bits from the sub options field.
This will then essentially give the admin a new set of pages which will use the new options logo / colours etc. It would almost need to generate a new set of templates based off the master templates to dynamically make sure it picked up the correct information from the sub options page.
I'm not sure if this is possible, I have seen it work elsewhere on another job I have worked on (not exactly the same as the above but similar), but I can't work out the process to make it work sadly, as I have a horrid feeling that there is some complex bits within the database going on to do the duplication dynamically.
My other option is to run everything as a WordPress Multisite but I was trying to avoid that if possible on this occasion, but I may have to use Multisite to achieve the above.
If anyone can help me work this out that would be amazing!
Thanks in advance for any help :)
You should be able to plug into the save_post action and create new subpages from there.
add_action( 'save_post', 'create_sub_pages' ); //Plug into save_post action
//Function create sub_pages
function create_sub_pages($post_ID) {
//Repeater field name
$repeater_field_array = get_field('repeater_field_name');
//Loops through all of the items in the repeater field
foreach($repeater_field_array as $key => $value) {
//Check to see if there is already a sub page with that post name
$child_pages = get_pages(array( 'child_of' => $post_ID ));
$child_page_exists = false;
foreach($child_pages as $pages) {
if ($pages->post_title === $key) {
$child_page_exists = true;
}
}
//If not, set up the creation of the new post
if ($child_page_exists === false) {
$new_page_title = esc_html__( $key );
$new_page_content = '';
$new_page = array(
'post_type' => 'page',
'post_date' => esc_attr( date('Y-m-d H:i:s', time()) ),
'post_date_gmt' => esc_attr( date('Y-m-d H:i:s', time()) ),
'post_title' => esc_attr( $new_page_title ),
'post_name' => sanitize_title( $new_page_title ), //This could from the sub_field in the repeater
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_parent' => $post_ID,
'menu_order' => $new_page_order
);
$new_page_id = wp_insert_post( $new_page );
update_post_meta( $new_page_id, '_wp_page_template', $value );
}
}
}
Again, this is just a spitball since there was not much code to review, but it could help you get going in the right direction.

How to handle/manage custom images?

I am working on a special plugin for a customer.
The situation in short:
The plugin contains a automatic import for a .zip file. Inside this files are one .xml file and images.
The plugin reads the .xml file and insert the information to the database.
My question:
How I can handle the images the best way. Should I import them into the wordpress gallery or should I manage them on my own.
Is there a way to use the wordpress gallery, because it will automatic generate thumbnails, or is it not a good idea?
I need some suggestions. Thanks!
You should add images in wordpress gallery. Then you have to get these uploaded images from the wordpress gallery:
Step 1: Prepare the query
global $post;
$args = array(
'post_parent' => $post->ID, // For the current post
'post_type' => 'attachment', // Get all post attachments
'post_mime_type' => 'image', // Only grab images
'order' => 'ASC', // List in ascending order
'orderby' => 'menu_order', // List them in their menu order
'numberposts' => -1, // Show all attachments
'post_status' => null, // For any post status
);
First, we set up the global Post variable ($post) so we can have access to the relevant data about our post.
Second, we set up an array of arguments ($args) that define the kind of information we want to retrieve. Specifically, we need to get images that are attached to the current post. We're also going to get all of them, and return them in the same order they appear in the WordPress gallery.
Step 2: Retrieve the Images from Wordpress gallery
// Retrieve the items that match our query; in this case, images attached to the current post.
$attachments = get_posts($args);
// If any images are attached to the current post, do the following:
if ($attachments) {
// Initialize a counter so we can keep track of which image we are on.
$count = 0;
// Now we loop through all of the images that we found
foreach ($attachments as $attachment) {
Here we are using the WordPress get_posts function to retrieve the images that match our criteria as defined in $args. We are then storing the results in a variable called $attachments.
Next, we check to see if $attachments exists. If this variable is empty (as will be the case when your post or page has no images attached to it), then no further code will execute. If $attachments does have content, then we move on to the next step.
Set parameters for a WordPress function called wp_get_attachment_image for the images information.
Source: Read the link for complete tutorial or other steps > https://code.tutsplus.com/tutorials/how-to-create-an-instant-image-gallery-plugin-for-wordpress--wp-25321

wordpress count image attachments

I'm looking for some help, for some reason this code isn't working when trying to display total image attachments on the main index.php page.
// Get all the attachments
$attachments = get_posts ( array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
) );
// Count all the attachments
$total = count( $attachments );
The problem is, the loop is in the main index.php page, but it's calling a post.php template so I have placed this code there instead (I'm assuming that's still in the loop?) I am then calling $total just below it where I want to show "View all # images".
Any ideas why this is just displaying the number as 0 even though I have added an image gallery to the post using the basic Wordpress gallery media library?
Thanks
The attachment count only increases if it's attached to a post. You can force the count by adding this:
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
in a function in your functions.php something like this:
function change_category_arg() {
global $wp_taxonomies;
if ( ! taxonomy_exists('category') )
return false;
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
}
add_action( 'init', 'change_category_arg' );
Explanation:
This is significant in the case of attachments. Because an attachment
is a type of post, the default _update_post_term_count() will be used.
However, this may be undesirable, because this will only count
attachments that are actually attached to another post (like when you
insert an image into a post). This means that attachments that you
simply upload to WordPress using the Media Library, but do not
actually attach to another post will not be counted. If your intention
behind associating a taxonomy with attachments was to leverage the
Media Library as a sort of Document Management solution, you are
probably more interested in the counts of unattached Media items, than
in those attached to posts. In this case, you should force the use of
_update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.
from Wordpress Codex on register_taxonomy

Creating a delete confirmation for images using the Wordpress meta box plugin

I am using the Meta Box plugin for Wordpress. I can successfully create fields in the cms for users to upload images. I would like to extend this in two ways:
First, I would like a delete confirmation when users remove an image from the image gallery
Here is the code:
$meta_boxes[] = array(
'id' => 'project_media',
'title' => 'Project Media',
'pages' => array( 'project' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Media Gallery',
'desc' => 'Images should be sized to 983px x 661px',
'id' => $prefix . 'project_media_gallery',
'type' => 'image'
)
);
This creates upload functionality in the custom post type where users can add images to a slideshow. The problem is if the user accidentally clicks the delete button, there is no confirmation to make sure it is deleted. Can I somehow extend the plugin through functions and call an alert when this button is clicked? Something that does not involve editing the WP core?
Second, the base functionality requires the user to upload an image from their local machine. Is there a way to tap into the Media Library for this?
No idea how to even start tackling this one.
To answer the first question
First, I would like a delete confirmation when users remove an image from the image gallery
You can do that by calling a custom script file from the functions.php.
function alert_delete() {
if(is_admin()){
wp_register_script( 'alert_delete', get_bloginfo('template_url'). '/js/alert_delete.js', array('jquery'));
wp_enqueue_script('alert_delete');
}
}
and create a file named alert_delete.js in the js directory of your theme.
alert_delete.js:
// admin delete check
jQuery(document).ready(function(){
jQuery(".rwmb-delete-file").click(function() {
if (!confirm("Are you sure? This process cannot be undone.")){
return false;
}
});
});
In response to the second question...
Second, the base functionality requires the user to upload an image
from their local machine. Is there a way to tap into the Media Library
for this?
Get the latest version of the Meta Box Plugin first.
then change
'type' => 'image'
to
'type' => 'image_advanced'
which will allow you to upload from the existing Media Gallery or a new file from your computer.

Multiple Featured Images Wordpress

So my aim is to find a method of adding more thumbnails only for displaying on a custom post type, for example I wish to have a large image (not the same image) for a featured post and a different image for the default view.
In the end i followed this tutorial and it did exactly what i required to a T.
http://www.lifeonlars.com/wordpress/how-to-add-multiple-featured-images-in-wordpress
have you try this add_image_size
why don't you use custom post template plugin
I got a solution from online. I also customized some code. You can check this.
Step 1 Download this library from this link and put beside functions.php ( theme root ).
Step 2: Copy this code below to functions.php.
/*
* Code for Multiple Featured Image.
* Multiple Featured image is only for your selected post type.
*/
require_once('library/multi-post-thumbnails.php');
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => '2nd Feature Image',
'id' => 'feature-image-2',
'post_type' => 'your_post_type_name'
)
);
new MultiPostThumbnails(array(
'label' => '3rd Feature Image',
'id' => 'feature-image-3',
'post_type' => 'your_post_type_name'
)
);
new MultiPostThumbnails(array(
'label' => '4th Feature Image',
'id' => 'feature-image-4',
'post_type' => 'your_post_type_name'
)
);
};
Step 3 Check now.
I can write entire code here, but clicking on this tutorial link is much easier :)

Resources