• Resolved Joe Greenwood

    (@jgreenwood)


    Hello, I am trying to add a secondary “subtitle” to my posts and pages. It will appear on a second line just below the post/page title.
    Like this:
    Fender American Standard Telecaster <— title
    Perhaps the greatest guitar ever made <— subtitle

    I am using a child theme of the Thematic theme. Inside my child theme’s functions.php, I have this:

    //  add subtitle to posts and pages
    function vg_subtitle($posttitle) {
    $subtitle = get_post_meta ($post->ID, 'subtitle', $single = true);
    if($subtitle !== '') echo $posttitle . ' - ' . $subtitle;
    }
    add_action('thematic_postheader_posttitle','vg_subtitle');

    When I view my page in browser, it shows everything but the value of $subtitle.

    Fender American Standard Telecaster

    By admin | Published: June 14, 2010 | Edit

    Anyone know why my variable $subtitle isn’t being recognized? Or maybe it is being recognized but is not displaying in browser?

    Thanks very much for any help. I am only a beginner in variables and functions. Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • I think this is what you want:

    //  add subtitle to posts and pages
    function vg_subtitle($posttitle) {
       $subtitle = get_post_meta ($post->ID, 'subtitle', true);
       echo $posttitle;
       if ($subtitle) echo ' - ' . $subtitle;
    }
    add_action('thematic_postheader_posttitle','vg_subtitle');

    This will display the title even if there is no subtitle, but will not show the dash unless there is one.

    Thread Starter Joe Greenwood

    (@jgreenwood)

    Thanks for the reply, vtxyzzy … I like your code better than my previous code because it only shows the dash if there’s a subtitle.

    Unfortunately though the value for $subtitle is still not showing on the browser page.

    I looked at the database to make sure the value is in the table, and it is:

    meta_id post_id meta_key meta_value
    9371 1662 ArticleID 54321
    9369 1662 Subtitle here’s the subtitle
    9344 1662 _edit_last 1
    9343 1662 _edit_lock 1276931207

    Note the meta_key starts with a capital letter ‘Subtitle’, so I adjusted the variable declaration in the php code.

    //  add subtitle to posts and pages
    function vg_subtitle($posttitle) {
       $subtitle = get_post_meta ($post->ID, 'Subtitle', true);
       echo $posttitle;
       if ($subtitle) echo ' - ' . $subtitle;
    }
    add_action('thematic_postheader_posttitle','vg_subtitle');

    I just cannot figure out why the value isn’t being retrieved. I see in the WordPress Codex and in some online tutorials mention of placing this code in The Loop. I’m pretty sure my function is in “The Loop” because I’m adding it to ‘thematic_postheader_posttitle’, which is called from ‘thematic_postheader’, which is called from ‘thematic_index_loop’, which appears to have the adequate ‘Loop’ structure (and everything else it’s calling does work) …

    Here’s ‘thematic_postheader_posttitle’, from ‘wp-content/themes/thematic/library/extensions/content-extensions.php’:

    // Create post title
    function thematic_postheader_posttitle() {
    
        if (is_single() || is_page()) {
            $posttitle = '<h1 class="entry-title">' . get_the_title() . "</h1>\n";
        } elseif (is_404()) {
            $posttitle = '<h1 class="entry-title">' . __('Not Found', 'thematic') . "</h1>\n";
        } else {
            $posttitle = '<h2 class="entry-title"><a href="';
            $posttitle .= get_permalink();
            $posttitle .= '" title="';
            $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
            $posttitle .= '" rel="bookmark">';
            $posttitle .= get_the_title();
            $posttitle .= "</a></h2>\n";
        }
        return apply_filters('thematic_postheader_posttitle',$posttitle); 
    
    } // end thematic_postheader_posttitle

    But, is it possible I’m adding this ‘vg_subtitle’ action incorrectly? Or that I have something missing in the variable declaration that I need for the value to be retrieved?

    Any help is greatly appreciated. Thank you.

    Thread Starter Joe Greenwood

    (@jgreenwood)

    I should also mention that the dash character is not displayed, even on a post that has a subtitle value. This indicates, to me, that this line is not storing a value for $subtitle.

    $subtitle = get_post_meta ($post->ID, 'Subtitle', true);

    Am I declaring the variable $subtitle correctly? And am I retrieving the value correctly?

    Thanks.

    Maybe you need to declare $post as a global:

    //  add subtitle to posts and pages
    function vg_subtitle($posttitle) {
       global $post;
       $subtitle = get_post_meta ($post->ID, 'Subtitle', true);
       echo $posttitle;
       if ($subtitle) echo ' - ' . $subtitle;
    }
    add_action('thematic_postheader_posttitle','vg_subtitle');
    Thread Starter Joe Greenwood

    (@jgreenwood)

    YESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS!

    vtxyzzy, you rock!

    it works now

    wow

    I learned a lot exploring that issue for the past twelve hours!

    now I think I’ll learn a bit more about when to declare a global variable, what that means, why .. etc..

    thank you so much, vtxyzzy … may your days be filled with joy

    You are most welcome!

    How did you implement this? I am looking to do just that, you mind explaining how this works? (for those of us who are slow php learners)

    If I add this to my functions, then I have to call it in my theme? how? (also in Thematic)

    Thanks for your time!

    If you add the code below to your functions.php, it gets called by the Thematic theme. You do not need to add a call.

    //  add subtitle to posts and pages
    function vg_subtitle($posttitle) {
       global $post;
       $subtitle = get_post_meta ($post->ID, 'Subtitle', true);
       echo $posttitle;
       if ($subtitle) echo ' - ' . $subtitle;
    }
    add_action('thematic_postheader_posttitle','vg_subtitle');
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Add Subtitle using Custom Fields and functions.php’ is closed to new replies.