Wordpress的简码

时间:2016-03-08 08:19:43

标签: php wordpress content-management-system

我正在尝试为Wordpress制作一个短代码,它会显示基于" id"的特定帖子。的帖子。

我在下面找到了一个短代码示例,其中显示了" category1"的最新帖子。

对于我该怎么做才能使这项工作的任何想法。

function pp_recent_work($atts, $content ) {
 extract(shortcode_atts(array(
    'limit'=>'4',
    'title' => 'Recent Work',
    'orderby'=> 'date',
    'order'=> 'DESC',
    'filters' => '',
    'carousel' => 'yes'
    ), $atts));
    $output = '';
    if($filters){
        $filterstemparray = explode(',', $filters);
        if (count($filterstemparray)>1) {
            $filtersarray = $filterstemparray;

        } else {
            $filtersarray = $filterstemparray[0];
        }
    };
    if($filters=="all" || empty($filters)) {
         $wp_query = new WP_Query(
            array(
                'post_type' => array('portfolio'),
                'showposts' => $limit,
                'orderby' => $orderby,
                'order' => $order
                ));
    } else {
       $wp_query = new WP_Query(
        array(
            'post_type' => array('portfolio'),
            'showposts' => $limit,
            'orderby' => $orderby,
            'order' => $order,
            'tax_query' => array(
                array(
                    'taxonomy' => 'filters',
                    'field' => 'slug',
                    'terms' => $filtersarray
                    )
                ),
            )
            );
    }

    if ( $wp_query->have_posts() ):
    while( $wp_query->have_posts() ) : $wp_query->the_post();

        $id = $wp_query->post->ID;
        $type = get_post_meta($id, 'pp_pf_type', true);
        if($carousel == 'yes') { $output .= '<li class="four columns">'; } else { $output .= '<div class="four columns">';}
        $output .= '<a href="'.get_permalink().'" class="portfolio-item"><figure>';

        $videothumbtype = ot_get_option('portfolio_videothumb');
            if($type == 'video' && $videothumbtype == 'video') {
            global $wp_embed;
            $videolink = get_post_meta($id, 'incr_pfvideo_link', true);
            $post_embed = $wp_embed->run_shortcode('[embed  width="220" height="147"]'.$videolink.'[/embed]') ;
            $output .= '<div class="picture recent_video">'.$post_embed.'</div>';
        } else {
            if ( has_post_thumbnail()) {
                $output .= get_the_post_thumbnail($wp_query->post->ID,'portfolio-thumb');
            }
        }
        $output .= '<figcaption class="item-description"><h5>'.get_the_title().'</h5>';
            $terms = get_the_terms( $wp_query->post->ID, 'filters' );
            if ( $terms && ! is_wp_error( $terms ) ) : $output .= '<span>';
                $filters = array();
                $i = 0;
                foreach ( $terms as $term ) {
                    $filters[] = $term->name;
                    if ($i++ > 0) break;
                }
                $outputfilters = join( ", ", $filters ); $output .= $outputfilters;
            $output .= '</span>';
            endif;
}
add_shortcode('recent_work', 'pp_recent_work');

2 个答案:

答案 0 :(得分:0)

使用此短代码

function pp_recent_work($atts, $content ) {

    $post_IDS = explode(',', $atts['post_ids']);

    if($filters=="all" || empty($filters)) {
         $wp_query = new WP_Query(
            array(
                'post_type' => array('portfolio'),
                'showposts' => $limit,
                'orderby' => $orderby,
                'order' => $order
                ));
    } else {
       $wp_query = new WP_Query(
        array(
            'post_type' => array('portfolio'),
            'showposts' => $limit,
            'orderby' => $orderby,
            'order' => $order,
            'post__in'=> array($post_IDS),
            )
        );
    }

    if ( $wp_query->have_posts() ):
    while( $wp_query->have_posts() ) : $wp_query->the_post();

        $id = $wp_query->post->ID;

        //do the stuff here

    endwhile;
    endif;
}
add_shortcode('recent_work', 'pp_recent_work');

并在页面中使用短代码[recent_work post_ids="21,22,23"]为多个帖子传递ID作为逗号分隔字符串

单个邮寄通行证ID为[recent_work post_ids="21"]

答案 1 :(得分:0)

在你的示例之后//在这里做了什么,我添加了以下内容,然后添加了你在帖子中提到的短代码。

它会返回特定类别的帖子,但不会返回我在短代码中指定的ID的帖子。

问题可能在下面的代码中?

{ $output .= '<div class="four columns">';}
        $output .= '<a href="'.get_permalink().'" class="portfolio-item"><figure>';

            if ( has_post_thumbnail()) {
                $output .= get_the_post_thumbnail($wp_query->post->ID,'post_ids');
            }

        $output .= '<figcaption class="item-description"><h5>'.get_the_title().'</h5>';