Community Forums › Forums › Archived Forums › Design Tips and Tricks › Add page and post specific content between title and content
- This topic has 9 replies, 2 voices, and was last updated 11 years, 7 months ago by Brad Dalton.
-
AuthorPosts
-
May 18, 2013 at 12:16 am #41473csbeckMember
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.community/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.
12345function
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'
);
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
May 18, 2013 at 1:55 am #41477Brad DaltonParticipantHi 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersadd_action( 'genesis_after_post_title', 'custom_field_before_content' ); function custom_field_before_content() { if(is_page() || is_single() ) { genesis_custom_field('chris'); } } 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!
May 18, 2013 at 8:46 am #41506csbeckMemberBrad 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.
May 18, 2013 at 8:55 am #41507csbeckMemberLooks 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).
May 18, 2013 at 10:23 am #41511csbeckMemberOk, 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:
123456add_action(
'genesis_after_post_title'
,
'custom_field_before_content'
);
function
custom_field_before_content() {
if
(is_page() || is_single() ) {
genesis_custom_field(
'subtitle_text'
);
}
}
The new code from the Codex, I changed the name of the field:
1234567// 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" />'
;
and the final line of code for what to do with it:
12// Do something with $mydata
update_post_meta(
$post_ID
,
'subtitle_text'
,
$mydata
);
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
May 18, 2013 at 12:41 pm #41521csbeckMemberI'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
May 18, 2013 at 1:30 pm #41525Brad DaltonParticipantGood 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.
May 18, 2013 at 2:06 pm #41529Brad DaltonParticipantThis 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/
May 31, 2013 at 3:26 pm #43414csbeckMemberHey 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
May 31, 2013 at 10:42 pm #43430Brad DaltonParticipantHi Chris
Install the Advanced custom fields plugin and create a custom met box.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.