WordPress是否可以使用自定义帖子类型显示推荐书,以及如何操作?
提前致谢。
答案 0 :(得分:0)
是的,您可以在WordPress中使用自定义帖子类型来显示推荐。
推荐自定义帖子类型
自定义帖子类型非常适合根据不同需求分离您的内容。特别是如果你的自定义内容不需要所有的直接帖子的花里胡哨。
add_action( 'init', 'testimonials_post_type' );
function testimonials_post_type() {
$labels = array(
'name' => 'Testimonials',
'singular_name' => 'Testimonial',
'add_new' => 'Add New',
'add_new_item' => 'Add New Testimonial',
'edit_item' => 'Edit Testimonial',
'new_item' => 'New Testimonial',
'view_item' => 'View Testimonial',
'search_items' => 'Search Testimonials',
'not_found' => 'No Testimonials found',
'not_found_in_trash' => 'No Testimonials in the trash',
'parent_item_colon' => '',
);
register_post_type( 'testimonials', array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'exclude_from_search' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 10,
'supports' => array( 'editor' ),
'register_meta_box_cb' => 'testimonials_meta_boxes', // Callback function for custom metaboxes
) );
}
添加Metabox
现在已经为您的推荐创建了自定义帖子类型,并且您已经为自定义元框建立了回调,您需要设置这些元框的显示方式。所以接下来你需要使用add_meta_box()
函数来做到这一点。
function testimonials_meta_boxes() {
add_meta_box( 'testimonials_form', 'Testimonial Details', 'testimonials_form', 'testimonials', 'normal', 'high' );
}
function testimonials_form() {
$post_id = get_the_ID();
$testimonial_data = get_post_meta( $post_id, '_testimonial', true );
$client_name = ( empty( $testimonial_data['client_name'] ) ) ? '' : $testimonial_data['client_name'];
$source = ( empty( $testimonial_data['source'] ) ) ? '' : $testimonial_data['source'];
$link = ( empty( $testimonial_data['link'] ) ) ? '' : $testimonial_data['link'];
wp_nonce_field( 'testimonials', 'testimonials' );
?>
<p>
<label>Client's Name (optional)</label><br />
<input type="text" value="<?php echo $client_name; ?>" name="testimonial[client_name]" size="40" />
</p>
<p>
<label>Business/Site Name (optional)</label><br />
<input type="text" value="<?php echo $source; ?>" name="testimonial[source]" size="40" />
</p>
<p>
<label>Link (optional)</label><br />
<input type="text" value="<?php echo $link; ?>" name="testimonial[link]" size="40" />
</p>
<?php
}
保存自定义元
由于您添加了自定义元框,因此您需要确保验证并保存所有数据。您需要挂钩save_post操作并设置回调函数。
add_action( 'save_post', 'testimonials_save_post' );
function testimonials_save_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( ! empty( $_POST['testimonials'] ) && ! wp_verify_nonce( $_POST['testimonials'], 'testimonials' ) )
return;
if ( ! empty( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
}
if ( ! wp_is_post_revision( $post_id ) && 'testimonials' == get_post_type( $post_id ) ) {
remove_action( 'save_post', 'testimonials_save_post' );
wp_update_post( array(
'ID' => $post_id,
'post_title' => 'Testimonial - ' . $post_id
) );
add_action( 'save_post', 'testimonials_save_post' );
}
if ( ! empty( $_POST['testimonial'] ) ) {
$testimonial_data['client_name'] = ( empty( $_POST['testimonial']['client_name'] ) ) ? '' : sanitize_text_field( $_POST['testimonial']['client_name'] );
$testimonial_data['source'] = ( empty( $_POST['testimonial']['source'] ) ) ? '' : sanitize_text_field( $_POST['testimonial']['source'] );
$testimonial_data['link'] = ( empty( $_POST['testimonial']['link'] ) ) ? '' : esc_url( $_POST['testimonial']['link'] );
update_post_meta( $post_id, '_testimonial', $testimonial_data );
} else {
delete_post_meta( $post_id, '_testimonial' );
}
}
自定义列表视图
在您创建了第一个推荐书后,您会看到它出现在自定义帖子类型的列表视图中;但是,您将看不到任何自定义元数据。
这是一个简单的修复:您只需添加更多功能来自定义列表视图列,以便显示您想要查看的所有信息。
add_filter( 'manage_edit-testimonials_columns', 'testimonials_edit_columns' );
function testimonials_edit_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => 'Title',
'testimonial' => 'Testimonial',
'testimonial-client-name' => 'Client\'s Name',
'testimonial-source' => 'Business/Site',
'testimonial-link' => 'Link',
'author' => 'Posted by',
'date' => 'Date'
);
return $columns;
}
add_action( 'manage_posts_custom_column', 'testimonials_columns', 10, 2 );
function testimonials_columns( $column, $post_id ) {
$testimonial_data = get_post_meta( $post_id, '_testimonial', true );
switch ( $column ) {
case 'testimonial':
the_excerpt();
break;
case 'testimonial-client-name':
if ( ! empty( $testimonial_data['client_name'] ) )
echo $testimonial_data['client_name'];
break;
case 'testimonial-source':
if ( ! empty( $testimonial_data['source'] ) )
echo $testimonial_data['source'];
break;
case 'testimonial-link':
if ( ! empty( $testimonial_data['link'] ) )
echo $testimonial_data['link'];
break;
}
}
展示推荐书
如果您想在主题的某个页面模板中的某个位置显示推荐,则需要创建一个功能来执行此操作。这是一个快速的,允许您显示客户推荐。您可以使用参数来使用ID选择一个特定的推荐,或者甚至通过传递'orderby'值来显示随机的推荐。
/**
* Display a testimonial
*
* @param int $post_per_page The number of testimonials you want to display
* @param string $orderby The order by setting https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
* @param array $testimonial_id The ID or IDs of the testimonial(s), comma separated
*
* @return string Formatted HTML
*/
function get_testimonial( $posts_per_page = 1, $orderby = 'none', $testimonial_id = null ) {
$args = array(
'posts_per_page' => (int) $posts_per_page,
'post_type' => 'testimonials',
'orderby' => $orderby,
'no_found_rows' => true,
);
if ( $testimonial_id )
$args['post__in'] = array( $testimonial_id );
$query = new WP_Query( $args );
$testimonials = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$testimonial_data = get_post_meta( $post_id, '_testimonial', true );
$client_name = ( empty( $testimonial_data['client_name'] ) ) ? '' : $testimonial_data['client_name'];
$source = ( empty( $testimonial_data['source'] ) ) ? '' : ' - ' . $testimonial_data['source'];
$link = ( empty( $testimonial_data['link'] ) ) ? '' : $testimonial_data['link'];
$cite = ( $link ) ? '<a href="' . esc_url( $link ) . '" target="_blank">' . $client_name . $source . '</a>' : $client_name . $source;
$testimonials .= '<aside class="testimonial">';
$testimonials .= '<span class="quote">“</span>';
$testimonials .= '<div class="entry-content">';
$testimonials .= '<p class="testimonial-text">' . get_the_content() . '<span></span></p>';
$testimonials .= '<p class="testimonial-client-name"><cite>' . $cite . '</cite>';
$testimonials .= '</div>';
$testimonials .= '</aside>';
endwhile;
wp_reset_postdata();
}
return $testimonials;
}
推荐书短信
您可能还希望在帖子或网页内容中显示推荐。那不是问题。您需要做的只是挂钩WordPress Shortcode API。
add_shortcode( 'testimonial', 'testimonial_shortcode' );
/**
* Shortcode to display testimonials
*
* [testimonial posts_per_page="1" orderby="none" testimonial_id=""]
*/
function testimonial_shortcode( $atts ) {
extract( shortcode_atts( array(
'posts_per_page' => '1',
'orderby' => 'none',
'testimonial_id' => '',
), $atts ) );
return get_testimonial( $posts_per_page, $orderby, $testimonial_id );
}
推荐存档页面模板
由于推荐书需要自定义元,因此您无法依赖默认存档页面模板来正确显示它们。要设置自定义存档页面,您需要创建一个名为archive-testimonials.php的文件,并将其添加到主题的主文件夹中。
<?php
/**
* Archive template for client testimonials
*/
get_header(); ?>
<section id="primary" class="site-content">
<div id="content" role="main">
<header class="archive-header">
<h1 class="archive-title">Testimonials</h1>
</header><!-- #archive-header -->
<?php while ( have_posts() ) : the_post();
$testimonial_data = get_post_meta( get_the_ID(), '_testimonial', true );
$client_name = ( empty( $testimonial_data['client_name'] ) ) ? '' : $testimonial_data['client_name'];
$source = ( empty( $testimonial_data['source'] ) ) ? '' : ' - ' . $testimonial_data['source'];
$link = ( empty( $testimonial_data['link'] ) ) ? '' : $testimonial_data['link'];
$cite = ( $link ) ? '<a href="' . esc_url( $link ) . '" target="_blank">' . $client_name . $source . '</a>' : $client_name . $source;
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'testimonial' ); ?>>
<span class="quote">“</span>
<div class="entry-content">
<p class="testimonial-text"><?php echo get_the_content(); ?><span></span></p>
<p class="testimonial-client-name"><cite><?php echo $cite; ?></cite></p>
</div>
</article>
<?php endwhile; ?>
<?php
global $wp_query;
if ( 1 < $wp_query->max_num_pages ) : ?>
<nav class="archive-navigation" role="navigation">
<div class="nav-previous alignleft"><?php next_posts_link( '<span class="meta-nav">←</span> Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts <span class="meta-nav">→</span>' ); ?></div>
</nav><!-- .archive-navigation -->
<?php
endif;
?>
</div>
</section><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>