Showing posts tagged php

the_post_image() = the_post_thumbnail()

This is to let you fellow lost souls out there know that the new the_post_image() functionality that was announced for WordPress 2.9 was subsequently renamed the_post_thumbnail(). This was not immediately apparent—I guess since it was never in an official release that way, it doesn’t have to be shown as a deprecated function.

Check back soon for a post on using the darn thing!

Add a User Interface to WordPress Custom Fields

Custom fields are great for adding additional functionality into WordPress sites. I have used them in the past to set up post thumbnails, for music album metadata, such as artists and album year, to set articles to link to their original sources, and many others.

The problem is, the default interface for adding custom fields is very clunky. The user has to either manually type in the field name, or select it from a list.

This doesn’t work too well for an average client. The reason I use WordPress on my sites in the first place is for ease of use. Fortunately, there’s an easy way to use custom input boxes! Simply paste the code below into your theme’s “functions.php” file, adjust the variables, and you’re good to go!

/* add actions to admin menu */

add_action('admin_menu', 'fieldname_add_custom_box');



/* use save_post action to handle data entered */

add_action('save_post', 'fieldname_save_postdata');



/* Adds a custom section to the "side" of the post edit screen */

function fieldname_add_custom_box() {

    if(function_exists('add_meta_box')) {

	add_meta_box('fieldname', 'fieldname', 'fieldname_custom_box', 'page', 'normal', 'high');

    }

}



/* prints the custom field in the new custom post section */

function fieldname_custom_box() {

    //get post meta value

    global $post;

    $fieldname = get_post_meta($post->ID,'fieldname',true);



    // use nonce for verification

    echo '';



    // The actual fields for data entry

    echo '
Label
'; echo 'Label:'; echo ''; echo '

Description of what this field does.

'; } /* when the post is saved, save the custom data */ function fieldname_save_postdata($post_id) { // verify this with nonce because save_post can be triggered at other times if (!wp_verify_nonce($_POST['fieldname_noncename'], 'fieldname')) return $post_id; // do not save if this is an auto save routine if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; $fieldname = $_POST['fieldname']; update_post_meta($post_id, 'fieldname', $fieldname); }

Go in there and replace fieldname with the name of your custom field, and fill in Label and the description and save your file. You can also add multiple fields into one option group. If you want this box to show up on posts instead of pages, change “page” to “post” in the arguments for add_meta_box().

This is what the new field looks like when setup on a site. This box lives just below the main entry area. You can adjust the options in add_meta_box() to change the placement on the page. I hope this helps you improve the usability on your WordPress site!

Show Sub-Pages on Parent in WordPress

This is a bit of code that will can be used as a page template in WordPress. It first displays the parent page, then enters a loop to display all of the child pages. This is really useful for organization in large WordPress-as-a-CMS sites.

Just make a new PHP file in your theme directory and paste in this code:

<?php
/*
Template Name: Section Listing Page
*/
?>

<?php get_header(); ?>

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="post" id="<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2><?php edit_post_link('Edit this Page','<p class="meta">','</p>'); ?>
<div class="entry">
<?php the_content(); ?>
</div>
</div> <!-- end "post" -->
<?php $pageid = get_the_ID(); ?>
<?php endwhile; endif; ?>

<?php $subpages = new WP_Query("post_parent=$pageid&post_type='page'&orderby=title&order=ASC");
if($subpages->have_posts()) : while($subpages->have_posts()) : $subpages->the_post(); ?>
<div class="post">
<?php echo '<h3><a name="' . the_slug() . '" href="' . the_permalink() . '">' . get_the_title() . '</a></h3>'; ?>
<?php edit_post_link('Edit this Page','<p class="meta">','</p>'); ?>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; endif; ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Then all you need to do is select your file under “Page Template” on the edit screen.

Get a WordPress Page ID with the Slug

When I develop for WordPress, I start out on my local machine, then eventually migrate to the production server. This works very well, but takes a bit more work when I get to the migration stage. To make it a little easier, I use this little function to eliminate any hard-coded page IDs in navigation links or conditional statements:

function get_ID_by_slug($page_slug) {
    $page = get_page_by_path($page_slug);
    if ($page) {
        return $page->ID;
    } else {
        return null;
    }
}

Then, whenever we need to call a function that requires a page ID, just pass it our new function.

wp_some_function(get_ID_by_slug('any-page'));

Display Full Images in RSS Feed with NextGen

I’ve worked with a number of people who use the NextGen Image Gallery plugin for WordPress. A common problem that folks have with NextGen is that it doesn’t allow for full-size images to be displayed in an RSS feed. The user instead has to click through to the site to see the full images. If you don’t like that, you can use this code to make the full images show up!

Eventually, I’ll be releasing this as a free WP plugin, but for now, by popular demand, I’m posting it here. Simply paste this code into your theme’s functions.php file (before the ?> at the end) and you should be good to go!

//Change Thumbnails to Full Images in RSS feed
function change_thumbs($content) {
// See if we're dealing with a feed
if (is_feed()) {
// Strip out the parts of img src that lead to thumbnails
$content = str_replace("thumbs/thumbs_", "", $content);
$content = str_replace("width=\"208\"", "", $content);
$content = str_replace("height=\"156\"", "", $content);
}
// Send the content on its way
return $content;
}
// Add filters for RSS
add_filter('the_excerpt_rss', 'change_thumbs', 20);
add_filter('the_content', 'change_thumbs', 20);

If you don’t feel comfortable mucking around in code, feel free to contact me for a quick quote to set this up on your site.