WordPress.org

Ready to get started?Download WordPress

Forums

Custom fields used like variables (6 posts)

  1. antistandard
    Member
    Posted 4 years ago #

    Hi WordPress geniuses,

    I am trying to understand custom fields and have read all I can on them in the wordpress forum and google searching, but I am still unsure how I can use them for the task below:

    Simply, I have the need to display:

    Name: Mr. Dudeman
    Company: Modest company
    Website: http://www.modestcompany.com
    Phone: 555-111-2222

    I want to use custom fields in posts, so if I want to add a new customer, I can just add their details (name, company ..etc) into the custom fields and then in another page, I can list all the posts(customers details) in a loop that will display the posts one at a time like so:

    Name: Mr. Dudeman
    Company: Modest company
    Website: http://www.modestcompany.com
    Phone: 555-111-2222

    Name: Mrs. Awesomeperson
    Company: Small company
    Website: http://www.smallcompany.com
    Phone: 555-211-2222

    Name: Dr. Stein
    Company: Mysterious company
    Website: http://www.mysteriouscompany.com
    Phone: 555-311-2222

    etc....

    Is custom fields the way to go?

    If so, then how can I make a post that is a template so I don't have to keep adding the custom fields over and over again for each new post.

    Thank you

    James

  2. keesiemeijer
    moderator
    Posted 4 years ago #

    I would use custom fields for this task. If a custom field key is used in one post you can select it under "Custom Fields" when you edit or write a new post. So you don't have to add the key the next time you write a post.

  3. antistandard
    Member
    Posted 4 years ago #

    Thanks Keesiemeijer.

    Also, where is the simplest instructions for using custom fields in wordpress without a plugin please. Some instructions that simply outline the most basic of ways to just add custom field calls into a wordpress page.

    Thank you

    James

  4. keesiemeijer
    moderator
    Posted 4 years ago #

    You can use a loop like this:

    <?php
    // get all posts with a custom field key "name"
    query_posts('meta_key=name'); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php
    $name = get_post_meta($post->ID, 'name', true);
    echo 'name: '.$name;
    $company = get_post_meta($post->ID, 'company', true);
    if($company != ''){ echo 'company: '.$company; }
    $website = get_post_meta($post->ID, 'website', true);
    if($website != ''){ echo 'website: '.$website; }
    $phone = get_post_meta($post->ID, 'phone', true);
    if($phone != ''){ echo 'phone: '.$phone; }
    ?>
    <?php endwhile; ?>
    <?php endif; ?>
  5. antistandard
    Member
    Posted 4 years ago #

    Thanks mate, I'll look at this later this weekend and let you know how I go.

    Thanks again for your time,

    James

  6. antistandard
    Member
    Posted 4 years ago #

    Hi Keesiemeijer,

    Yes, that worked, thank you very much.

    I didn't need the lines:
    // get all posts with a custom field key "name"
    query_posts('meta_key=name'); ?>

    but everything else was perfect.

    James

Topic Closed

This topic has been closed to new replies.

About this Topic