我是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(); ?>
最后一段代码中的关键部分是&#34; query_posts()&#34;功能然后循环。
正如我所说,我是wordpress的新手,我无法理解。
答案 0 :(得分: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
修改强>
主题上有一个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; ?>
希望这有帮助。