使用Wordpress短代码获取自定义帖子类型/分类

时间:2019-05-13 09:41:27

标签: php wordpress

由于某些原因,我无法使用Wordpress文档并创建了此自定义功能/简码。

/** Get Stories Shortcode **/

    function register_shortcodes() {
        add_shortcode( 'stories', 'stories_func' );
    }
    add_action( 'init', 'register_shortcodes' );

    function stories_func( $atts ) {

        global $wp_query, $post;

        $atts = shortcode_atts( array(
            'cat' => ''
        ), $atts );

        $loop = new WP_Query( array(
            'posts_per_page'    => 4,
            'post_type'         => 'stories',
            'orderby'           => 'rand',
            'tax_query'         => array (
                array (
                    'taxonomy'  => 'story_category',
                    'field'     => 'slug',
                    'terms'     => array (sanitize_title($atts['cat']))
                )
            )
        ));

        if( ! $loop->have_posts() ) {
            return false;
        }

        while( $loop->have_posts() ) {
            $loop->the_post();
            echo the_title();
        }

        wp_reset_postdata();
    }

我使用的简码是:[stories cat =“ Career Stories”]

2 个答案:

答案 0 :(得分:1)

Shortcode应该始终返回值,而不是在回调函数中回显。请检查以下示例。简码输出使用ob_get_contents()函数收集,并在最后返回。

function register_shortcodes() {
    add_shortcode( 'stories', 'stories_func' );
}
add_action( 'init', 'register_shortcodes' );

function stories_func( $atts ) {

    global $wp_query, $post;

    $atts = shortcode_atts( array(
        'cat' => ''
        ), $atts );


    $loop = new WP_Query( array(
        'posts_per_page'    => 4,
        'post_type'         => 'stories',
        'orderby'           => 'rand',
        'tax_query'         => array (
            array (
                'taxonomy'  => 'story_category',
                'field'     => 'slug',
                'terms'     => array (sanitize_title($atts['cat']))
                )
            )
        ));

    if( ! $loop->have_posts() ) {
        return false;
    }

    ob_start();

    while( $loop->have_posts() ) {
        $loop->the_post();
        the_title();
    }

    wp_reset_postdata();

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}

答案 1 :(得分:0)

这里是更新的代码。

function register_shortcodes() {
    add_shortcode( 'stories', 'stories_func' );
}
add_action( 'init', 'register_shortcodes' );

/**
 * Produtos Shortcode Callback
 * 
 * @param Array $atts
 *
 * @return string
 */
function stories_func( $atts ) {
    global $wp_query,
        $post;
   $terms = get_terms( array(
    'taxonomy' => 'story_category',
    'hide_empty' => false,
    ) );
   foreach($terms as $term)
   {
     $term_slugs[] = $term->slug;
   }
    $atts = $term_slugs;

    $loop = new WP_Query( array(
        'posts_per_page'    => 4,
        'post_type'         => 'stories',
        'orderby'           => 'rand',
        'tax_query'         => array( array(
            'taxonomy'  => 'story_category',
            'field'     => 'slug',
            'terms'     => $atts
        ) )
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }

    while( $loop->have_posts() ) {
        $loop->the_post();
        echo the_title();
    }

    wp_reset_postdata();
}

经过测试,效果很好