Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two custom post types (Authors and Partners). I display their archive page's in the main navigation and use archive-authors.php and archive-partners.php to make a couple small tweaks to the display of posts in each.

Now, my client would like to display some text before the archive's post listing. So far I can think of the following ways to do that:

  1. Save the text as the post type's description and display that.
  2. Create a separate page and hard code a custom WP_Query() loop for just that page (by ID) above the archive.
  3. Write a custom loop with WP_Query() to produce the CPT archive for each CPT and setup "Author Archive" and "Partner Archive" templates that can be used on static, editable pages.

However, all of these solutions seem suboptimal for one or more of the following reasons:

  • They require technical knowledge to update (#1, #2)
  • It's not abstracted (e.g. the solution has to be custom-coded for each archive) (#2, #3)
  • Updating the text requires technical knowledge (#1)
  • The solution essentially duplicates the template hierarchy (#3).

I'm looking for a solution that's WordPress friendly, abstracted, and easy-to-update for the client.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

One of the easiest (although not the only) of ways to achieve this is by creating a custom options panel in the WP dashboard that will allow your client to create and update information that can be used through out your template files with no technical knowledge being necessary.

You can either paste the following directly into your functions.php file or you can save it in a file i.e. config-menu.php (within your theme directory) and then include it into your functions.php file - the choice is yours however the code is;

// create config menu in dashboard
add_action('admin_menu', 'config_menu');

function config_menu() {

    //create a menu in the dashboard
    add_menu_page('Website Custom Settings',
                  'Configure Site', 
                  'administrator',
                  __FILE__,
                  'custom_settings_page',
                  ''.get_bloginfo('template_directory').'/images/your_icon.png', 4);

}

//register settings
add_action( 'admin_init', 'register_settings' );

function register_settings() {

    register_setting(   'my-settings-group', 'partners');
    register_setting(   'my-settings-group', 'authors');
}

function my_settings_page() {
?>

<div class="wrap">

    <form method="post" action="options.php">

    <?php settings_fields('my-settings-group'); ?>

    Enter your Partner description here <br/>
    <textarea  name="partners"><?php echo get_option('partners');?></textarea>

    <br />

    Enter your Author description here <br/>
    <textarea  name="authors"><?php echo get_option('authors');?></textarea>
    <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />

    </form>

</div>

<?php } ?>

Then in your archive template files for each post type you can do the following;

<?php echo wpautop(get_option('partners');?>

and

<?php echo wpautop(get_option('authorss');?>

This will call the respective values (text entered) into the textarea fields you have created in the dashboard area for the client.

NOTE: The example code above is very rudementary and its stripped down to provide you a basic example. No CSS styling provided which I'll leave up to you. But this will get the job done.

share|improve this answer
    
I'm still thinking this over, but I like the direction it's headed. I'm still not thrilled that it's not completely abstracted (as defined in the OP), but this seems like a good solution that I'll probably try out. –  mrwweb Apr 24 '12 at 16:33
    
I assume that I can replace <textarea> with wp_editor() and use the get_option(...) as the first argument? –  mrwweb Apr 24 '12 at 16:34
    
Yes see HERE on how to pass values to setup wp_editor(). That aside what do you mean by completely abstracted? At someone you are going to have to insert some placeholder into your template files that says "hey, call this bit of text from such and such place" there's not much way around that but you only have to do that once. After that you've given your client the ability to control the information displayed in those areas at will without ever having to touch a piece of code. –  userabuser Apr 24 '12 at 18:03
    
Thanks again for your help on this @userabuser. I'm working on implementation and stuck on the wp_editor portion of things. wordpress.stackexchange.com/questions/51248/… –  mrwweb May 5 '12 at 18:33

There is now a plugin CPT Descriptions that does this.

As of v0.1, the plugin is pretty buggy (editors can't save descriptions), but most of the stuff is taken care of on the GitHub repository version of the plugin and you can find some other changes/improvements amongst the one existing fork and pull requests. Hopefully those will be integrated into the WordPress repo version of the plugin, but for now they're up and tested on GitHub.

To use the plugin, simply install it, fill out the "Description" fields for each post type you desire and then output the description on an archive page with:

the_post_type_description();

or get a specific post type's description with:

the_post_type_description('my_post_type');

or save it to a variable with:

$description = get_post_type_description();
share|improve this answer
    
I just tested the latest version from GitHub (version number 0.2) under WP 3.8 and it appears to work perfectly. –  Dakota Jan 7 at 19:43
    
@Dakota The big problem is that the descriptions are only editable by Administrators. –  mrwweb Jan 7 at 23:43
    
@mewweb Yes I see, I just tested and can easily explain it: to keep things simple, the plugin uses the WordPress Options system, which is only modifiable by Administrator role users. The plugin could be rewritten to use a Custom Post Type to store the data, which would give easy role permissions and also allow other metadata about custom post types to be easily stored. I'd do it myself, but I'm just another user of the plugin and currently don't have any need for those advanced features. –  Dakota Jan 27 at 22:25
    
@Dakota the problem is actually that the options aren't quite implemented correctly. There is a filter to allow non-admin usage of options. I've implemented in in this branch but that hasn't been integrated into plugin and I'm not sure when/if it will. –  mrwweb Jan 27 at 22:45
    
Doesn't that filter allow all options? Probably not what you want, security-wise. –  Dakota Jan 29 at 3:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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