2013-06-05 25 views
0

我是新来的WordPress。总之,我希望将自定义帖子输出到页面上,但是几次尝试都不成功。WordPress的:输出自定义发布数据

我已成功创建自定义帖子类型,该类型通过自定义字段保存自定义数据。

<?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"]); 
     } 
    } 

?> 

这说明在WordPress管理区域的接口: http://eoghanoloughlin.com/link_images/custom_fields.jpg

最后,这就是问题的所在,我要输出信息到页面上。这是我目前使用的代码:

<?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(); ?> 

在这最后一段代码作为“query_posts()”函数,然后循环中的关键部分。

正如我所说,我是新来的wordpress,我无法想象它。

回答

1

这有帮助吗?

<?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

并且对具有一些附加实施例中的受试者中的Smashing Magazine Article

记住我的php是非常基本的,但我认为你可以通过执行以下操作来将元日期置于$中。

<?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); 
?> 

而且比回声出来:

<?php echo $adress; ?> 

希望这有助于。

+0

可能希望检查出http://www.advancedcustomfields.com真正强大的元框等。 – Kortschot

+0

和http://generatewp.com非常好,可以快速获得您的自定义帖子类型 – Kortschot

+0

谢谢,但我将如何专门定位自定义字段,如地址和电子邮件等?像在the_content()下面一样? – loxyboi

相关问题