Wordpress - 用于在轮播中的自定义分类中显示自定义帖子类型的短代码

时间:2014-01-24 21:01:02

标签: php wordpress

我发现这样的事: Is it possible to create a shortcode that will query a post based on taxonomies

function posts_shortcode_handler($atts, $content) {
extract(shortcode_atts(array(
    'posts_per_page' => '5',
    'post_type' => 'gallery'
                ), $atts));
global $post;
$temp = $post;
$posts = new WP_Query($atts);
$retVal = '';
if ($posts->have_posts()) {
    while ($posts->have_posts()) {
        $posts->the_post();
        // these arguments will be available from inside $content
        $parameters = array(
            'PERMALINK' => get_permalink(),
            'TITLE' => get_the_title(),
            'CONTENT' => get_the_content(),
            'CATEGORIES' => get_the_category_list(', '),
            'THUMBNAIL' => get_the_post_thumbnail()
        );
        $finds = $replaces = array();
        foreach ($parameters as $find => $replace) {
            $finds[] = '{' . $find . '}';
            $replaces[] = $replace;
        }
        $retVal .= str_replace($finds, $replaces, $content);
    }
}
wp_reset_query();
$post = $temp;
return $retVal;
}

add_shortcode('galerie', 'posts_shortcode_handler');

我的短代码如下:

[galerie post_type="gallery" posts_per_page="5" taxonomy_name="movies"]
<h5><a href="{PERMALINK}">{TITLE}</a></h5>
<div>{THUMBNAIL}
{CONTENT}</div>
[/galerie]

我的问题是关于taxonomy_name =“电影”不适合我。 在我的自定义分类名称'Kategorie'中,我有两个子类别“电影”和“照片”。 Shortcode忽略选择'taxonomy_name'并显示post_type =“gallery”中的所有自定义帖子。我想在custom_taxonomy中选择子类别,以便从短代码中显示自定义帖子类型。

请帮助我,我被困住了:(

3 个答案:

答案 0 :(得分:2)

在您的wp页面编辑器中使用此代码。这是函数输入:


add_shortcode( 'list-slider', 'slidermy' );

function slidermy( $atts ) {
    ob_start();
    $query = new WP_Query( array(
    'post_type' => 'slider',
    'color' => 'blue',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'title',
    ) );
    if ( $query->have_posts() ) { ?>
    <div class="home-slider">
    <div class="container">
    <div class="cycle-slideshow home-slideshow" data-cycle-slides="&gt; div" data-cycle-pager=".home-pager" data-cycle-timeout="5000" data-cycle-prev="#HomePrev" data-cycle-next="#HomeNext">
    <?php
    while ( $query->have_posts() ) : $query->the_post();
    $imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
    ?>
    <div class="slide" style=" background-image:url(<?php echo $imgurl;?>)">
    <div class="caption">
    <div class="con">
    <h1><?php the_title(); ?></h1>
    </div>
    </div>
    </div>
    <?php endwhile;
    wp_reset_postdata(); ?>
    </div>
    </div>
    </div>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

答案 1 :(得分:1)

add_shortcode( 'list-slider', 'slidermy' );
function slidermy( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'slider',
        'color' => 'blue',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'orderby' => 'title',
    ) );
    if ( $query->have_posts() ) { ?>
      <div class="home-slider">
       <div class="container">
        <div class="cycle-slideshow home-slideshow" data-cycle-slides="&gt; div" data-cycle-pager=".home-pager" data-cycle-timeout="5000" data-cycle-prev="#HomePrev" data-cycle-next="#HomeNext">
            <?php
                while ( $query->have_posts() ) : $query->the_post();
                $imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
            ?>
             <div class="slide" style=" background-image:url(<?php echo $imgurl;?>)">
               <div class="caption">
                    <div class="con">
                      <h1><?php the_title(); ?></h1>
                    </div>
               </div>
            </div>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </div>
       </div>
    </div>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

答案 2 :(得分:0)

您必须将“taxonomy_name”替换为分类法的名称。 您的短代码的第一行应如下所示:

[galerie post_type="gallery" posts_per_page="5" Kategorie="movies"]

这样,这是传递给WP_Query的$ atts数组:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => '5',
    'kategorie' => 'movies'
);

此处有更多详情: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters