Custom Field Values

coffee2code.com » Get Custom Field Values

tags: wordpress, plugin, customfield

 

Tips & Examples

  • With this simple invocation, you can echo the value of any metadata field (where, of course, ‘mymood’ is the custom field key of interest):

    <?php echo c2c_get_custom('mymood'); ?>

  • Want the behavior of a moods plugins?
    <?php echo c2c_get_custom('mood', '(Current mood : ', ')'); ?>

    Example results:
    [if ‘mood’ is set to ‘happy!’] (Current mood: happy!)
    (add filters if you wish to use smilies, etc)

  • <?php echo c2c_get_recent_custom('mymood', 'Most recent mood: '); ?>
  • Want the behavior of a currently reading/currently listening to plugin?

    <?php echo c2c_get_custom('reading', 'Currently reading :'); ?>

    [if value for ‘reading’ is ‘Cujo’] Currently reading: Cujo

    <?php echo c2c_get_custom('reading', 'Currently reading: <i>', '</i>', '', '</i>, <i>'); ?>

    [if three ‘reading’ fields were defined, with values ‘Carrie’, ‘Cujo’, and ‘The Shining’]
    Currently reading: Carrie, Cujo, The Shining

    <?php echo c2c_get_custom('listening', 'Now playing ', ' on my stereo.', 'nothing'); ?>

    [if value for ‘listening’ is ‘The Beatles’] Now playing The Beatles on my stereo.
    [if no value defined for ‘listening’] Now playing nothing on my stereo.

  • Want the behavior of a Post Icon plugin (image to display for the post, not derived from category)?

    <?php echo c2c_get_custom('post_icon', '<img alt="post icon" class="posticon" src="/wp-images/posticons/', '.gif" />', 'blank'); ?>

    Which would yield:

    [if value for ‘post_icon’ is ‘wordpress’]
    post icon

  • Want to do per-post customized ‘more’ text? (to override the default “Read the rest of this entry >>”) (This example will show that default, which you can of course change, if you don’t define the custom field ‘more’ for a post)

    In index.php, look for this function: <?php the_content(); ?>

    Replace it with this:

    <?php the_content(c2c_get_custom('more', '<span class="more">', '</span>', 'Read the rest of this entry »')); ?>

  • <?php echo c2c_get_custom('todays_link', '<a class="tlink" href="', '" >Today's Link</a>'); ?>
  • <?php echo c2c_get_custom('related_offsite_links', 
    'Here's a list of offsite links related to this post:<ol><li><a href="',
    '">Related</a></li></ol>',
    '',
    '">Related</a></li><li><a href="'); ?>
  • <?php echo c2c_get_custom('more_pictures',
    'Pictures I've taken today:<br /><div class="more_pictures"><img alt="[photo]" src="',
    '" /></div>',
    '',
    '" /> : <img alt="[photo]" src="'); ?>

Template Tags

Functions

  • <?php function c2c_get_custom( $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>
    Template tag for use inside “the loop” and applies to the currently listed post.
  • <?php function c2c_get_recent_custom( $field, $before='', $after='', $none='', $between=', ', $before_last='', $limit=1, $unique=false, $order='DESC', $include_pages=true, $show_pass_post=false ) ?>
    Template tag for use outside “the loop” and applies for custom fields regardless of post.

Arguments

  • $field
    Required argument. The custom field key of interest.
  • $before
    Optional argument. The text to display before all field value(s).
  • $after
    Optional argument. The text to display after all field value(s).
  • $none
    Optional argument. The text to display in place of the field value should no field value exists; if defined as ” and no field value exists, then nothing (including no $before and $after) gets displayed.
  • $between
    Optional argument. The text to display between multiple occurrences of the custom field; if defined as ”, then only the first instance will be used.
  • $before_last
    Optional argument. The text to display between the next-to-last and last items listed when multiple occurrences of the custom field; $between MUST be set to something other than ” for this to take effect.

Arguments that only apply to c2c_get_recent_custom():

  • $limit
    Optional argument. The limit to the number of custom fields to retrieve.
  • $unique
    Optional argument. Boolean (’true’ or ‘false’) to indicate if each custom field value in the results should be unique.
  • $order
    Optional argument. Indicates if the results should be sorted in chronological order (’ASC’) (the earliest custom field value listed first), or reverse chronological order (’DESC’) (the most recent custom field value listed first).
  • $include_pages
    Optional argument. Boolean (’true’ or ‘false’) to indicate if pages should be included when retrieving recent custom values; default is ‘true’.
  • $show_pass_post
    Optional argument. Boolean (’true’ or ‘false’) to indicate if password protected posts should be included when retrieving recent custom values; default is ‘false’.

Related Links

  • WordPress Support Forums announcement of Get Custom Field Values

Frequently Asked Questions

  • Q: I added the template tag to my template and the post has the custom field I’m asking for but I don’t see anything about it on the page; what gives?

    A: Did you echo the return value of the function, e.g. <?php echo c2c_get_custom('mood', 'My mood: '); ?>

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中


%d人のブロガーが「いいね」をつけました。