php - 如何在 WordPress 中显示推荐自定义帖子类型?

标签 php css wordpress

WordPress 是否可以使用自定义帖子类型显示推荐,以及如何做到这一点?

提前致谢。

最佳答案

是的,您可以使用 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
    ) );
}

添加元框

既然已经为您的推荐创建了自定义帖子类型,并且您已经为自定义元框建立了回调,您需要设置这些元框的显示方式。因此接下来您需要使用 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
}

保存自定义元

由于您添加了自定义元框,因此您需要确保所有数据都经过验证并保存。您需要 Hook 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' );
    }
}

自定义 ListView

创建第一个推荐后,您会看到它出现在自定义帖子类型的 ListView 中;但是,您不会看到任何自定义元数据。

这是一个简单的解决方法:您只需添加几个函数来自定义 ListView 列,以便显示您想要查看的所有信息。

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">&ldquo;</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">&ldquo;</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">&larr;</span> Older posts' ); ?></div>
                    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts <span class="meta-nav">&rarr;</span>' ); ?></div>
                </nav><!-- .archive-navigation -->
                <?php
            endif;
            ?>
        </div>

    </section><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

关于php - 如何在 WordPress 中显示推荐自定义帖子类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49505721/

相关文章:

php - 通过PHP向MySQL添加数据

php - 如何在 Symfony2 项目中使用 Zend GData

php - div 元素的背景图像无法在 php 文件中显示

css - Sass 与 CSS 性能对比 - Wordpress

mysql - 如何在 IIS 中查找 mysql 的连接字符串详细信息?

php - 渲染 javascript 文件,同时将其作为静态文件使用

php - 我的 WHERE 查询的 MYSQL 部分未运行

iphone - 如何停止在 Iphone Safari 中的两个表之间显示正文背景

IE8 中的 CSS 下划线变体

jquery - 类型错误 : 'undefined' is not a function (evaluating '$(document)' )