简体   繁体   中英

Wordpress - shortcode to display custom post types in custom taxonomies in carousel

I`ve found sth like this: 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');

My shortcode looks like this:

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

My problem is about the taxonomy_name="movies" that`s not working for me. In my custom taxonomy name 'Kategorie' I have two sub categories 'movies' and 'photos'. Shortcode ignores choosen 'taxonomy_name' and display all custom posts from post_type="gallery". I would like to choose sub category in my custom_taxonomy to display custom post type from shortcode.

Please Help me, I`m stuck :(

Use this Code your wp page editor. this is function input:


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;
    }
}

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;
    }
}

You have to replace "taxonomy_name" with the name of your taxonomy. The first line of your shortcode should look like this:

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

That way, this is the $atts array passed to WP_Query:

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

More details here: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM