在我的网站上,我使用自定义的帖子类型来实现一个稍微复杂一点的版本。
我的要求是:;
标题上的富文本可以在“特色项目”(此处未涉及)之间切换的JavaScript动画,可以方便地从后端为管理员用户编辑特色项目,可以将特色项目链接到我们网站上的任何内容,无论是文章;博客《新闻邮报》;物种概况或术语表条目我认为CPT是前进的方向;这是我在定制WP安装的管理区域开发的界面。
自定义帖子类型本身的代码非常简单。我在中创建了一个文件夹plugins
调用的目录sf-featured-items
并创建了一个名称类似的php文件。
这是完整的代码。它看起来比实际情况复杂得多!
<?php
/*
Plugin Name: Seriously Fish: Featured Items
Plugin URI: http://dunc.seriouslyfish.com
Description: This plugin allows administrators to control the "Featured.." banner on index.php of Seriously Fish.
Version: 0.1
Author: Duncan Wraight
Author URI: http://dunc.seriouslyfish.com
License: GPL2
Copyright 2012 DUNCAN WRAIGHT (email : DUNC@SERIOUSLYFISH.COM)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (!class_exists(\'FeaturedItem\')) :
class FeaturedItem {
var $plugin_url;
function FeaturedItem()
{
$this->plugin_url = trailingslashit(plugins_url(\'sf-featured-items\'));
}
function enqueue_scripts() {
/* start of replacement tinyMCE instance
wp_register_script(\'cleditor\', $this->plugin_url .\'cleditor/jquery.cleditor.min.js\', array(\'jquery\'));
wp_enqueue_script(\'cleditor\'); */
}
function enqueue_styles() {
/* Image upload styles
wp_enqueue_style(\'thickbox\'); */
}
function register_featured_item(){
$labels = array(
\'name\' => _x(\'Featured Item\', \'post type general name\'),
\'singular_name\' => _x(\'Featured Item\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'profile\'),
\'add_new_item\' => __(\'Add New Featured Item\'),
\'edit_item\' => __(\'Edit Featured Item\'),
\'new_item\' => __(\'New Featured Item\'),
\'view_item\' => __(\'View Featured Item\'),
\'search_items\' => __(\'Search Featured Items\'),
\'not_found\' => __(\'No featured items found\'),
\'not_found_in_trash\' => __(\'No featured items found in Trash\'),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Featured\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => false,
\'exclude_from_search\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => false,
\'capability_type\' => \'post\',
\'has_archive\' => false,
\'hierarchical\' => false,
\'menu_position\' => 50,
\'menu_icon\' => $this->plugin_url . \'images/star.png\',
\'supports\' => array(\'title\',\'editor\',\'thumbnail\')
);
add_filter(\'post_updated_messages\', \'featured_items_updated_messages\');
function featured_items_updated_messages( $messages ) {
global $post, $post_ID;
$messages[\'species\'] = array(
0 => \'\', // Unused. Messages start at index 1.
1 => sprintf( __(\'Featured item updated. <a href="%s">View</a>.\'), esc_url( get_permalink($post_ID) ) ),
2 => __(\'Custom field updated.\'),
3 => __(\'Custom field deleted.\'),
4 => __(\'Featured item updated.\'),
/* translators: %s: date and time of the revision */
5 => isset($_GET[\'revision\']) ? sprintf( __(\'Featured item restored to revision from %s.\'), wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
6 => sprintf( __(\'Featured item published. <a href="%s">View</a>.\'), esc_url( get_permalink($post_ID) ) ),
7 => __(\'Featured item saved.\'),
8 => sprintf( __(\'Featured item submitted. <a target="_blank" href="%s">Preview</a>.\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
9 => sprintf( __(\'Featured item scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview</a>.\'),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( \'M j, Y @ G:i\' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __(\'Featured item draft updated. <a target="_blank" href="%s">Preview</a>.\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
);
return $messages;
}
register_post_type(\'featured-item\', $args);
}
function meta_link(){
global $post;
if ($post->ID) {
$link_to_post = get_post_meta( $post->ID , \'link_to_post\', TRUE );
}
?>
<?php wp_nonce_field( plugin_basename( __FILE__ ), \'sf_featured_noncename\' ); ?>
<div class="meta_control">
<p>Make sure you use the permalink (i.e. written link) rather than the short link (numeric).</p>
<input type="text" name="link_to_post" class="range" value="<?php if (isset($link_to_post)) { echo esc_attr($link_to_post); } ?>" />
</div>
<?php
}
function register_featured_meta(){
add_meta_box("link_to_post", "Link to Post", array($this, \'meta_link\'), "featured-item", "normal", "core");
}
function update_featured_meta(){
global $post;
if ($post) {
if($post->post_type != \'featured-item\')
return;
if ( !wp_verify_nonce( $_POST[\'sf_featured_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Taxonomy meta
update_post_meta( $post->ID, "link_to_post", $_POST["link_to_post"] );
}
}
}
else :
exit("Class FeaturedItem already exists.");
endif ;
/* INSTANCE CLASS */
if (!isset($FeaturedItem)) {
$FeaturedItem = new FeaturedItem();
}
if (isset($FeaturedItem)) {
add_action( \'init\' , array (&$FeaturedItem, \'register_featured_item\' ));
add_action( \'admin_enqueue_scripts\', array (&$FeaturedItem, \'enqueue_scripts\' ));
add_action( \'admin_enqueue_scripts\', array (&$FeaturedItem, \'enqueue_styles\' ));
add_action( \'admin_init\' , array( &$FeaturedItem, \'register_featured_meta\' ));
add_action( \'save_post\' , array( &$FeaturedItem, \'update_featured_meta\' ));
}
?>
增加的复杂性可能是您不需要的—文章的链接。回顾过去,我本可以完全不使用它,只需使用富文本编辑器的内置超链接功能。
前端的代码也很简单:
创建辅助post查询(WP_Query
)仅检索类型的帖子featured-item
限制帖子使用posts_per_page => 3
;跑步<?php while ( $query->have_posts() ) : $query->the_post(); ?>
显示帖子,根据需要修改格式(下面代码中的格式非常复杂,因为我在互联网上看到了一个我喜欢的美学示例)<?php $query = new WP_Query(
array(
\'post_type\' => \'featured-item\',
\'posts_per_page\' => 3,
\'meta_query\' => array( array( \'key\' => \'_thumbnail_id\' ) )
)
);
?>
<div id="featured" >
<?php
$li = \'\';
$content = \'\';
$count = 1;
?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
if ($count == 1) {
$li .= \'<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-\'. $count .\'"><a href="#fragment-\'. $count .\'"><p>\'. get_the_title() . \'</p><span>\'. get_the_content() .\'</span></a></li>\';
$content .= \'<div id="fragment-\'. $count .\'" class="ui-tabs-panel" style="">
\'. get_the_post_thumbnail($post->ID, \'indexheader-thumb\') .\'
<div class="info" >
<p>\'. get_the_title() . \'</p>
<h2><a href="\'. get_post_meta( $post->ID , \'link_to_post\', TRUE ) .\'" >\'. get_the_content() .\'</a></h2>
</div>
</div>\';
} else {
$li .= \'<li class="ui-tabs-nav-item" id="nav-fragment-\'. $count .\'"><a href="#fragment-\'. $count .\'"><p>\'. get_the_title() .\'</p><span>\'. get_the_content() .\'</span></a></li>\';
$content .= \'<div id="fragment-\'. $count .\'" class="ui-tabs-panel ui-tabs-hide" style="">
\'. get_the_post_thumbnail($post->ID, \'indexheader-thumb\') .\'
<div class="info" >
<p>\'. get_the_title() . \'</p>
<h2><a href="\'. get_post_meta( $post->ID , \'link_to_post\', TRUE ) .\'" >\'. get_the_content() .\'</a></h2>
</div>
</div>\';
}
$count++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<ul class="ui-tabs-nav">
<?php echo $li; ?>
</ul>
<!-- First Content -->
<?php echo $content; ?>
</div>
如果你想尝试这样做,请随时向我询问更多信息。