Tony States
Tony States

Reputation: 189

how to add more text below blog title in WordPress

I would like to add some text below my blog titles, I have researched this most of the day and cant figure out how to do it, I am not an expert at PHP, but I have a basic understanding of where all the php files need to be and I already have a child theme which I built with a functions.php file in my child theme folder. my child theme is built on WordPress Twenty-Eleven!

Here's a screenshot:

enter image description here

the site I am trying to do this for is here

Upvotes: 0

Views: 6879

Answers (2)

Joe Buckle
Joe Buckle

Reputation: 871

There is a way to add this simple functionality without installing a plugin.

Unless I'm reading this wrong, all you want to do is add a new meta box where you can insert a sub title and it be displayed on that post.

In functions.php, add this to create a meta box to house your sub title field

function your_sub_title() {
    add_meta_box('your_sub_title_metabox', 'Edit Sub Title', 'your_sub_title_metabox', 'post', 'normal', 'default'); ## Adds a meta box to post type
}

Now also in functions.php add the code for your new field

function your_sub_title_metabox() {

    global $post; ## global post object

    wp_nonce_field( plugin_basename( __FILE__ ), 'your_sub_title_nonce' ); ## Create nonce

    $subtitle = get_post_meta($post->ID, 'sub_title', true); ## Get the subtitle

    ?>
    <p>
        <label for="sub_title">Sub Title</label>
        <input type="text" name="sub_title" id="sub_title" class="widefat" value="<?php if(isset($subtitle)) { echo $subtitle; } ?>" />
    </p>
    <?php
}

The next thing you have to do is create your save function. Also in functions.php

function sub_title_save_meta($post_id, $post) {
    global $post;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return false; ## Block if doing autosave

    if ( !current_user_can( 'edit_post', $post->ID )) {
        return $post->ID; ## Block if user doesn't have priv
    }

    if ( !wp_verify_nonce( $_POST['your_sub_title_nonce'], plugin_basename(__FILE__) )) {


    } else {
        if($_POST['sub_title']) {
            update_post_meta($post->ID, 'sub_title', $_POST['sub_title']);
        } else {
            update_post_meta($post->ID, 'sub_title', '');
        }
    }

    return false;

}
add_action('save_post', 'sub_title_save_meta', 1, 2);

Then, just below your the_title() in your single.php template

...
the_title();
$subtitle = get_post_meta(get_the_ID(), 'sub_title', true);
if(isset($subtitle)) {
  echo $subtitle;
}

Upvotes: 3

joshrathke
joshrathke

Reputation: 7774

I would suggest looking into a plugin that will allow you add a subtitle, or something like that, which means you will need to create a custom field so that you can add text under the title on a post to post basis. I would highly recommend Meta-Box.

To install Meta-Box all you have to do is install the Meta-Box plugin like you would any other plugin from your dashboard. Once you have done that create a new file called "custom-meta.php" in your theme directory and copy this code.

    <?php

$prefix = '';
global $meta_boxes;
$meta_boxes = array();

// Custom Post Info Meta Box
$meta_boxes[] = array(

    'id' => 'custom_post_info',
    'title' => __( 'Custom Post Info', 'rwmb' ),
    'pages' => array( 'post', 'page' ),
    'context' => 'normal',
    'priority' => 'high',
    'autosave' => true,
    'fields' => array(
        array(
            'name'  => __( 'Subtitle', 'rwmb' ),
            'id'    => "{$prefix}subtitle",
            'desc'  => __( 'Enter Subtitle Here', 'rwmb' ),
            'type'  => 'text',
            'std'   => __( '', 'rwmb' ),
            'clone' => false,
        ),
    ),
);


/********************* META BOX REGISTERING ***********************/

/**
 * Register meta boxes
 *
 * @return void
 */
function register_meta_boxes()
{
    if ( !class_exists( 'RW_Meta_Box' ) )
        return;

    global $meta_boxes;
    foreach ( $meta_boxes as $meta_box )
    {
        new RW_Meta_Box( $meta_box );
    }
}
add_action( 'admin_init', 'register_meta_boxes' );

Then update your functions file to include the "custom-meta.php" file by adding this line somewhere that makes sense.

include 'custom-meta.php';

Then go back into your posts and check and see if you have a new box to insert a subtitle into. If you do, great!

And then after you have a custom field on each post, all you need to do is pull that data into the theme template. You will need to go into your single.php file, and fine this line of code.

<?php the_title(); ?>

And add this below it. I doubt this would be a problem, but for the helper function to work, it needs to be within the loop.

<?php echo rwmb_meta('subtitle'); ?>

Upvotes: 0

Related Questions