Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a second title to a Custom Post in WordPress?

Tags:

php

wordpress

At the moment I am just learning about Custom Post Types so I am unsure how to proceed from here.

I created a custom post for events, it is almost identical to a normal post with just slightly different wording. But the reason I made one is because I want to add a second title after the initial title.

To make this as easy as possible for the user I would ideally like this text field just underneath the first one above the main editor. Just wondering how do I add that here?

register_post_type('events',
    array(  
        'label' => 'Events',
        'description' => 'Events section',
        'public' => true,'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => ''),
        'query_var' => true,
        'exclude_from_search' => false,
        'supports' => array('title','editor','thumbnail','author','page-attributes',),
        'taxonomies' => array('category',),
        'labels' => array (
            'name' => 'Events',
            'singular_name' => 'Event',
            'menu_name' => 'Events',
            'add_new' => 'Add Event',
            'add_new_item' => 'Add New Event',
            'edit' => 'Edit',
            'edit_item' => 'Edit Event',
            'new_item' => 'New Event',
            'view' => 'View Event',
            'view_item' => 'View Event',
            'search_items' => 'Search Events',
            'not_found' => 'No Events Found',
            'not_found_in_trash' => 'No Events Found in Trash',
            'parent' => 'Parent Event',
        ),
    ) 
);

'title' seems to be the standard one, so I'm wondering if I can create another instance of that?

like image 773
Adrian Avatar asked Dec 20 '25 05:12

Adrian


2 Answers

Update: there's an easy way to do it using some hooks and managing the save_post just like a regular meta box.


No, there can only be one title.

You have to create a Custom Field to record the value of a Second Title.

And, problem is that it cannot be put between the title and the content box.

I suggest the plugin Advanced Custom Fields. It's been actively developed and is quite handy for generating a variety of CF's.


This is how I'd do it, click on the images to enlarge:

Configure one Advanced Custom Field

acf configuration


Result in Custom Post Type screen

enter image description here


Moving the field with jQuery help

add_action( 'admin_head', 'so_13726274_move_field' );

function so_13726274_move_field()
{
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($)
        {     
            $('#acf-text').prependTo('#postdivrich');
            $('#acf-field_50baa73272855').css('width','100%');
        });
    </script>
    <?php
}

Results in

enter image description here

jQuery Notes

  • $('#acf-text') is the container div, corresponds to "#acf-FIELD_NAME"
  • $('#acf-field_50baa73272855') is the text field itself, we need this command because the width gets shorter when we move the container div
  • the text field #ID has to be detected in your own installation as it won't be the same, use Chrome Inspector or FireBug
like image 149
brasofilo Avatar answered Dec 22 '25 19:12

brasofilo


You could do it by changing your editor layout for your custom post type. This site will show you how you can do that. You can make a box inside your editor for a Subhead line.

http://wp.smashingmagazine.com/2011/10/14/advanced-layout-templates-in-wordpress-content-editor/

like image 33
JCBiggar Avatar answered Dec 22 '25 20:12

JCBiggar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!