2

I added HTML banners to the top of certain pages on a client's site. He wants to make edits to the text phrases on these banners without having to edit any HTML. How do I open this up for him?


In the past, I have simply made a collection of pages that I insert into my HTML banners using the following code:

<div class="banner-top">
<h1><?php echo $about_us_phrase->post_title; ?></h1>
<p><?php echo apply_filters( 'the_content', $about_us_phrase->post_content ); ?></p>
</div>

But this feels hackish, especially because it forces the client to swim through a potentially large number of pages to find the one that corresponds to the text they'd like to edit.

What is the most Wordpress-y way to do this?

2

Are the banners related to each page? If so, I would use a custom field or just a metabox that the user could edit or enter information into on the page that you want the banner to show up. The field would show up below the content in the page editor. You would then in your single.php add the code you noted above to encase whatever is typed in the custom field.

If the banners are NOT related to individual pages. I would make a custom post type "banners" and then have them randomly show up on pages by inserting a call for the CPT within the single.php.

You can either use a plugin to create the fields or these reference links will help you get started on creating your own:

https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes (explanation etc)

https://developer.wordpress.org/reference/functions/add_meta_box/ (code examples and usage)

  • The banners are related to each page. Do you mean this metabox? metabox.io – Lincoln Bergeson Feb 28 '17 at 20:08
  • That will definitely work. the company is called metabox, but they are making custom fields. Another option would be Advanced custom fields. I don't think I am supposed to recommend one, but I use ACF more. An actual metabox you can create without a plugin as well. These two plugin options do the work of creating them for you. Pretty slick. In either case you'll have to still edit your single.php files to add the new code. – rudtek Feb 28 '17 at 20:12
  • Oh great, thanks. How do I create a metabox without using a plugin? – Lincoln Bergeson Feb 28 '17 at 20:15
  • I added to links to my answer to get you started. Feel free to mark answered for me if this is helpful! – rudtek Feb 28 '17 at 20:21
  • I'll try it out when I get home and let you know :) – Lincoln Bergeson Feb 28 '17 at 20:22
0

Make a custom sidebar for the banner, then add from the theme any code you don't want them to edit or touch, finally have them use a simple Text Widget to add/change/remove text.

That way, if there is no text widget (eg banner not in use), the banner won't display at all.

For more complex content, I'd also say use a custom post type and custom fields.

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.