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,我無法想象它。
可能希望檢查出http://www.advancedcustomfields.com真正強大的元框等。 – Kortschot
和http://generatewp.com非常好,可以快速獲得您的自定義帖子類型 – Kortschot
謝謝,但我將如何專門定位自定義字段,如地址和電子郵件等?像在the_content()下面一樣? – loxyboi