0

I created several template files like this:

<?php
/*
Template Name: My Template 1
*/

get_header('my-template-1');
while ( have_posts() ) : the_post();
  the_content();
endwhile;
get_footer('my-template-1');

?>

I can assign a template to each page of my site.

Question:

How can I make specific portions of these templates to be editable in admin?

For example, I wish to have a field (which is assigned to one template) with some specific text in it, and if I edit and save this field - the updated text should appear in all pages that use this specific template.

1

Custom fields can accomplish what you're trying to do. http://codex.wordpress.org/Custom_Fields

You define the custom key and values in an edit page, and then you can re-use them in other edit pages. Once defined, you need to display the custom fields in your template(s). The documentation isn't super clear, but if your key was "currently_reading", your php in your template would look something like,

get_post_meta($post_id, 'currently_reading');

...which would spit out "Calvin and Hobbes" for that particular page.

There are also plugins that can dress up the wp-admin area and the syntax, rather than just a textarea or text field and the custom field key slug. I use Advanced Custom Fields in almost every site I make: http://www.advancedcustomfields.com/ . There's a free and pro version.

  • You proposed to use custom fields for posts or pages, but I need custom fields for templates. After editing a custom field on a page, the update will appear only on that page. But, I need to edit a custom field on a template, where the update should appear on all pages that use this template. – acoder Jan 30 '15 at 18:55
  • I think I understand now. You want a consistent value among pages. The shortest way is probably to reference a specific page/post_id in get_post_meta(), where that post is where you enter the value that the different pages share. You can also set consistent values via theme option codex.wordpress.org/Creating_Options_Pages , or a widge codex.wordpress.org/… . – Taruc Jan 30 '15 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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