doushang4293 2019-05-30 08:03
浏览 88
已采纳

如何创建短代码以使用其帖子ID显示特定的推荐?

i have created custom post type for testimonials in wordpress.i have added 89 testimonials and wants to display 2 which i wants to displany in home page . so wanted to created shortcode which will display testimonials according to their Post ID. can anyone please tell me the code for shortcode.

Below i am showing code i had written to create custom post type for testimonial . Pls tell me the code to create shortcode like this:-[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 );
  • 写回答

2条回答 默认 最新

  • duancongduo4109 2019-05-30 09:25
    关注

    Use the shortcode attributes with known attributes and fills in defaults when needed.

     function testimonials($atts) {
        $a = shortcode_atts( array(
            'posts_per_page' => '',
            'testimonial_id'  =>  ''
        ), $atts );
    
        $testimonials = '';
        $post_in = esc_attr($a['testimonial_id']);
        $posts_per_page = esc_attr($a['posts_per_page']);
    
        $post_artay = explode(',', $post_in);
        $args = array(
            'post__in' => $post_artay,
            'posts_per_page' => $posts_per_page,
            'post_type'        => 'testimonials',
            'order_by' => 'post__in',
        );
    
        // the query
        $the_query = new WP_Query( $args );
    
        if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
            $testimonials.= '<div class="title">'.get_the_title().'</div>';
            $testimonials.= '<div class="content">'.get_the_content().'</div>';
            $testimonials.='<div class="date">'.get_the_date().'</div>';
            $testimonials.='<div class="author">'.get_the_author().'</div>';
        endwhile;
        endif;
        return $testimonials;
    }
    add_shortcode('testimonial', 'testimonials' );
    

    use the shortcode like.

    [testimonial posts_per_page="5" testimonial_id="29,23"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件
  • ¥15 gis中用栅格计算器或加权总和后图层不显示,值也明显不对
  • ¥15 python使用python-pptx如何给幻灯片添加只读密码。