• I have a problem with setting the page title.
    I have a photo gallery plugin, with a shortcode handler. When that shortcode is placed on a page, this page acts as my photo gallery, based on query vars.

    //register some query vars
    //....
    //init shortcode
    add_shortcode( 'mygallery', 'shortcodeHandler' );
    function shortcodeHandler(){
    //retrieve pictures from db and display them on screen based on various query vars(page, album,photoid etc)
    }

    My problem is that i cannot find a way to set the page title from the shortcode handler itself. To show album title, photo title etc
    The only way i can do this is by
    add_filter( 'wp_title'...
    with a filter handler.
    But this way i have to redo all the queries i made in the shortcode handler based on the query vars.

    So, what i want is different page title for gallery page, album page, photo page etc. I display all these pages using the same shortcode + various query vars params

    How would you solve this?
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    By the time the shortcode handler executes it’s too late to effect the title. You could use the same query var logic as in the shortcode handler to affect the title from the ‘wp_title’ filter callback. I don’t understand why you would have to redo all the queries – you’re just checking them for certain values, not changing them, right?

    But what I probably would do is develop an entirely separate custom page template for my photo galleries instead of trying to alter what the default one does.

    Thread Starter gstan07

    (@gstan07)

    Hi bcworkz, thanks for your answer!

    Let me give you an example of why i’m redoing queries.
    if album_id query param is present, we’re dealing with a “Album page” so i have to query for the Album title, number of photos etc.
    In the wp_title filter callback i would have to redo at least the query for the album title. Otherwise, all my album pages will have the same page title.

    By developing separate custom pages you mean creating different pages for, let’s say, main gallery, album view and photo view? (with different shortcodes or different shortcode args). This will help a bit but still does not solve the “different title for each album” problem.

    Moderator bcworkz

    (@bcworkz)

    Ah, I see. You shouldn’t redo the query, you should do a custom secondary query, which I suspect is really the same thing, except you’re failing to call wp_reset_postdata() so that the original query remains valid.

    My approach would involve a single page template. The code on that template would be adequate to deal with all the various views, assuming they all have similar structure, only the content is different.(A single template could also handle multiple structures, FWIW) Exactly what it does for each view is determined from the query vars. You don’t really need shortcodes because what the shortcode handler does could be done by the template code. Since you have shortcodes already though, you can make use of them with do_shortcode() or by calling the handler directly.

    You would still need to create at least one page based on the template, that may be enough for all conditions. A single page slug is used to access the content, but the title and content the user sees is determined by the query vars. Alternately, additional rewrite rules could obscure that single slug so that even the permalinks relate to the content displayed, the user doesn’t see the actual page slug.

    Instead of a page template, this custom template might instead be a supplemental theme template. Depending on the nature of the data, the theme could automatically use such a template, provided it’s named properly. See Template Hierarchy.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Shortcodes and page title’ is closed to new replies.