Managing WordPress Template Tags with Dreamweaver Snippets

For designers who don’t write PHP from scratch, the WordPress template code mentioned in past articles can be cumbersome to type out for each new project. For me, that code is a perfect candidate for Dreamweaver’s ‘Snippets’ tool which allows the code to be saved, managed and re-used with relative ease.

Create a Repository

In Dreamweaver, open the Snippets panel by hitting Shift+F9. Right-click somewhere in the whitespace, select “New Folder” and name it something simple like “WordPress”:

Write a Snippet

My most frequently used snippet is a simple query_posts function which pulls the last few blog headlines from the database and puts them in an unordered list. We’ll use it as our example:

<?php query_posts('showposts=3&cat=6'); ?>
<ul>
 <?php while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
 <?php endwhile; ?>
 </ul>

Save It

Copy the code above, and with “Code View” open, paste it into Dreamweaver. Then select it, right-click it, name it and save it to your repository:

Implement It

Putting your snippet into action is as easy as clicking once in the “Code View” window, placing your cursor on the relevant line, then going over to the Snippets panel and double-clicking:

My Snippets

It’s as easy as that. As you can see from the image above, I’ve got a handful of my own snippets already saved.

  1. WP Custom Field Value
    <img src="/images/<?php $values = get_post_custom_values("key_name"); echo $values[0]; ?>" alt="" />
  2. WP List Tertiary Page Links
    <ul>
    <?php if(wp_list_pages("title_li=&child_of=2")) { ?>
    <?php } ?>
    </ul>
    
  3. WP PHP Include
    <?php include (TEMPLATEPATH . "/nav.php"); ?>
    
  4. WP Show Latest Headlines
    <?php query_posts('showposts=3&cat=6'); ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    
  5. WP Show Recent Post Excerpt
    <?php query_posts('showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_time('m.d.y') ?><?php the_excerpt(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark">+More</a>
    | <a href="/blog">+All Posts</a>
    <?php endwhile; ?>
    

    (See on the homepage of DarrenHoyt.com)

  6. WP Template Header
    <?php
    /*
    Template Name: Snarfer
    */
    ?>
    

Feel free to re-use this code, and let me know what snippets you use most often.

8 Responses {+}

Trackbacks

  1. Wordpress Template Tags and Dreamweaver Snippets | Butterfly Media Romania Blog