首页 > 解决方案 > 如何创建短代码以使用其帖子 ID 显示特定推荐?

问题描述

我在 wordpress 中为推荐创建了自定义帖子类型。我添加了 89 个推荐,并希望显示 2 个我想在主页中显示的。所以想创建一个短代码,它将根据他们的帖子 ID 显示推荐。谁能告诉我简码的代码。

下面我展示了我编写的为推荐创建自定义帖子类型的代码。请告诉我创建这样的短代码的代码:-[testimonial posts_per_page="5" testimonial_id="123,145"]

function custom_post_testimonial_type() {

// Set UI labels for Custom Post Type
$labels = array(
    'name'=> _x( 'Testimonials', 'Post Type General Name', 'walker_theme' ),
 'singular_name'=> _x( 'Testimonial', 'Post Type Singular Name', 'walker_theme' ),
   'menu_name'=> __( 'Testimonials', 'walker_theme' ),
'parent_item_colon' => __( 'Testimonial', 'walker_theme' ),
'all_items'  => __( 'All Testimonials', 'walker_theme' ),
'view_item' => __( 'View Testimonial', 'walker_theme' ),
'add_new_item' => __( 'Add New Testimonial','walker_theme' ),
'add_new'  => __( 'Add New', 'walker_theme' ),
'edit_item'  => __( 'Edit Testimonial','walker_theme' ),
'update_item' => __( 'Update Testimonial','walker_theme' ),
'search_items' => __( 'Search Testimonial', 'walker_theme' ),
'not_found'           => __( 'Not Found', 'walker_theme' ),
'not_found_in_trash'  => __( 'Not found in Trash','walker_theme' ),
);

  // Set other options for Custom Post Type

$args = array(
'label'               => __( 'testimonials', 'walker_theme' ),
'description'         => __( 'Home page testimonials', 'walker_theme' ),
'labels'              => $labels,
    // Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'author','thumbnail', 'tags'),
    // You can associate this CPT with a taxonomy or custom 
      taxonomy. 
    'taxonomies'          => array( 'genres', 'post_tag' ),
    /* A hierarchical CPT is like Pages and can have
    * Parent and child items. A non-hierarchical CPT
    * is like Posts.
    */  
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
);

// Registering your Custom Post Type
register_post_type( 'testimonials', $args );

    }

    add_action( 'init', 'custom_post_testimonial_type', 0 );

标签: phpwordpresscustom-post-typeshortcodetestimonials

解决方案


嗨,让我们将此代码添加到主题的 functions.php 文件中。

add_shortcode( 'testimonial', 'testimonial_shortcode_callback' );

function testimonial_shortcode_callback( $atts ) {
ob_start();

extract( shortcode_atts( array(
    'posts_per_page' => 5,
    'testimonial_id' => '',
), $atts ) );

// define query parameters based on attributes
$options = array(
    'post_type'      => 'testimonials',
    'posts_per_page' => $posts_per_page,
);

if ( ! empty( $testimonial_id ) ) {
    $options['post__in'] = array_map( 'trim', explode( ',', $testimonial_id ) );
}

$testimonial_query = new WP_Query( $options );
// run the loop based on the query
if ( $testimonial_query->have_posts() ) :
    ?>
    <ul class="testimonial-listing">
        <?php
        while ( $testimonial_query->have_posts() ) : $testimonial_query->the_post();
            ?>
            <li id="testimonial-<?php the_ID(); ?>">
                <?php the_content(); ?>
            </li>
        <?php
        endwhile;
        wp_reset_postdata();
        ?>
    </ul>
    <?php
    $testimonial_output = ob_get_clean();

    return $testimonial_output;
endif;
}

比使用这个简码为例 [testimonial posts_per_page="2" testimonial_id="123,145"]


推荐阅读