0

I am new to WordPress. In short, I wish to output custom posts onto a page, but several different attempts have been unsuccessful.

I have successfully created a custom post type which saves custom data through custom fields.

<?php

    /*------------------------------------------------------------------------
        ->  CREATE SECTION FOR LOCATION MANAGEMENT IN WORDPRESS ADMIN AREA
    ------------------------------------------------------------------------*/

    add_action('init', 'location_manager_register');
    function location_manager_register() {
        $args = array(
            'label' => __('Locations'),
            'singular_label' => __('location'),
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => true,
            'has_archive' => true,
            'supports' => array('title', 'thumbnail'),
            'rewrite' => array('slug' => 'locations', 'with_front'=> false)
        );
        register_post_type('locations', $args);

    }

    /*------------------------------------------------------------------------
        ->  ADD BOX WHICH WILL CONTAIN CUSTOM TEXT FIELDS TO NEW SECTION
    ------------------------------------------------------------------------*/

    // CREATE THE BOX
    add_action("admin_init", "contact_manager_add_meta");
    function contact_manager_add_meta() {
        add_meta_box(
            "location-meta",
            "Location Options",
            "location_manager_meta_options",
            "locations",    // THIS PLACES THE BOX IN THE RIGHT SECTION
            "normal",       // PLACEMENT WITHIN SECTION
            "high"          // PRIORITY OF BOX
        );
    }

    // CREATE CUSTOM VALUES & ADD TEXT BOXES WITH CUSTOM VALUES
    function location_manager_meta_options() {
        global $post;
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
        $custom = get_post_custom($post->ID);
        $address = $custom["address"][0];
        $telephone = $custom["telephone"][0];
        $fax = $custom["fax"][0];
        $email = $custom["email"][0];
?>
        <style type="text/css">
            <?php include('css/contact_manager.css'); ?>
        </style>

        <div class="contact_manager_extras">
            <div>
                <label> Address: </label>
                <input type="text" name="address" value="<?php echo $address; ?>"/>
            </div>
            <div>
                <label> Telephone: </label>
                <input type="text" name="telephone" value="<?php echo $telephone; ?>"/>
            </div>
            <div>
                <label> Fax: </label>
                <input type="text" name="fax" value="<?php echo $fax; ?>"/>
            </div>
            <div>
                <label> Email: </label>
                <input type="text" name="email" value="<?php echo $email; ?>"/>
            </div>
        </div>
<?php
    } // END function location_manager_meta_options

    // ADD FUNCTIONALITY TO SAVE THE FIELD VALUES
    add_action("save_post", "contact_manager_save_extras");
    function contact_manager_save_extras() {
        global $post;
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
        else {
            update_post_meta($post->ID, "store_name", $_POST["store_name"]);
            update_post_meta($post->ID, "address", $_POST["address"]);
            update_post_meta($post->ID, "telephone", $_POST["telephone"]);
            update_post_meta($post->ID, "fax", $_POST["fax"]);
            update_post_meta($post->ID, "email", $_POST["email"]);
        }
    }

?>

This shows the interface in the Wordpress admin area: http://eoghanoloughlin.com/link_images/custom_fields.jpg

Finally, this is where the problem is, I want to output the information onto a page. Here is the code I am currently using:

<?php /* Template Name: Locations */ ?>

<?php get_header(); ?>

<?php query_posts('post_type=locations'); ?>

<section class="middle">
    <div class="container location">

        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
            <!-- MAIN AREA -->
        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

        <div class="main">
            <img src="<?php echo IMAGES ?>/content/dublin_map.jpg" class="location_map">


            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <?php 
                global $post;
                $address = get_post_custom_values('address')[0];
                $telephone = get_post_custom_values('telephone')[0];
                $fax = get_post_custom_values('fax')[0];
                $email = get_post_custom_values('email')[0];
            ?>

            <article>

                <h6> <?php the_title(); ?> </h6>

                <address> <p> <span>Address:</span> <?php echo $address; ?> </p> </address>

                <p> <span>Tel:</span> <a href="tel:+045-444365" >045-444365</a> </p>

                <p> <span>Fax:</span> <?php echo $fax; ?> </p>

                <p> <span>Email:</span> <a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a> </p>

            </article>

            <?php
                endwhile;
                endif;
            ?>

        </div>

        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
            <!-- SIDE BAR -->
        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

        <aside>
            <?php get_sidebar('main'); ?>
        </aside>

    </div>
</section>

<?php get_footer(); ?>

The key parts in this last piece of code being the "query_posts()" function and then the loop.

As I said, I am new to wordpress, I cannot figure it.

1 Answer 1

1

Does this help?

<?php

$args = array(
    'post_type' => 'locations',
    'some_thing_here' => '',
    'some_thing_here' => '',
    'some_thing_here' => '',
    'some_thing_here' => ''
);

$mylocations = new WP_Query( $args );

if ( $mylocations -> have_posts() ) : while ( $mylocations->have_posts() ) : $mylocations -> the_post();

?>

<?php the_title(); ?>    
<?php the_content(); ?>    

<?php endwhile; else: ?>

    Oops, there are no Locations.

<?php endif; ?>

http://codex.wordpress.org/Class_Reference/WP_Query

EDIT

And there is an Smashing Magazine Article on the subject with some additional examples.

Remember my php is pretty basic, but I think you can put the meta date in a $ by doing the following.

<?php 
    $adress = get_post_meta($post->ID, 'adres', true);
    $telephone = get_post_meta($post->ID, 'telephone', true);
    $fax = get_post_meta($post->ID, 'fax', true);
?>

And than echo it out:

<?php echo $adress; ?>

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

might want to check out advancedcustomfields.com realy powerfull with meta boxes and such.
Thank You, but how would I specifically target the custom fields, like address & email etc? Like underneath the_content() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.