创建简码以显示自定义帖子类型的分类术语帖子

时间:2018-11-21 23:28:56

标签: wordpress wordpress-theming custom-post-type custom-taxonomy wordpress-shortcode

我有一个简短的代码工作,该代码基本上以自定义布局(图像,标题,描述)显示来自自定义帖子类型的所有帖子。这确实非常有效,但是我还对带有各种术语的自定义帖子类型进行了分类。

我认为我可以添加一个属性,以便根据类别/术语显示自定义帖子类型的帖子,但是我还没有运气。

目前,我已经获得了显示短代码和分类法的代码:

            //shortcode to display posts
 function show_posts_shortcode( $atts ) {

//shortcode atts
$a = shortcode_atts( array(
    'post_type' => 'post',
    'pagename' => '',
    'limit' => '-1',
    'order' => 'DESC',
    'orderby' => 'date',
    'thumbnail_size' => 'large',
    'content' => '',
    'button' => '',
    'container_class' => '',
    'post_class' => '',
    'content_class' => '',
    'featured_bg' => 'false',
    'hide_featured' => '',
    'hide_title' => '',
    'show_custom_fields' => '',
    'link_target' => '_self',
    'bg_overlay' => 'transparent'),$atts);

// WP_Query arguments
$args = array(
    'post_type'              => explode( ',', $a[post_type] ),
    'pagename'               => $a['pagename'],
    //'posts_per_page'       => $a['count'], post_per_page doesn't work, altered while loop instead
    'nopaging'               => true,
    'ignore_sticky_posts'    => true,
    'order'                  => $a['order'],
    'orderby'                => $a['orderby'],
);

// The Query
$post_query = new WP_Query( $args );

// The Loop
if ( $post_query->have_posts() ) {
    $content_before;
    $content_output;
    $content_after;
    ob_start(); ?>

    <div class="<?php echo $a['container_class']; ?> clearfix"><?php  

    $content_before = ob_get_clean();

    $limit = (int)$a['limit']; //custom post limiter as post_per_page arg does not work
    $count = 0;

    while ( $post_query->have_posts() && $limit !== $count ) {
        $post_query->the_post();

        ob_start(); ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class($a['post_class']); ?> <?php generate_article_schema( 'CreativeWork' ); if( $a['featured_bg'] == 'true' ) { ?>style="background-image: url(<?php the_post_thumbnail_url('full'); ?>)" <?php } ?>>

                <div class="inside-article" <?php if( strlen($a['bg_overlay']) > 0 ) { ?>style="background-color: <?php echo $a['bg_overlay']; ?>"<?php } ?>>
                        <a href="<?php get_field('external_link') ? the_field('external_link') : the_permalink(); ?>" target="<?php echo $a['link_target']; ?>" title="<?php the_title(); ?>"><?php

                        if( $a['featured_bg'] !== 'true' && $a['hide_featured'] !== 'true' ) {

                            the_post_thumbnail( $a['thumbnail_size'] );
                        }

                        if( $a['hide_title'] !== 'true' ) {

                            the_title('<h2 class="entry-title" itemprop="headline">', '</h2>');
                        } ?>

                        <div class="entry-content <?php echo $a['content_class']; ?>" itemprop="text"><?php

                        if( $a['content'] == 'full' ) {

                            the_content();
                        }
                        elseif( $a['content'] == 'excerpt' || '' ) {

                            the_excerpt();
                        }

                        if( strlen($a['button']) > 0 ) {

                            ?><p><a class="button" href="<?php the_permalink(); ?>"><?php echo $a['button']; ?></a></p><?php
                        } ?>

                        </div>
                        </a>
                </div><!-- .inside-article -->
            </article><!-- #post-## -->
        <?php

        $content_output .= ob_get_clean();

        ob_start(); ?>

        </div><!-- container --><?php

        $content_after = ob_get_clean();

        $count++;
    }
    return $content_before . $content_output . $content_after;

    // Restore original Post Data
    wp_reset_postdata();
}
else
{
    ob_start();

    _e('No ' . $a['post_type'] . ' found...');

    return ob_get_clean();
 }
}
add_shortcode( 'show-posts', 'show_posts_shortcode' );


// cpt taxonomy
if ( ! function_exists( 'sponsor_group' ) ) {

// Register Custom Taxonomy
function sponsor_group() {

$labels = array(
    'name'                       => 'Sponsors',
    'singular_name'              => 'Sponsor',
    'menu_name'                  => 'Sponsors',
    'all_items'                  => 'All Sponsors',
    'parent_item'                => 'Parent Sponsor',
    'parent_item_colon'          => 'Parent Sponsor:',
    'new_item_name'              => 'New Sponsor Name',
    'add_new_item'               => 'Add New Sponsor',
    'edit_item'                  => 'Edit Sponsor',
    'update_item'                => 'Update Sponsor',
    'view_item'                  => 'View Sponsor',
    'separate_items_with_commas' => 'Separate Sponsors with commas',
    'add_or_remove_items'        => 'Add or remove Sponsors',
    'choose_from_most_used'      => 'Choose from the most used',
    'popular_items'              => 'Popular Items',
    'search_items'               => 'Search Sponsors',
    'not_found'                  => 'Not Found',
    'no_terms'                   => 'No Sponsors',
    'items_list'                 => 'Sponsors list',
    'items_list_navigation'      => 'Sponsors list navigation',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'sponsor', array( 'manufacturer' ), $args );

}
add_action( 'init', 'sponsor_group', 0 );

}

或者,我必须为每个赞助者组创建单独的简码,我只是想有一种更有效的方法,所以它可能类似于:

 [show-posts post_type="manufacturer" sponsor_group="education"]

这将显示制造商职位类型的所有教育赞助商。

非常感谢您的帮助或指出正确的方向。

0 个答案:

没有答案