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?
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)