WordPress.org

Ready to get started?Download WordPress

Forums

Media Library Assistant
[resolved] Pull Featured Image from MLA Gallery (8 posts)

  1. fluxappeal
    Member
    Posted 7 months ago #

    I love this plug-in more and more! My site has 6,000+ photos and I use mla gallery shortcodes + attachment categories to create slideshows. My favorite thing is that adding to a gallery is just a matter of uploading images and assigning the categories, the posts update themselves, it's automagic :)

    My question is: Is there a possibility to use the mla shortcodes with the_post_thumbnail to pull one image from each gallery to be used as a featured image? I'm using the galleries on pages where there is no other content, I really just need a featured image so that when a page is linked, for example, to social media, there's an assigned image. I'm no php expert, but it seems like this should be possible.

    I'm using twentytwelve theme and the site is here: http://www.arthurtaussig.com.

    Thanks again for this amazing plug-in.

    Best,
    Michele

    http://wordpress.org/plugins/media-library-assistant/

  2. David Lingren
    Member
    Plugin Author

    Posted 7 months ago #

    Michele,

    Thanks for your positive feedback on the plugin, your question and the link to your site. Arthur's life and work are fascinating, and the site looks great.

    I believe your question is a variation on this recent support topic:

    Featured Image (Post_thumbnail)

    Have a look at that and let me know if I am understanding your question. If so, I'd like more information to be more helpful.

    There are two parts to the Featured Image puzzle. First, when to assign the image. The easy options for this part are 1) when the page/post is first published, or 2) when the page/post is published and when it is updated. That's the approach taken in the earlier, related topic. A more difficult option would be "whenever the post/page is displayed". The hardest option for assignment is something like "whenever the content of the gallery on the page changes". If, as you say, "the posts/pages update themselves" then we need to find an appropriate hook to hang the Featured Image assignment logic on. So, when do you want the assignment to take place?

    The second part of the puzzle is where to display the image. If there's "no other content" on your pages, where would the Featured Image go?

    By the way, are they posts or pages (you use both terms in your remarks)?

    Let me know how the other topic relates to your needs, and give me any more detail on the questions raised above. I will see if I can come up with something useful. Thanks again for your interest.

  3. fluxappeal
    Member
    Posted 7 months ago #

    Hello David,

    Thanks so much for your response. Yes, Arthur is one interesting guy!

    I did spend some time reviewing the thread you mention, but I couldn't figure out how to adapt the instructions for my circumstances. Sorry for the confusion about pages vs. posts. I'm using pages for the slideshows, so the "categories" I'm working with are the mla attachment categories, not wp post categories.

    I don't actually need featured images to appear on the site because I have created separate index pages with thumbnails that link to the slideshows. The problem is that when I want to share links on social media, there are no thumbnails. I assume this is because I'm using shortcodes and the images aren't actually attached to those pages.

    On to the options..
    The pages/slideshows have already been published, and the majority of them will never be changed or updated once the site is finished, so I think this means Options 1 & 2 are out, if I understand correctly. (What I meant by pages updating themselves is simply that, on those occasions when I do need to add an image, there's no need to revisit or republish the page, I only have to upload and assign the image to the appropriate attachment category.

    Here's an example of what I'm using to display the slideshows:

    [mla_gallery mla_alt_shortcode=gallery royalslider="3" orderby="title, orderby=ASC" attachment_category="alchemical-alice"]

    Since the slideshows are ordered by ASC, is there a way to assign the image with the lowest numerator in the attachment category to appear as the featured image?

    I appreciate any direction you can give me. Thank you!

    Michele

  4. David Lingren
    Member
    Plugin Author

    Posted 7 months ago #

    Thank you for posting the additional information I asked for.

    The short answer is: 1) yes, there is a way to assign the featured image (with the lowest post_id) on your pages when they are displayed, but 2) the image will be displayed on the page (unless you have modified the theme to suppress it), and 3) if you don't display the image, I don't think it will be picked up as a thumbnail when you share the link on social media (I haven't done testing of this).

    It's quite possible that you could solve issues 2) and 3) by modifying the theme (specifically the content-page.php file). What I have in mind is changing the class or style attributes of the image to hide it on the hope that social media will still pick it up as a thumbnail. I'll have to test that idea before we get too excited about it.

    If you're willing to modify your theme files I can try to create a small custom plugin for you along the lines of the plugin created for the "Featured Image (Post_thumbnail)" support topic. You would install and activate the plugin and them modify the theme's content-page.php file to use it. Would you be interested in that solution?

    Actually, you may not have to mess with the Featured Image at all. It might be enough to just add a hidden image to the page under the right conditions. There's no other reason you want a Featured Image, is there?

  5. fluxappeal
    Member
    Posted 7 months ago #

    Hi David,

    I'm definitely open to trying for a solution, I'm not adverse to modifying the theme, and I totally appreciate your suggestions and offer! Thank you

    If this idea doesn't work out, I had another thought, not sure how this could play out. As I mentioned, each slideshow page is already linked to by a thumbnail from an index page. Here's one example:

    http://www.arthurtaussig.com/photographs/the-museum-project/alabama-museums/

    These are just wp galleries to which custom links have been added via a plug-in called WP Custom Gallery Links. Maybe there's a way to work backward and hook into the thumbnail that's already linked to the page. I realize that if something like this were even possible, it would be reliant on a plug-in which could at some point become obsolete, but it might buy me some time in searching for another solution down the road.

    Again, thanks so much for your efforts in helping me get this sorted.

    Michele

  6. David Lingren
    Member
    Plugin Author

    Posted 7 months ago #

    Thanks for the update. I will do some experiments and let you know what I come up with. Just to confirm, if I can do it without modifying the Featured Image, i.e., just adding a hidden image to the page, that would be acceptable, right?

    Also, it would be helpful to see the WordPress page source text for one or more of the pages on which you'd like the images to be added (I should have asked earlier). If you don't want to post it here, send me a note with your e-mail address and I will give you an e-mail address we can use for offline communication. You can use the Contact Us page at our web site:

    Fair Trade Judaica/Contact Us

    There's a reasonable chance that I can give you a solution that avoids modifying the theme's PHP code.

    If you could also give me the source text for the "Alabama Museums" page you used for your example it would be helpful.

    By the way, I spent a minute looking at the WP Custom Gallery Links plugin you mentioned. I'm pretty sure you could replace it with MLA's [mla_gallery] shortcode and the Gallery Display Content parameters. If that's of interest, let me know.

  7. fluxappeal
    Member
    Posted 7 months ago #

    I'm ok with a hidden image as long as it doesn't somehow adversely affect the SEO. Since the page source codes are lengthy, I'll submit these to you offline.

  8. David Lingren
    Member
    Plugin Author

    Posted 7 months ago #

    Thank you for working with me offline to pursue this topic. The best solution to communicating the thumbnails and other information to social media sites like Facebook and Twitter requires the addition of HTML "meta" tags that Facebook and Twitter support for the OpenGraph and other APIs.

    Since you use the "All in One SEO Pack" on your site, which supports most of the required tags, all we need to do is add the "image" tags and populate them with images from your [mla_gallery] and [gallery] shortcodes. This requires a small modification to the header.php file in your Twenty Twelve theme and adding some code to your theme's functions.php file.

    The header.php modification is:

    <?php
    /*
    * Code to insert thumbnail image tags for Facebook, Twitter, etc.
    */
    if ( function_exists( 'mla_insert_social_tags' ) ) {
                    mla_insert_social_tags();
    }
    ?>

    This code goes just above the two lines that add the WordPress header tags and end the < header > part of the page:

    <?php wp_head(); ?>
    </head>

    The mla_insert_social_tags() function is added to the end of the theme's functions.php file:

    /**
     * Code to insert thumbnail image tags for Facebook, Twitter, etc.
     *
     * @return void
     */
    function mla_insert_social_tags() {
    	if ( is_page() ) {
    		global $post;
    		if ( empty( $post->post_content ) ) {
    			return;
    		}
    
    		$count = preg_match( '/\[mla_gallery.*attachment_category="([^\"]*)\"/', $post->post_content, $mla_matches );
    		if ( $count ) {
    			$matched_category = $mla_matches[1]; // for preg_match
    			$gallery = do_shortcode( sprintf( '[mla_gallery %1$s="%2$s" size=full link=none mla_style=none posts_per_page=5]', 'attachment_category', $matched_category ) );
    			$count = preg_match_all( '/src=\"([^\"]*)\"/', $gallery, $mla_matches );
    			if ( $count ) {
    				foreach ( $mla_matches[1] as $match ) {
    					echo sprintf( '<meta property="og:image" content="%1$s" />', $match ) . "\n";
    				}
    
    				echo sprintf( '<meta name="twitter:image:src" content="%1$s" />', $mla_matches[1][0] ) . "\n";
    			}
    
    			return;
    		} // found mla_gallery
    
    		$count = preg_match( '/\[gallery.*ids="([^\"]*)\"/', $post->post_content, $mla_matches );
    		if ( $count ) {
    			$matched_posts = $mla_matches[1]; // for preg_match
    			$gallery = do_shortcode( sprintf( '[mla_gallery %1$s="%2$s" size=full link=none mla_style=none posts_per_page=5]', 'ids', $matched_posts ) );
    			$count = preg_match_all( '/src=\"([^\"]*)\"/', $gallery, $mla_matches );
    			if ( $count ) {
    				foreach ( $mla_matches[1] as $match ) {
    					echo sprintf( '<meta property="og:image" content="%1$s" />', $match ) . "\n";
    				}
    
    				echo sprintf( '<meta name="twitter:image:src" content="%1$s" />', $mla_matches[1][0] ) . "\n";
    			}
    
    		} // found gallery
    	} // is_page
    }

    I am happy that these small changes to your theme are able to get you the results you need, and hope that other MLA users will find them of interest. I am marking this topic resolved, but let me know if you have any problems or further questions. Thanks for raising the issue, for your help in developing the solution and your interest in the plugin.

Reply

You must log in to post.

About this Plugin

About this Topic