0

On the frontpage of a blog I want a piece of text (like an excerpt) under the title but I don't want is showing up in the full post.

Any ideas how I would go about this?

EDIT: Wasn't precise enough in my question. The piece of text under the title will be different for every post and has to be able to be set in the backend by the end user, preferably when typing the post.

1
  • What theme are you using? Commented Feb 22, 2014 at 23:59

4 Answers 4

0

Tested this and it worked perfectly on Genesis.

You can also use HTML tags in the custom field.

add_action( 'genesis_entry_header', 'content_after_archive_title', 15 );

function content_after_archive_title() {

if ( is_home() && genesis_get_custom_field('after-title') ) :

echo '<div class="after-title">'. genesis_get_custom_field('after-title') .'</div>';

endif;

}

Uses the native custom fields meta box which you can reposition under the Editor.

enter image description here

You could also hand code a meta box for this using the edit_form_after_title hook if you want the box before the editor or use a plugin like ACF.

If your theme doesn't include a hook for the title or post info meta, you could use the content or post meta filter or action hook instead.

Change Position: If you want it before the post info, simply change the 3rd parameter to 11 and here's the result:

enter image description here

1

You actually could use the user-generated excerpt for this. Just be sure to add a user-generated excerpt for each post, then do something like so:

<h1><?php the_title(); ?></h1>
<div>
<?php 
if ( is_front_page() ) { 
    the_excerpt(); 
} else {
    the_content();
}
?>
</div>
0

Find the code block responsible for the post title and excerpt. Then simply use the following:

<h2 class="entry-title"><?php the_title(); ?></h2>
<?php
//here is your code block
if( is_front_page() || is_home() ) {
   echo '<div>My text block here</div>';
}
?>

Reference:

1
  • Hi Mayeenul, thanks for your quick answer! I wasn't elaborate enough in my question. I want different text for each blog post and the end user has to be able to set it in the backend himself in the post area. Commented Feb 22, 2014 at 17:06
0

There is a hook called edit_form_after_title that seems built for this case. That plus a custom meta field should do it.

function after_title_cb($post) {
  $af = get_post_meta($post->ID,'_after_title',true);
  echo '<input id="aftertitle" name="_after_title" value="'.$af.'">';
}
add_action('edit_form_after_title','after_title_cb');

function save_after_title( $post_id ) {

  $mydata = (!empty($_POST['_after_title'])) 
    ? wp_kses($_POST['_after_title'])
    : '';

  update_post_meta( $post_id, '_after_title', $mydata );
}
add_action( 'save_post', 'save_after_title' );

You can even wrap the whole thing in a standard metabox if you'd like.

function after_title_cb($post) {
  $af = get_post_meta($post->ID,'_after_title',true);
  echo '<input id="aftertitle" name="_after_title" value="'.$af.'">';
}

function add_before_editor($post) {
  global $post;
  add_meta_box(
    'generic_box', // id, used as the html id att
    __( 'Text Only Content' ), // meta box title
    'after_title_cb', // callback function, spits out the content
    'post', // post type or page. This adds to posts only
    'pre_editor', // context, where on the screen
    'high' // priority, where should this go in the context
  );
  do_meta_boxes('post', 'pre_editor', $post);
}
add_action('edit_form_after_title','add_before_editor');

function save_after_title( $post_id ) {

  $mydata = (!empty($_POST['_after_title'])) 
    ? wp_kses($_POST['_after_title'])
    : '';

  update_post_meta( $post_id, '_after_title', $mydata );
}
add_action( 'save_post', 'save_after_title' );

On the front end you'd grab and display the value with something like this:

// careful with $post
// This must be in a Loop to be reliable
$af = get_post_meta($post->ID,'_after_title',true); 
if ( !empty($af) ) { 
  echo $af;
} 
5
  • I think the edit_form_after_title hook is only used for the backend. Commented Feb 23, 2014 at 0:14
  • @BradDalton : It is a back-end hook, but part of the question is that this "has to be able to be set in the backend by the end user, preferably when typing the post." Most of the effort here is meeting that criteria. The front end part is easy-- see the last block of code in my answer.
    – s_ha_dum
    Commented Feb 23, 2014 at 0:16
  • Got it. You're using it to add the meta box in that hook position. Commented Feb 23, 2014 at 7:54
  • Thanks for your solution! I used Brad's solution so I made that the correct answer but yours would work fine too. Commented Feb 23, 2014 at 23:47
  • @FullContactCoder : That solution is specific to Genesis. Are you using Genesis?
    – s_ha_dum
    Commented Feb 23, 2014 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.