You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress能否用自定义文章类型展示推荐语?及实现方法

Hey there! Great questions—yes, WordPress absolutely lets you use a custom post type (CPT) to showcase testimonials, and it’s a clean, scalable way to manage them separately from regular posts or pages. Let’s break down how to make this work step by step:

1. Confirmation: WordPress CAN Use Custom Post Types for Testimonials

Custom Post Types are one of WordPress’s most powerful features—they’re designed to let you create and manage unique content types beyond the default posts and pages. Testimonials are a perfect use case here: you can store each testimonial as its own entry, add custom details like the author’s name/title, and display them anywhere on your site without cluttering up your regular content library.

2. Step-by-Step Implementation

2.1 Register the Testimonial Custom Post Type

First, we need to tell WordPress to create our new "Testimonials" post type. You can add this code to your theme’s functions.php file (or a custom plugin, which is safer if you plan to switch themes later—pro tip: always back up your file before editing!).

function register_testimonial_cpt() {
    $args = array(
        'public' => true,
        'label'  => 'Testimonials',
        'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
        'menu_icon' => 'dashicons-format-quote', // Uses a quote icon for the admin menu
        'rewrite' => array( 'slug' => 'testimonials' ),
        'has_archive' => true,
    );
    register_post_type( 'testimonial', $args );
}
add_action( 'init', 'register_testimonial_cpt' );
  • public: true makes the post type visible in the WordPress admin and on the front end
  • supports defines which features each testimonial can use (title, editor for the quote, thumbnail for author photos, etc.)
  • menu_icon adds a recognizable quote icon to your admin menu
  • rewrite sets the URL slug for your testimonials archive (e.g., yourdomain.com/testimonials)
  • has_archive enables an archive page to display all testimonials

After adding this code, you’ll see a new "Testimonials" option in your WordPress admin sidebar!

2.2 Add Custom Fields (Optional but Super Useful)

Most testimonials need more than just a quote—you’ll want to include the author’s name, job title, and maybe their company. You can add custom fields manually using WordPress’s built-in custom fields, but for a cleaner admin experience, let’s add a dedicated meta box with code:

