WordPress.org

Ready to get started?Download WordPress

Forums

Custom Fields? (8 posts)

  1. Jon W
    Member
    Posted 3 years ago #

    I have started adding boxes (an HTML table aligned right) to the top right of some articles which lists hand picked alternative pages.

    I know that I should probably do this with divs and css, but while looking in to that came across another idea - using the custom fields.

    Thing is, I tried custom fields once before but it still displayed some code on the screen even when nothing was in the field.

    OK, maybe I need to show you what I am talking about. See here: http://www.motleyhealth.com/fitness/20-minute-home-workout

    On the right is a menu "More Workouts". So I am using code like this for that:

    <table width="33%" align="right">
    <tbody>
    <tr>
    <td align="left" valign="top">
    <h3>More Workouts</h3>
    <ul>
    	<li> .... links ..... </li>
    </ul>
    </td>
    </tr>
    </tbody>
    </table>

    Which works, but obviously not ideal.

    So, how do I set up a custom field (assuming that this is the best option) which will only show when I add content to that field - and not just show a blank square when there is no content.

    I am going through the articles slowly, there are quite a lot so it is a case of work in progress.

  2. mixmastermichael
    Member
    Posted 3 years ago #

    so, custom fields are a little confusing at first but once you get one to work you'll see they're not really so bad. Here's an example of a custom field that just has text in it for now... you could replace this with your UL when set up.

    In the wordpress admin for the page, first go up to screen options on the upper right side and make sure custom fields are clicked / visible.

    Then, down below the entry for the page, go to the 'add custom field' pane and add a new field called "title" without the "" though. then just put in some text for the value.

    Okay, now in your template that you want the custom field to appear, paste this code...

    <?php echo get_post_meta($post->ID, 'title',true) ?>

    save the page. update your admin wp page.

    You will see that it just brings in the custom field from "title" onto your page... just repeat the process now wand change the "title" field to whatever you want.

    There are probably loads of tutorials out there but I found them to be pretty confusing at first and just learned by example like this.

    Someone else, please jump in if I've missed anything...

  3. mixmastermichael
    Member
    Posted 3 years ago #

    double post.

  4. Jon W
    Member
    Posted 3 years ago #

    OK, but if I set CSS (somehow) for the custom field, and it is a set width etc. will it not appear on every post as an empty field?

    Is there a way of only including the custom field when I actually input something into it? I have about 750 posts, so to start off with most are going to be empty.

  5. mixmastermichael
    Member
    Posted 3 years ago #

    So, you can apply css to the custom field(s) to make them unique wherever you use them in your template files.

    Set your posts to the template that you want the custom fields to be in / not to be in and you should be set.

    Example:

    <div id="something_1"><?php echo get_post_meta($post->ID, 'title',true) ?><div>
    ...
    <div id ="something_2"><?php echo get_post_meta($post->ID, 'title_2',true) ?></div>

  6. Jon W
    Member
    Posted 3 years ago #

    Oh I see, have a template with the custom field, not stick it in the universal one. Gotcha!

    So, change template, add UL to custom field, update post.

  7. mixmastermichael
    Member
    Posted 3 years ago #

    Yup!

  8. Jon W
    Member
    Posted 3 years ago #

    OK, I have been messing about and just ended up totally confused.

    For the theme I am using (Magazine Basic) I have created custom templates of sorts for Pages, but struggling to do so for Posts.

    Have created a single-navbox.php and added

    <?php
    /*
    Single Post Template: Nav Box
    Description: Top right navigation box using Custom Field
    */
    ?>

    so the top, but no option is appearing in the post edit page to pick an alternative template.

    Any ideas what I could be doing wrong? In Magazine Basic all the template stuff is in loop.php, do I need to put my custom part in there somehow?

Topic Closed

This topic has been closed to new replies.

About this Topic