wecobble / Subtitles

Add subtitles into your WordPress posts, pages, custom post types, and themes. No coding required. Simply activate Subtitles and you're ready to go.
http://wordpress.org/plugins/subtitles/
GNU General Public License v2.0
117 stars 186 forks source link

Not working with `single_post_title()` #57

Closed webmandesign closed 9 years ago

webmandesign commented 9 years ago

Hi Philip,

I've recently noticed that the subtitles don't work on single_post_title() function. I use it on blog page to display the correct blog title (the page is not front page). Could you have a look at that please?

Thanks and regards,

Oliver

philiparthurmoore commented 9 years ago

Hi Oliver,

The plugin should work on that function. See this. Can you give me more information about your setup? It could be that you're using this function outside of the Loop.

Cheers, Philip

webmandesign commented 9 years ago

Hi Philip,

I use it the very similar way (the same way, actually) as in Underscores' index.php file. The function is outside the loop in there too.

If it needs to be in a loop, I can still modify my index.php or use a filter hook, so no worries. I just thought this is a bug.

Regards,

Oliver

philiparthurmoore commented 9 years ago

The function is outside the loop in there too.

No, the function in _s is inside of a Loop. Either way, you can totally use this outside of your Loop; you'll just need to use one of the built-in template tags.

Closing this out as it's not a plugin bug.

Cheers!

webmandesign commented 9 years ago

Hi Philip,

Thanks for explanation and help.

Doesn't the loop starts with while? I thought so ;)

Regards,

Oliver

philiparthurmoore commented 9 years ago

Oops. You're totally right. I missed https://github.com/Automattic/_s/commit/955cd140f8de7f56f3ac6b0af8279237cfa469a3 when we added it into _s. :)

webmandesign commented 9 years ago

So, I understand, this wouldn't work in _s either. For that particular use it is actually managable as we can find out the home (posts list) page ID. I will continue with custom solution then.

Thanks again!

philiparthurmoore commented 9 years ago

A custom solution is super-easy. It'd just be this:

            <?php if ( is_home() && ! is_front_page() ) : ?>
                <header>
                    <h1 class="page-title screen-reader-text">
                        <?php single_post_title(); ?>
                        <?php
                            if ( function_exists( 'the_subtitle' ) ) {
                                the_subtitle( '<p class="entry-subtitle">', '</p>' );
                            }
                        ?>
                    </h1>
                </header>
            <?php endif; ?>

Is that not working?

webmandesign commented 9 years ago

That actually wouldn't work as on is_home() page it picks up the subtitle from the first post in the list.

Here is my custom solution that works when using single_post_title() outside the loop:

/**
 * Subtitles support in single post title outside the loop
 *
 * @param  string $title
 * @param  object $post
 */
function themeslug_subtitles_single_post_title( $title, $post ) {

    // Processing

        if (
                function_exists( 'get_the_subtitle' )
                && ! doing_action( 'wp_head' )
                && ! in_the_loop()
            ) {

            $subtitle = get_the_subtitle( absint( $post->ID ) );

            if ( ! empty( $subtitle ) ) {

                $title  = '<span class="entry-title-primary">' . $title . '</span>';
                $title .= ' <span class="entry-subtitle">' . $subtitle . '</span>';

            }

        }

    // Output

        return $title;

} // /themeslug_subtitles_single_post_title

add_filter( 'single_post_title', 'themeslug_subtitles_single_post_title', 10, 2 );
philiparthurmoore commented 9 years ago

Boom, there ya go. Thanks for sussing this out and sharing. I'll roll these into the docs and make sure to share this thread with anyone else who runs into a similar issue. Cheers!