categories - Add category attribute to custom shortcode

I have created a custom post type called "faq". The custom post type uses categories. I have created a shortco

I have created a custom post type called "faq". The custom post type uses categories. I have created a shortcode [faq] that displays all the faq posts.

What I would like to do is add an attribute to the shortcode that specifies an faq category. So instead of using the shortcode [faq] it would be something like [faq category="travel"] and it would only display the posts from the "travel" category.

I am using the following code for the shortcode at the moment. I just need to add the category attribute somehow:

add_shortcode('faq', function() {

    $posts = get_posts(array(  //Get the FAQ Custom Post Type
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'faq',
    ));

    $faq  = '<div id="faq-accordion">'; //Open the container
    foreach ( $posts as $post ) { // Generate the markup for each Question
        $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
            $post->post_title,
            wpautop($post->post_content)
        );
    }
    $faq .= '</div>'; //Close the container

    return $faq; //Return the HTML.
});

I have created a custom post type called "faq". The custom post type uses categories. I have created a shortcode [faq] that displays all the faq posts.

What I would like to do is add an attribute to the shortcode that specifies an faq category. So instead of using the shortcode [faq] it would be something like [faq category="travel"] and it would only display the posts from the "travel" category.

I am using the following code for the shortcode at the moment. I just need to add the category attribute somehow:

add_shortcode('faq', function() {

    $posts = get_posts(array(  //Get the FAQ Custom Post Type
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'faq',
    ));

    $faq  = '<div id="faq-accordion">'; //Open the container
    foreach ( $posts as $post ) { // Generate the markup for each Question
        $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
            $post->post_title,
            wpautop($post->post_content)
        );
    }
    $faq .= '</div>'; //Close the container

    return $faq; //Return the HTML.
});
Share Improve this question asked Jul 9, 2013 at 14:59 Ciaran GaffeyCiaran Gaffey 911 gold badge3 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2
add_shortcode( 'faq', 'wpse105856_shortcode_callback' );

function wpse105856_shortcode_callback( $atts ) {
    extract( shortcode_atts( array(
        'category' => ''
    ), $atts ) );

    $args = array(
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'faq'
    ));

    if ( ! empty( $category ) ) {
        $args['category_name'] = $category;
    }

    $posts = get_posts( $args );

    $faq  = '<div id="faq-accordion">'; //Open the container
    foreach ( $posts as $post ) { // Generate the markup for each Question
        $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
            $post->post_title,
            wpautop($post->post_content)
        );
    }
    $faq .= '</div>'; //Close the container

    return $faq; //Return the HTML.
});

Note: Even though the WP_Query parameter is called category_name, it expects to be given a slug.

I have created a custom post type called "whiz_qt_quote". The custom post type uses categories. I have created a shortcode for all the post [quote].

What I would like to display one category post on one page. So instead of using the shortcode [quote], it would be something like [quote category="motivation"] and it would only display the posts from the "motivation" category.

I am using the following code for the shortcode at the moment. I just need to add the category attribute somehow:

if (!defined('ABSPATH')) exit;

function whiz_qt_feed_shortcode( $atts ) {
    extract( shortcode_atts( array( 'limit' => 3, 'type' => 'whiz_qt_quote','category' => ''), $atts ) );
    global $paged;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;  

        $args = query_posts(  array ( 
        'posts_per_page' => $limit, 
        'post_type' => $type, 
        'order' => 'ASC', 
        // 'orderby' =>'menu_order', 
        'paged' => $paged ,
        'category_name' => $category) );
        if ( ! empty( $category ) ) {
        $args['category_name'] = $category;
    }
$list = ' ';   
while ( have_posts() ) { the_post();

    $list .= '<article class="listing-view clearfix">' 
    . '<div class="listing-content">' 
    . '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>' 
    .'<p>' . get_the_excerpt() . '</p>'
    . '</div>'
    . '</article>';
}

return 
'<div class="listings clearfix">' 
. $list 
. '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ) ) . '</div>'
. '<div class="nav-next">' . get_previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) . '</div>'
. '</div>' .
wp_reset_query();

}
add_shortcode( 'quote', 'whiz_qt_feed_shortcode' );
?>`

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745413946a4626659.html

相关推荐

  • categories - Add category attribute to custom shortcode

    I have created a custom post type called "faq". The custom post type uses categories. I have created a shortco

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信