Wednesday, July 1, 2015

Multiple Content Areas

Out of of the box, WordPress only supports a single content area per page / post; with the custom fields feature providing limited additional flexibility.  Luckily, a very powerful WordPress plugin called Advanced Custom Fields (ACF) can be used support multiple content areas per page / post.

To get started, install the Advanced Custom Fields plugin.

Using admin > Custom Fields create a new field group called home with two WYSIWYG fields named top and bottom and set it to show when Page is equal to home.  To limit the end-user to just editing these fields, hide all the other elements from the edit screen.

With this in place, we can update the front-page.php as follows:

front-page.php
<?php get_header(); ?>
<?php wp_nav_menu(array('theme_location' => 'header-menu')); ?>
<?php get_search_form(); ?>
<?php while (have_posts()) : the_post(); ?>
  <?php the_field('top'); ?>
  <?php the_field('bottom'); ?>
<?php endwhile; ?>
<?php get_footer(); ?>

While this example only used WYSIWYG fields, we can limit the end-user to content like plain text and images to be used in highly styled elements.

No comments:

Post a Comment