I need to customize the native WordPress Gallery function so that I can display a gallery based on the name instead of the ID of the post it is attached to.
A gallery plugin isn't an option.
I found some useful info here:
[[LINK href="http://wordpress.stackexchange.com/questions/43558/how-to-manually-fix-the-wordpress-gallery-code-using-php-in-functions-php"]][[/LINK]]
and I see the wordpress file is at wp-includes/media.php
But I'm not sure how to write the function / filter/action to make this work.
Anyone have the answer?
Arnav Joy answers:
try this
<?php
$post_title = 'my_post';
$postData = get_page_by_title( $post_title, 'OBJECT' , 'post');
$postID = $postData->ID;
if(!empty($postID)) {
echo do_shortcode('[gallery] id="'.$postID.'"]');
}
?>
Spencer Tomosvary comments:
Just doing this much returns nothing..
$post_title = ( 'Home' );
$postData = get_page_by_title( $post_title, 'OBJECT' , 'post');
$postID = $postData->ID;
print_r ($postID);
Arnav Joy comments:
please see this line
$postData = get_page_by_title( $post_title, 'OBJECT' , 'post');
if you want to get title of the page then pass 'page' as last parameter or if the title is to be of post the pass 'post' as parameter , i have passed it as post , so for page use this
$postData = get_page_by_title( $post_title, 'OBJECT' , 'page');
you can also leave it blank
$postData = get_page_by_title( $post_title);
let me know if it is helpfull
Spencer Tomosvary comments:
got it..
I used:
$post_title = ( 'Home' );
$postData = get_page_by_title( $post_title);
$postID = $postData->ID;
if(!empty($postID)) {
echo do_shortcode('[gallery id="'.$postID.'"]');
}
Which worked. Thanks!
One last thing.. how do I format the post_title if the name has a space? Can't seem to get that to work..
I tried: $post_title = ( 'Home" "Images' );
and
$post_title = ( 'Home'." ".'Images' );
but no luck.
Martin Pham answers:
Do you want to show gallery based on the name of the album?
ex:
<?php
$album_name = 'ABC';
$display = get_gallery_by_name($album_name);
// Or do_shortcode('[gallery name="'.$album_name.'"]');
?>
Spencer Tomosvary comments:
Yes.. if the album name is the name of the post to which it is attached to..
Martin Pham comments:
please try this
function get_post_id_by_title($title) {
global $wpdb;
$post_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} AND post_type='post' ", $Title) );
if ($post_id)
return (int)$post_id;
return false;
}
function get_images_gallery($title) {
$postid = get_post_id_by_title($title);
if($postid) {
echo do_shortcode('[gallery] id="'.$postid.'"]');
}
}
Spencer Tomosvary comments:
Awesome, that certainly looks like it will work. I'm not doing something quite right though..
I added the functions into my functions.php .. then I was looking to see where I would insert the gallery title..
Do I add $title="Gallery Name Here";
below the declaration of the first function?
Or can I use it like this get_images_gallery('Gallery Name Here');
??
Somehow my syntax is wrong and it's breaking the template..