Title: PHP container
Last modified: January 12, 2017

---

# PHP container

 *  Resolved [willywhy](https://wordpress.org/support/users/willywhy/)
 * (@willywhy)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/php-container/)
 * 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](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/php-container/#post-8648416)
 * 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](https://codex.wordpress.org/Function_Reference/post_class).
   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.

 * ![](https://s.w.org/plugins/geopattern-icon/kia-subtitle_f6f5f6.svg)
 * [KIA Subtitle](https://wordpress.org/plugins/kia-subtitle/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/kia-subtitle/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/kia-subtitle/)
 * [Active Topics](https://wordpress.org/support/plugin/kia-subtitle/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/kia-subtitle/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/kia-subtitle/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/php-container/#post-8648416)
 * Status: resolved