Add page and post specific content between title and content

Community Forums Forums Design Tips and Tricks Add page and post specific content between title and content

This topic is: resolved

This topic contains 9 replies, has 2 voices, and was last updated by  braddalton 1 year, 1 month ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #41473

    csbeck
    Participant
    Post count: 135

    I have a website (testing PAGE – http://acquireb2bdev.com/products) that the client wants to have some custom content on each page and perhaps each post between the title and the content of the page. I’m using Agency for the child theme.

    Thanks to Brad Dalton who helped hook some content between the title and the content (http://www.studiopress.com/forums/topic/add-excerpt-after-page-post-entry-title/).

    This is currently just some static content though. I need there to be some way for the client to add content for each page and post that this appears. I had originally thought that I could do this by showing the EXCERPT. Then I thought perhaps it would be good to use CUSTOM FIELDS. This needs to be fairly easy for the customer to change the text for each page/post. I believe it would be more efficient and intuitive for the customer to change that content on that particular page/post.

    Can someone help either with the code in the functions.php or someway to get this content on the page.

    [php]function after_title_text() {
    if(is_page() || is_single() )
    echo ‘<div class="single-title">Add content after your titles but before your content here</div>’;
    };
    add_action(‘genesis_after_post_title’, ‘after_title_text’);[/php]

    I guess the only other way to do this would be to create a custom-styled paragraph in the content. If I can’t get an Excerpt or Custom Fields to work, then I’ll fall back on that method. Perhaps I’m complicating this.

    I appreciate your assistance or opinion.

    Chris

    #41477

    braddalton
    Participant
    Post count: 8849

    Hi Chris

    This code creates a custom field named chris.

    All you need to do then is select it on all edit post/page screens using the native custom fields built into WordPress and then add a value which basically means adding the text content you want to output in the hook position, which is genesis_after_ post_title.

    Code not displaying? Grab it on Github.

    You’re right that a custom field would be best but it would be even easier for your client to use a custom meta box (with a custom field) which you can position directly under the editor. That way they simply type in content and its added to the hook position included in the function above.

    I’ll find a plugin that creates this meta box and get back to you soon.

    Or you could simply remove all the default custom fields and only display yours.

    Thanks for the promotion!!! haha and i also added some extra braces in the code!


    Specializing In StudioPress Child Theme Customization.
    500+ StudioPress Tutorials

    #41506

    csbeck
    Participant
    Post count: 135

    Brad you are the man! I knew there just had to be a way. Thanks so much for your continued effort on this! And with the extra braces ;) – haha.

    This works like a charm. I believe the client will be able to understand this. True though – I agree it would be better to use a custom meta box. That would be great. I have attempted custom post types on another trial project so I see the ability and the power of doing that but didn’t want to go with complete custom post types since it’s still basically a page or post. I can only imagine there might be some few lines of code somewhere that could add a custom meta box without going the route of custom post types. Yes, please let me know if you find anything. I will also do some searching.

    Thanks again! You’ve been very helpful.

    #41507

    csbeck
    Participant
    Post count: 135

    Looks like WP has custom meta boxes:

    http://codex.wordpress.org/Function_Reference/add_meta_box

    Looks a bit hairy and complicated. I’m sure I could copy the entire code sample and put it in my functions.php. I’ll try it and see what happens. I’m just a little confused at how to grab the content from that custom meta box. i.e. how to reference it. I’ll give it a go and let you know what I come up with (meaning, if it crashes me or not).  ;)

     

    #41511

    csbeck
    Participant
    Post count: 135

    Ok, I’ve used that code from the WP Codex and added your function and got it to work.!

    I only made changes to a couple places.

    Your code, I added the call to the meta item:

    [php]add_action( ‘genesis_after_post_title’, ‘custom_field_before_content’ );
    function custom_field_before_content() {
    if(is_page() || is_single() ) {
    genesis_custom_field(‘subtitle_text’);
    }
    }[/php]

    The new code from the Codex, I changed the name of the field:

    [php]  // The actual fields for data entry
    // Use get_post_meta to retrieve an existing value from the database and use the value for the form
    $value = get_post_meta( $post->ID, ‘subtitle_text’, true );
    echo ‘<label for="myplugin_new_field">’;
    _e("Please enter the custom text that follows the page title.", ‘myplugin_textdomain’ );
    echo ‘</label> ‘;
    echo ‘<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="’.esc_attr($value).’" size="80" />’;[/php]

    and the final line of code for what to do with it:

    [php]   // Do something with $mydata
    update_post_meta($post_ID, ‘subtitle_text’, $mydata);[/php]

    When I have time, I’ll add a Gist or Github.

    My wishlist would be to have it use a wysiwyg editor. I’m sure a plugin or more code would do that. I’ll keep my eyes open for that.

    Chris

    #41521

    csbeck
    Participant
    Post count: 135

    I’ve created a Gist of the code that I’m using with the addition of Brad’s code as well. I’ve put this in the bottom of my functions.php file. Hopefully it will help someone else. I will still note if I find a suitable and helpful plugin that performs this function while giving a wysiwyg editor.

    https://gist.github.com/csbeck/5605378

    Chris

    #41525

    braddalton
    Participant
    Post count: 8849

    Good job Chris.

    I’ll modify this code and increase the height of the box as well as reposition it so its the first module under the editor.

    I know Pippin has a plugin for creating meta boxes.


    Specializing In StudioPress Child Theme Customization.
    500+ StudioPress Tutorials

    #41529

    braddalton
    Participant
    Post count: 8849

    This plugin will help you customize the backend, reposition the meta boxes and hide anything you like based on user roles.

    http://wordpress.org/extend/plugins/adminimize/


    Specializing In StudioPress Child Theme Customization.
    500+ StudioPress Tutorials

    #43414

    csbeck
    Participant
    Post count: 135

    Hey Brad,

    One more item with this feature. When the code gets the content from the new field, it sanitizes it – strips off any html markup. I’d like to make it possible to add markup in that field. Can you help resolve that so any html code is viable? Or perhaps make the field have a wysiwyg editor and not have the field get sanitized?

    I ultimately want to be able to stylize the text that is there with <p> (because I’ll need to indent it more than the parent div) and possibly give some of the words tags like <strong> and <em>.

    Thanks so much for your help!

    Chris

    #43430

    braddalton
    Participant
    Post count: 8849

    Hi Chris

    Install the Advanced custom fields plugin and create a custom met box.


    Specializing In StudioPress Child Theme Customization.
    500+ StudioPress Tutorials

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.