Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
hello, I thought this would be straightforward - but it isn't!

I have a clonable template - art-and-poetry-projects.php. Within this template is a repeatable region for uploading images to create a lightbox.

Each art-and-poetry-projects.php page is shown on a listing page. A page view may or may not be required.

My objective is to create a shortcode so that the client can inject a link to launch a lightbox gallery wherever they wish. So this may be from the page in list view or it may be from the page in page view - if the page requires a detail page of its own.

The problem is that the page ID is not available to the shortcode in list view ... I don't know how to get hold of it. Here is the shortcode:

Code: Select all
$FUNCS->register_shortcode( 'lightbox', 'lightbox_handler' );
function lightbox_handler( $params, $content=null ){
    global $FUNCS, $CTX;
    extract( $FUNCS->get_named_vars(array(
        'linktext' => '',
        'linkstyle' => ''
    ), $params) );

    $pageid = $CTX->get('k_page_id');
   
    $html =<<<EOS
    <cms:pages masterpage='art-and-poetry-projects.php' show_future_entries='1' id='$pageid' limit='1'>
        <cms:show_repeatable 'lightbox_photos' >           
            <cms:if show_lightbox_photo eq 'yes' >
               <a class="lightbox-link<cms:if k_count='1'> lightbox-link$linkstyle</cms:if>" href="<cms:show lightbox_photo />" <cms:if k_total_records gt '1'>data-lightbox-gallery="gallery<cms:show k_page_id/>"</cms:if> <cms:if "<cms:not_empty lightbox_photo_title />">title="<cms:show lightbox_photo_title/>"</cms:if>><cms:if k_count='1'>$linktext</cms:if></a>
            </cms:if>                   
        </cms:show_repeatable>
    </cms:pages>
EOS;

    return $FUNCS->embed( $html, $is_code=1 );
}
}


Heeeelp - anyone?
Hi potato,
The problem is that the page ID is not available to the shortcode in list view..

In a list-view there is no page and so there is no page_id - that is a given.
To bring a page (or multiple pages) in context we use the cms:pages loop. I'm sure that is what you'd be doing.

I tested your code from within the cms:pages loop and found that your shortcode gets the ID of the pages being iterated just fine. It can also access the data from the repeatable-regions just fine.

To see it for yourself, comment out the red highlighted line and put in the green one in its place -
...
return $html;
//return $FUNCS->embed( $html, $is_code=1 );
}

Now the shortcode will simply output the raw Couch code it fabricates (instead of getting it executed by $FUNCS->embed() ).

Do a view:source and examine the generated code. Following is a part of what I got -
Code: Select all
<cms:pages masterpage='art-and-poetry-projects.php' show_future_entries='1' id='451' limit='1'>
    <cms:show_repeatable 'lightbox_photos' >           
        <cms:if show_lightbox_photo eq 'yes' >
           <a class="lightbox-link<cms:if k_count='1'> lightbox-link</cms:if>" href="<cms:show lightbox_photo />" <cms:if k_total_records gt '1'>data-lightbox-gallery="gallery<cms:show k_page_id/>"</cms:if> <cms:if "<cms:not_empty lightbox_photo_title />">title="<cms:show lightbox_photo_title/>"</cms:if>><cms:if k_count='1'></cms:if></a>
        </cms:if>                   
    </cms:show_repeatable>
</cms:pages>

You'll have to carefully study and see if the code should give the results you intended it to.
You can paste it straight into the template's list-view to debug.

Bottom line - the PHP code is ok. You need to re-examine the Couch code it dynamically generates.

Hope it helps.
Thanks for your help troubleshooting this @KK .... I swapped in return $html; - but still didn't get the page ID

But I looked at my Couch template and discovered that the problem was that I had put <cms:do_shortcodes> </cms:do_shortcodes> outside <cms:pages> </cms:pages>

It didn't occur to me that this would cause a problem .... doh! .... THANKS!!!
As I mentioned, @potato -
In a list-view there is no page and so there is no page_id - that is a given.
To bring a page (or multiple pages) in context we use the cms:pages loop.

I'm glad you could resolve the problem.
4 posts Page 1 of 1