Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Wordpress structure - thoughts on inputting all site content in widgets

I have a client site I am re-structuring at the moment and would like to cut down on the amount of repeated code in the website.

The site is built in sections, some of which are unique but many of which are repeated across multiple different pages. Ideally changing one of these sections would update them all (DRY) but that is not the current configuration.

I plan on updating all repeated content into the widgets and basically managing the site content from the child theme's functions.php file by adding widget areas to all pages where I want that widget repeated.

Is there a better way to do this short of actually modifying my child theme in some way? (I don't have much experience with wordpress).

Notes: Using genesis

Sarah Giles
Sarah Giles
2,815 Points

I'm not fully understanding you. Is this what you mean? you have several pages with content such as "contact us info" it contains text of a phone number and address and email. Currently the sprint is repeated on every page statically so that when the email address changes it had to be updated on every page. You are wanting to put this info into a widget and drop the widget onto the page template so it is included on every page.

Is this what you mean?

If so you may have to modify the theme.

  1. If you are wanting it to be on a few pages and these pages will have a distinctive visual different, for instance the widget will be in an aside that makes the page appear to have 2 col, you will need to create a custom page template and set each of those page templates to the pages.

  2. If the theme's page template does not have a place for a widget, You will need to add a widget are to the page template

if you are creating a brand new custom widget you will have to modify the theme.

You should be able to go one of two routes. Add a widget manually to a theme page.

<?php dynamic_sidebar( 'home_right_1' ); ?>

or add it as a condition that lets you control the widget in the admin panel.

<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?> <div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary"> <?php dynamic_sidebar( 'home_right_1' ); ?> </div><!-- #primary-sidebar --> <?php endif; ?>

for more info vist: https://codex.wordpress.org/Widgetizing_Themes

Hey Sarah,

Thanks for your answer. The site is for an author, so I have lots of full width sections for different series or book countdowns etc which are repeated on different pages but yes essentially the same concept as the contact example you gave.

I'm less worried about the specific code implementation as the advantages or disadvantages associated with my solution (using one widget per repeated section and assigning it to multiple locations) or other, better ways to do it.

2 Answers

Just for anyone else trying to do something similar;

Info Using Wordpress with genesis framework and child theme.

Problem Site had multiple full width content sections which were repeated across multiple pages. Making any changes to one section had to be done across multiple pages with each repeated instance of that repeated section. Took ages and was terrible coding.

Solution Instead of applying content to pages using text widgets filled with code, which means each repeated section needs a new text widget, I created one custom widget for each repeat section which can then be applied to as many pages or places as necessary. Editing the widget edits all instances. I also downloaded a plugin called "adminimise" which allows you to edit which widgets are visible in the appearance -> widgets admin page to have a cleaner process of applying the the custom widgets to your various layouts.

Sarah Giles
Sarah Giles
2,815 Points

Repetitive code is bad. I think a widget would be the way to go.