Support » Plugin: KIA Subtitle » PHP container

  • Resolved willywhy

    (@willywhy)


    Hi Kathy,
    I have used your PHP snippet to insert the subtitle into my page which works great. I am wondering if it is possible to do so in a way that inserts a container of some kind even when there is no subtitle to pull. When there is no subtitle, the content below moves up to fill that spot but I’d like to keep the spacing. (ie. I want scenario C rather than B when there is no subtitle. That way it matches the spacing of Scenario A). Thanks so much! Happy to donate to your plugin if you have a quick fix for this. My PHP skills are copy and pastey :-).

    A.
    Title
    Subtitle
    Content

    B.
    Title
    Content

    C.
    Title

    Content

Viewing 1 replies (of 1 total)
  • Plugin Author helgatheviking

    (@helgatheviking)

    I think there are two ways of going about this. The first method is preferred because you don’t clutter up the HTML with empty DIVs. It involves filtering the post classes. Hopefully your theme follows best coding practices and includes the post_class() function. You can add the following to your theme’s `functions.php and it should check each post for the existence of a subtitle and add the appropriate class name to the post.

    add_filter( 'post_classes', 'kia_add_subtitle_post_class', 10, 3 );
    function kia_add_subtitle_post_class( $classes, $class, $post_id ) {
    	if( function_exists( 'get_the_subtitle' ) && get_the_subtitle( $post_id ) ) {
    		$classes[] = 'has-subtitle';
    	} else {
    		$classes[] = 'no-subtitle';
    	}
    	return $classes;
    }

    From there you can now style the two different post divs differently.

    .has-subtitle h1.title { margin-bottom: 0 ; }
    .no-subtitle h1.title { margin-bottom: 1em; }
    

    I don’t know the markup of your theme so it’s up to you to get the right selector, but that’s an example.

    Alternatively, you could do a similar check to determine if the post has a subtitle and if it does, display the subtitle, and if it does not, add some empty markup and style that.

    if( function_exists( 'get_the_subtitle' ) && get_the_subtitle() ) {
    	the_subtitle( '<h3 class="the-subtitle"', '</h3>' );
    } else {
    	echo '<div class="spacer"></div>';
    }
Viewing 1 replies (of 1 total)
  • The topic ‘PHP container’ is closed to new replies.