Forums

[resolved] Shortcode only run on one post per page? (6 posts)

  1. dizee
    Member
    Posted 1 year ago #

    I am dealing with legacy code here so I can't really alter the shortcode - only the code that parses it out.

    I have the following shortcode:

    [gallimg:LARGE_FULL_URL,THUMB_FULL_URL,TITLE]

    I wrote a plugin to generate a gallery of each image and strip all the breaks it generates when removing them

    http://pastebin.com/njuwReig

    It works fine, but for the first post only. On the post listing page, only the first post will show the gallery and the others just show nothing (all shortcode and breaks are stripped).

    What am I doing wrong???

    The code was copied from another plugin and I tried to hack it in a while back (for the first version of the site, I don't really know what I'm doing with the shortcode parsing... as you can see) and now I know how to use the actual gallery... but I can't go back and edit all the posts - they need to stay as is.

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    without seeing the output of the code in your site:

    in the
    function show_legacy_gallery(){

    try to use string concatenation and a return at the end, instead of using echo for each element.

    (using echo in connection with shortcode is often the cause for problems)

  3. dizee
    Member
    Posted 1 year ago #

    I'm not sure what you mean about seeing the code... it is exactly what is in the function, except of course with the $gallery_id with is a php uniqid. The first post shows that code and the rest show nothing... nada... zip...

    Unfortunately putting the string together and returning it at the end didn't help :( Thanks for the tip, though!

    Any other ideas?

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    function galleries_shortcode($attr){
            global $gallery_displayed;
            if (!$gallery_displayed){
                    $gallery_displayed = true;
                    return show_legacy_gallery() ."\n";
            }
    }

    what does this code do?
    or rather:
    what is the variable $gallery_displayed supposed to control?

    -to me it seems to be there to show the gallery only once;

    you are setting the variable $gallery_displayed to 'false' only once (in line 6) - then the code above sets it to 'true' and prevents itself from showing the gallery again.

  5. dizee
    Member
    Posted 1 year ago #

    THANK YOU for the hint! That is essentially what the problem was... It actually needed to be reset back to false for every post, and the image arrays needed to be reset.

    I modified this function to reset everything...

    function gallery_content($content){
    	global $gallery_displayed, $full_images, $thm_images, $captions, $gallery_id;
    
    	//reset all the gallery info
    	$gallery_id = false;
    	$gallery_displayed = false;
    	$full_images = Array();
    	$thm_images = Array();
    	$captions = Array();
    
    	...
    }

    Again, thank you for bringing this to my attention! :)

  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    i am glad it is working ;-)

    please mark this thread as 'resolved' if you don't have any further questions ...

Topic Closed

This topic has been closed to new replies.

About this Topic