homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / WordPress
Forum Library, Charter, Moderators: lorax & rogerd

WordPress Forum

Wordpress How to output simple text
wordpress text output

 4:16 pm on Mar 29, 2012 (gmt 0)

Im new to Wordpress and making a theme. I just want a simple way to output some text on one page that I can change from time to time. It's not really a post but just want 3 separate content areas I will have the ability to add a paragraph of text or two into.
What is the best code to put into my template to let this happen so I can use the dashboard to add the text into these areas?



 4:27 pm on Mar 29, 2012 (gmt 0)

Why not just create a table or separate DIVs on a Post or Page and write/edit your copy inside of these?


 4:23 pm on Mar 30, 2012 (gmt 0)

It's not really a post but just want 3 separate content areas I will have the ability to add a paragraph of text or two into.

The driving point behind Wordpress is that everything you enter via admin is HTML-less, at least, structurally. That being said, if you know how to customize the page.php template (or one of it's descendants in the new themes,) you can do this with custom fields.

First, create a test page (not a post.) Put some test content in the test block. Under Custom Fields*, create two new custom fields. Enter a name for the new custom fields and enter a test value for them:

output-block-1 Value: this is output block one

output-block-2 Value: this is output block two

*If you don't see custom fields under the main text block area, use the screen options box at the upper right to make it visible.

Save this, then in your editor (offline,) create a new template file and re-save it as page-custom.php (or file name of your choosing.) At the top, give it a unique name so Wordpress can recognize it.

You extract the value of your custom fields by using the get_post_meta() function, with the the post ID as the first parameter, custom field name as the second, and a 1 or true as the third to indicate the data is to be extracted as a scalar (a flat single value variable, not an array.)

The below is probably not working code as it was typed on the fly, but is a bare bones example of what I'm trying to explain:

Template Name: My Test Template
if (have_posts()) {
// Custom field variables for your text blocks.
$custom_field_1 = get_post_meta($post->ID,'output-block-1',1);
$custom_field_2 = get_post_meta($post->ID,'output-block-2',1);
echo '<h1>';
echo '</h1>';
echo "<div id=\"cf-1\">$custom_field_1</div>";
echo "<div id=\"cf-2\">$custom_field_2</div>";

Save this file and upload to your template theme directory.

Now go back to the Wordpress admin and re-select the test page for editing (or reload if you still have it open.) Select "My Test Template" for the template, save, and go to the URL of your test page (permalink).

Between the header and footer you should see

<h1>My Test Page</h1>
<p>This is my test content from the main content block</p>
<div id="cf-1">this is output block one</div>
<div id="cf-2">this is output block two</div>

Once you see this concept in action, you can see how you can "place" custom blocks anywhere on any page using custom fields, and minimize the HTML you'd need within the Wordpress admin UI. As posted, you can modify your stylesheet to add selectors for #cf-1 and #cf-2 to lay them out as you like without having to modify the theme PHP code.

The (one of the) cool thing(s) about custom fields: once you create one, they become available to all pages. Any page that uses this template can access the custom field for that page or post, and the value itself is page-specific - that is, the value of "output-block-1" and -2 can be different on different pages.

More info on the Wordpress codex.


 10:58 pm on Mar 30, 2012 (gmt 0)

Thanks for the code, rocknbill.

The wordpress codex is a lot like the official mysql documentation site; very hard to figure out how to apply things in the real world.

But your example is Muay Awesomenetay!


 5:18 am on Apr 6, 2012 (gmt 0)

WOW, that is a great example. thanks for taking the time to explain all of that. I will try this tonight!

Thanks again


 7:27 pm on Apr 16, 2012 (gmt 0)

this idea worked great. thanks again

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / WordPress
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved