【问题标题】:Wordpress - shortcode to display custom post types in custom taxonomies in carouselWordpress - 在轮播的自定义分类中显示自定义帖子类型的简码
【发布时间】:2014-02-16 00:30:58
【问题描述】:

我找到了这样的东西: 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="movies"。 在我的自定义分类名称“类别”中,我有两个子类别“电影”和“照片”。 简码忽略选择的 'taxonomy_name' 并显示来自 post_type="gallery" 的所有自定义帖子。我想在我的 custom_taxonomy 中选择子类别来显示短代码中的自定义帖子类型。

请帮帮我,我卡住了:(

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    将此代码用于您的 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;
        }
    }
    
    

    【讨论】:

      【解决方案2】:
      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;
          }
      }
      

      【讨论】:

      • 点评来源: 您好,请不要只回答源代码。尝试对您的解决方案如何工作提供一个很好的描述。见:How do I write a good answer?。谢谢
      • 此代码粘贴 function.php 然后使用此短代码 [list-slider]
      【解决方案3】:

      您必须将“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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 2012-09-18
        • 2013-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多