首页 文章

显示自定义帖子类型w / Taxonomy / Category Shortcode

提问于
浏览
0

我正在创建一个短代码函数,它将显示一个自定义的帖子类型,并且可以通过其类别/分类法进行过滤 . 我已经弄清楚如何创建短代码并让它显示帖子类型 . 但我不能让它显示相关的分类法类别 .

我创建了一个名为“testimonials”的自定义帖子类型,然后创建了一个名为“testimonial-category”的分类 . 在这个分类中,我有一个名为'testimonial-home'的类别 .

我希望能够使用带有'testimonial-home'的短代码作为其过滤器,以显示主页推荐 .

我的短代码没有属性,效果很好:[list-testimonials] - 显示所有推荐 . 但是当我添加:[list-testimonials category =“testimonial-home”]时,不会显示任何内容 .

我很难过 . 我很亲密,我确信有一些非常明显的东西我正在俯瞰 . 任何和所有的帮助非常感谢!谢谢!

这是我的短代码功能:

//使用参数创建短代码,以便用户可以定义所查询的内容 - 默认是列出所有博客帖子

add_shortcode( 'list-testimonials', 'post_listing_parameters_shortcode' );
    function post_listing_parameters_shortcode( $atts ) {
    ob_start();

    // define attributes and their defaults
    extract( shortcode_atts( array (
        'type' => 'testimonials',
        'order' => 'date',
        'orderby' => 'title',
        'posts' => -1,
        'category' => '',
    ), $atts ) );

    // define query parameters based on attributes
    $options = array(
        'post_type' => $type,
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,
        'category_name' => $category,
    );
    $query = new WP_Query( $options );
    if ( $query->have_posts() ) { ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div
        class="small-12 medium-12 large-4 columns testimonial-column homepage-    testimonial-column">

        <!--HOME PAGE SINGLE TESTIMONIAL CONTAINER-->
        <div
            class="testimonial-container homepage-testimonial-container"  
            id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <!--HOME PAGE SINGLE TESTIMONIAL TEXT-->
            <?php if( get_field('testimonial_text') ): ?>
            <div
                 class="testimonial testimonial-textarea">
                    <?php the_field('testimonial_text'); ?>   
            </div>
            <?php endif ?>
            <!--END HOME PAGE SINGLE TESTIMONIAL TEXT-->

            <!--HOME PAGE SINGLE TESTIMONIAL DETAILS-->
            <div
                 class="testimonial-details">

                <!--HOME PAGE SINGLE TESTIMONIAL IMAGE-->
                <?php if( get_field('testimonial_photo') ): ?>
                <img
                     src="<?php the_field('testimonial_photo'); ?>"
                     class="testimonial-photo" />
                <?php endif ?>

                <!--HOME PAGE SINGLE TESTIMONIAL BIO INFO-->
                <div
                    class="testimonial-bio">

                    <!--HOME PAGE SINGLE TESTIMONIAL NAME-->
                    <?php if( get_field('testimonial_name') ): ?>
                    <h5
                        class="testimonial-name">
                            <?php the_field('testimonial_name'); ?> 
                    </h5>
                    <?php endif ?>
                    <!--END HOME PAGE SINGLE TESTIMONIAL NAME-->

                    <!--HOME PAGE SINGLE TESTIMONIAL TITLE-->
                    <?php if( get_field('testimonial_title') ): ?>
                    <p
                       class="testimonial-title">
                           <?php the_field('testimonial_title'); ?>
                    </p>
                    <?php endif ?>
                    <!--END HOME PAGE SINGLE TESTIMONIAL TITLE-->

                    <!--HOME PAGE SINGLE TESTIMONIAL COMPANY-->
                    <?php if( get_field('testimonial_company') ): ?>
                    <p
                       class="testimonial-company">
                           <?php the_field('testimonial_company'); ?>
                    </p>
                    <?php endif ?>
                    <!--END HOME PAGE SINGLE TESTIMONIAL COMPANY-->

                </div>
                <!--END HOME PAGE SINGLE TESTIMONIAL BIO INFO-->

            </div>
            <!--END HOME PAGE SINGLE TESTIMONIAL DETAILS-->

    </div>
    <!--END HOME PAGE SINGLE TESTIMONIAL CONTAINER-->
    </div>
    <!--HOME PAGE TESTIMONIALS COLUMN-->

        <?php endwhile;
        wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable; 
   }    
}

1 回答

  • 1

    我会开始(如果你没有首先测试它的重要性 . 另外,要超级迂腐:当提到自定义分类法时,类别的术语是 term 或术语 .

    对于它的 Value ,一个原始的自定义post类型循环示例:

    <?php
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'testimonials', // This is the CPT's slug!
        'tax_query' => array(
            array(
                'taxonomy' => 'testimonial-category', // This is the taxonomy's slug!
                'field' => 'slug',
                'terms' => array('testimonial-home') // This is the term's slug!
            )
        ),
        'order' => 'ASC',
        'orderby' => 'menu_order'
      );
    $my_query = new WP_Query( $args );
    
    if($my_query->have_posts()): 
      while($my_query->have_posts()): $my_query->the_post(); ?>
          <li>
            <h3><?php the_title(); ?></h3>
            <?php if($thumbnail): ?>
              <img src="<?php echo $thumbnail[url]; ?>" />
            <?php endif; ?>
            <p><?php the_content(); ?></p>
          </li>
      <?php endwhile; // End while $my_query->have_posts
    endif; // End if $my_query->have_posts
    
    wp_reset_postdata(); ?>
    

相关问题