// Add meta box to testimonial post type
function testimonial_meta_box() {
    add_meta_box(
        'testimonial_author_meta',
        'Testimonial Author Details',
        'render_testimonial_author_meta',
        'testimonial',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'testimonial_meta_box' );

// Render the meta box content
function render_testimonial_author_meta( $post ) {
    // Security nonce to prevent unauthorized saves
    wp_nonce_field( 'save_testimonial_meta', 'testimonial_meta_nonce' );
    
    // Get existing values if they exist
    $author_name = get_post_meta( $post->ID, '_testimonial_author_name', true );
    $author_title = get_post_meta( $post->ID, '_testimonial_author_title', true );
    
    // Output form fields
    echo '<p><label for="testimonial_author_name">Author Name:</label><br>';
    echo '<input type="text" id="testimonial_author_name" name="testimonial_author_name" value="' . esc_attr( $author_name ) . '" size="40"></p>';
    
    echo '<p><label for="testimonial_author_title">Author Title/Company:</label><br>';
    echo '<input type="text" id="testimonial_author_title" name="testimonial_author_title" value="' . esc_attr( $author_title ) . '" size="40"></p>';
}

// Save the meta box data
function save_testimonial_meta( $post_id ) {
    // Verify nonce and permissions
    if ( !isset( $_POST['testimonial_meta_nonce'] ) || !wp_verify_nonce( $_POST['testimonial_meta_nonce'], 'save_testimonial_meta' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( !current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
    
    // Sanitize and save data
    if ( isset( $_POST['testimonial_author_name'] ) ) {
        update_post_meta( $post_id, '_testimonial_author_name', sanitize_text_field( $_POST['testimonial_author_name'] ) );
    }
    if ( isset( $_POST['testimonial_author_title'] ) ) {
        update_post_meta( $post_id, '_testimonial_author_title', sanitize_text_field( $_POST['testimonial_author_title'] ) );
    }
}
add_action( 'save_post', 'save_testimonial_meta' );

Now when you edit a testimonial, you’ll see a new box at the bottom to enter the author’s details.

2.3 Display Testimonials on Your Site

There are two main ways to show your testimonials: using a custom archive page, or adding them anywhere with a shortcode.

Option 1: Custom Archive Page

Create a dedicated page to show all testimonials. Copy your theme’s archive.php file, rename it archive-testimonial.php, and modify the loop to display your testimonial content:

<?php get_header(); ?>

<div class="container">
    <h1>Our Client Testimonials</h1>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div class="testimonial-card">
            <?php if ( has_post_thumbnail() ) : ?>
                <div class="testimonial-avatar">
                    <?php the_post_thumbnail( 'thumbnail' ); ?>
                </div>
            <?php endif; ?>
            <div class="testimonial-content">
                <blockquote><?php the_content(); ?></blockquote>
                <p class="testimonial-author">
                    <?php echo get_post_meta( get_the_ID(), '_testimonial_author_name', true ); ?>
                    <span class="testimonial-title"><?php echo get_post_meta( get_the_ID(), '_testimonial_author_title', true ); ?></span>
                </p>
            </div>
        </div>
    <?php endwhile; else : ?>
        <p>No testimonials have been added yet!</p>
    <?php endif; ?>
</div>

<?php get_footer(); ?>

Now, when you visit yourdomain.com/testimonials, you’ll see all your testimonials displayed in this format.

Option 2: Shortcode for Any Page/Post

If you want to add testimonials to a specific page (like your homepage), use a shortcode. Add this to your functions.php:

function testimonial_shortcode( $atts ) {
    // Default shortcode attributes
    $atts = shortcode_atts( array(
        'number' => 3, // Show 3 testimonials by default
    ), $atts );
    
    // Query testimonial posts
    $args = array(
        'post_type' => 'testimonial',
        'posts_per_page' => $atts['number'],
        'orderby' => 'date',
        'order' => 'DESC',
    );
    
    $testimonials = new WP_Query( $args );
    
    ob_start();
    if ( $testimonials->have_posts() ) : ?>
        <div class="testimonial-grid">
            <?php while ( $testimonials->have_posts() ) : $testimonials->the_post(); ?>
                <div class="testimonial-item">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <?php the_post_thumbnail( 'medium' ); ?>
                    <?php endif; ?>
                    <blockquote><?php the_excerpt(); ?></blockquote>
                    <h4><?php echo get_post_meta( get_the_ID(), '_testimonial_author_name', true ); ?></h4>
                    <p class="testimonial-title"><?php echo get_post_meta( get_the_ID(), '_testimonial_author_title', true ); ?></p>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else : ?>
        <p>No testimonials available at the moment.</p>
    <?php endif;
    wp_reset_postdata(); // Reset post data to avoid conflicts
    return ob_get_clean();
}
add_shortcode( 'display_testimonials', 'testimonial_shortcode' );

Now you can add [display_testimonials number="5"] to any page or post to show 5 testimonials (adjust the number as needed).

Add some CSS to make your testimonials look polished. You can add this to your theme’s custom CSS editor or style.css file:

.testimonial-card {
    margin-bottom: 2rem;
    padding: 1.5rem;
    border: 1px solid #eee;
    border-radius: 8px;
    display: flex;
    gap: 1rem;
    align-items: flex-start;
    background-color: #f9f9f9;
}

.testimonial-avatar img {
    border-radius: 50%;
    width: 80px;
    height: 80px;
    object-fit: cover;
    border: 2px solid #ddd;
}

.testimonial-content blockquote {
    font-style: italic;
    color: #555;
    margin: 0 0 1rem;
    line-height: 1.6;
}

.testimonial-author {
    font-weight: 600;
    margin: 0;
    color: #222;
}

.testimonial-title {
    font-weight: normal;
    color: #777;
    font-size: 0.9rem;
    display: block;
    margin-top: 0.25rem;
}

.testimonial-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 2rem;
    margin-top: 2rem;
}

.testimonial-item {
    text-align: center;
    padding: 1.5rem;
    border: 1px solid #eee;
    border-radius: 8px;
}

.testimonial-item img {
    border-radius: 50%;
    margin: 0 auto 1rem;
}

内容的提问来源于stack exchange,提问作者john andy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:27:34