loop - How do I get the attributes of a short code from a post?

I am trying to preview a NextGen gallery on my main page (and category pages) to show a single image from the gallery on

I am trying to preview a NextGen gallery on my main page (and category pages) to show a single image from the gallery on the main page next to the text from the post that normally shows up. I have found examples of PHP that get the images from a gallery given a gallery ID. In my loop I have a current post. What I cannot figure out is how to, given a post, get the attributes of the [nggallery] short code.

In other words for each post with a NextGEN gallery I need the id value form the short code. For example if the post contains [nggallery id=50] I need the value 50.

How can I get that information from a post?

I was hoping to find the solution in the source code of the next gen plug-in but of course that code registered a short code handler and lets WP call them back. There are no examples in their source code where they parse a post looking for their short code.

I am trying to preview a NextGen gallery on my main page (and category pages) to show a single image from the gallery on the main page next to the text from the post that normally shows up. I have found examples of PHP that get the images from a gallery given a gallery ID. In my loop I have a current post. What I cannot figure out is how to, given a post, get the attributes of the [nggallery] short code.

In other words for each post with a NextGEN gallery I need the id value form the short code. For example if the post contains [nggallery id=50] I need the value 50.

How can I get that information from a post?

I was hoping to find the solution in the source code of the next gen plug-in but of course that code registered a short code handler and lets WP call them back. There are no examples in their source code where they parse a post looking for their short code.

Share Improve this question asked Jun 28, 2012 at 20:39 Ian LelsieIan Lelsie 1731 silver badge7 bronze badges 1
  • I just found this question which mentions get_shortcode_regex which has an example that would let me find out if the nggallery short code was in a post. Still nothing that would let me see the attributes of said short code. – Ian Lelsie Commented Jun 28, 2012 at 20:59
Add a comment  | 

3 Answers 3

Reset to default 3

Your solution has helped me find an answer to my own problem of getting the attribute of a certain shortcode, but I fear there may be an issue with your approach.

You're using preg_match to check the post_content, which will only return 1 match. If you have a post that has multiple shortcodes in it, it will only return the first one, which may not be the one you're looking for.

Instead, you should use preg_match_all, then loop through the regex_matches array to check for the shortcode you need.

Also, you don't need to use str_replace to remove the quotes from the string. Wordpress has a built-in function shortcode_parse_atts which takes that a string of parameters (in your case it would be $regex_matches[3]) as an argument and outputs an array. This would also spare you the use of wp_parse_args function.

I have found a solution. After a bunch of searching around I found some code that determines if a post has a specific short code in it.

That code also has some attempt to parse the parameters to the short code. Which is good because I want the gallery ID. There were some issues with the code as posted so I tweaked it. Here is code that can find a short code in a post and get its parameters:

        <?php //  Look for a NextGEN gallery
        $galleryID;
        $previewIndex = 1; 
        $regex_pattern = get_shortcode_regex();
        preg_match ('/'.$regex_pattern.'/s', $post->post_content, $regex_matches);
        if ($regex_matches[2] == 'nggallery') :
            //  Found a NextGEN gallery find out what ID
            //  Turn the attributes into a URL parm string
            $attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
            $attribureStr = str_replace ('"', '', $attribureStr);

            //  Parse the attributes
            $defaults = array (
                'preview' => '1',
            );
            $attributes = wp_parse_args ($attribureStr, $defaults);

            if (isset ($attributes["id"])) :
                $galleryID = $attributes["id"];
            endif;
            if (isset($attributes["preview"])) :
                $previewIndex = $attributes["preview"];
            endif;
        endif;
        ?>

What needed to be tweaked was the handling of the parameters. Using trim instead of some secret charter and switching to wp_parse_args to correctly handle the short code parameters. Once the above code finishes executing in a WP loop $galleryID will hold the NextGEN gallery ID and previewIndex will be set to the preview index or 1 if no previewIndex attribute was present.

previewIndex is an attribute I "added" to indicate what thumbnail to use for the gallery preview. NextGEN ignores it and the galleries render as normal but now I can use it for my Theme to display a specific icon in the preview entry.

Here is the code from my loop-index.php and loop-category.php that handles creating the gallery preview:

            <?php /* Enhance the content preview with an image from the NextGEN gallery */ ?>
            <?php
            global $nggdb;
            $gallery = $nggdb->get_gallery ($galleryID, 'sortorder', 'ASC', true, 0, 0);
            $image = $gallery[$previewIndex];
            $total_images = count ($gallery);
            ?>

            <?php if (isset($image)  &&  isset($image->thumbURL)) : ?>
                <?php /* Show the thumbnail */ ?>
                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                    <img class="entry-preview-image" src="<?php echo $image->thumbURL ?>" align="left" />
                </a>
            <?php endif; ?>

            <?php /* Show the text excerpt */ ?>
            <?php the_excerpt(); ?>

            <?php /* Show the statement of number of images contained */ ?>
            <em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, 'twentyten' ),
                'href="' . get_permalink() . '" title="' . sprintf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
                number_format_i18n( $total_images )); ?>
            </em>
        <?php endif; ?>

This makes use of information from another answer regarding accessing NextGEN gallery objects in order to get the thumbnail and the count of images in the gallery.

Either way you can do it very easily. First Hook a Function. I used wp_head but you can use anyone you want.

add_action( 'wp_head', 'find_shortcode_attribute' );

function find_shortcode_attribute() {
global $post;

if( has_shortcode( $post->post_content, 'nggallery' ) ) {
    preg_match_all( '/' . get_shortcode_regex() . '/', $post->post_content, $matches, PREG_SET_ORDER );

    echo "<pre>";
    print_r( $matches );
    echo "</pre>";

    // you will get the attribute on $matches[3]
    // do whatever you want here.

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744914513a4600753.html

相关推荐

  • loop - How do I get the attributes of a short code from a post?

    I am trying to preview a NextGen gallery on my main page (and category pages) to show a single image from the gallery on

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信