editing shortcode for custom post type within a plugin

My head is spinning on this code. I'm trying to add the ability to an existing plugin's shortcode, to display

My head is spinning on this code. I'm trying to add the ability to an existing plugin's shortcode, to display only certain custom post types that contain a certain category name.

So far, I have this:

   public function shortcode_litters( $args ) {

    $output = '';
    $atts = shortcode_atts( array(
            'category' => ''
    ), $atts );
    $terms = get_terms('product_category');
            wp_reset_query();

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'order'             => 'asc',
        'numberposts'       => -1,
        'tax_query' => array(
                 array(
                    'taxonomy' => 'product_category',
                    'field' => 'slug',
                    'terms' => $atts,
                ),
             ),

    );

    $litters = get_posts($query_args);

    if($litters) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $litters as $litter ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-litter.php', $litter );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No litter found.</span>";
    }

    return $output;
}

originally it was this:

  public function shortcode_litters( $args ) {

    $output = '';
    $query_from_date = strtotime(date("Y-m-d", strtotime("-3 months")));

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'title',
        'order'             => 'asc',
        'numberposts'       => -1
    );

    if(!empty($args['show']) && in_array($args['show'], array('current', 'past'))) {
        if ($args['show'] == "current") { 
            $meta_query = array(
                'meta_query' => array(
                    array(
                        'key'       => '_litter_mating',
                        'value'     => $query_from_date,
                        'compare'   => '>=',
                        'type'      => 'NUMERIC'
                    )
                )
            );
        }
        if ($args['show'] == "past") {
            $meta_query = array(
                'meta_query' => array(
                    array(
                        'key'       => '_litter_mating',
                        'value'     => $query_from_date,
                        'compare'   => '<',
                        'type'      => 'NUMERIC'
                    )
                )
            );
        }
    }
    else {
        $meta_query = array('meta_query' => '');
    }

    $litters = get_posts($query_args + $meta_query);

    if($litters) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $litters as $litter ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-litter.php', $litter );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No litter found.</span>";
    }

    return $output;
}

Essentially, I'm changing the shortcode from [litters show=""] to [litters category=""] - as I want it to display via category name (not number) in the shortcode. Originally, it has show="" (current or past) and it determines which one it is based on the date. I want to manually be able to determine if it is current or past by selecting a category name via the shortcode.

I've been trying to figure this out all day, and I'm brain dead at this point. Any help would be appreciated!

My head is spinning on this code. I'm trying to add the ability to an existing plugin's shortcode, to display only certain custom post types that contain a certain category name.

So far, I have this:

   public function shortcode_litters( $args ) {

    $output = '';
    $atts = shortcode_atts( array(
            'category' => ''
    ), $atts );
    $terms = get_terms('product_category');
            wp_reset_query();

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'order'             => 'asc',
        'numberposts'       => -1,
        'tax_query' => array(
                 array(
                    'taxonomy' => 'product_category',
                    'field' => 'slug',
                    'terms' => $atts,
                ),
             ),

    );

    $litters = get_posts($query_args);

    if($litters) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $litters as $litter ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-litter.php', $litter );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No litter found.</span>";
    }

    return $output;
}

originally it was this:

  public function shortcode_litters( $args ) {

    $output = '';
    $query_from_date = strtotime(date("Y-m-d", strtotime("-3 months")));

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'title',
        'order'             => 'asc',
        'numberposts'       => -1
    );

    if(!empty($args['show']) && in_array($args['show'], array('current', 'past'))) {
        if ($args['show'] == "current") { 
            $meta_query = array(
                'meta_query' => array(
                    array(
                        'key'       => '_litter_mating',
                        'value'     => $query_from_date,
                        'compare'   => '>=',
                        'type'      => 'NUMERIC'
                    )
                )
            );
        }
        if ($args['show'] == "past") {
            $meta_query = array(
                'meta_query' => array(
                    array(
                        'key'       => '_litter_mating',
                        'value'     => $query_from_date,
                        'compare'   => '<',
                        'type'      => 'NUMERIC'
                    )
                )
            );
        }
    }
    else {
        $meta_query = array('meta_query' => '');
    }

    $litters = get_posts($query_args + $meta_query);

    if($litters) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $litters as $litter ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-litter.php', $litter );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No litter found.</span>";
    }

    return $output;
}

Essentially, I'm changing the shortcode from [litters show=""] to [litters category=""] - as I want it to display via category name (not number) in the shortcode. Originally, it has show="" (current or past) and it determines which one it is based on the date. I want to manually be able to determine if it is current or past by selecting a category name via the shortcode.

I've been trying to figure this out all day, and I'm brain dead at this point. Any help would be appreciated!

Share Improve this question edited Aug 28, 2019 at 8:14 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Aug 28, 2019 at 4:37 AirAir 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In your code $atts is always $atts = [ 'category' => '' ], you cannot use 'terms' => $atts, in query parameters $query_args.

[litters category="first_slug,second_slug"]

You can assign to 'terms' in 'tax_query' parameter a string (single category) or an array (multiple category). $args['category'] is passed to the shortcode function as string, so you need to check if it's one category and no action is required, or a list of slugs separated by commas and they must be converted to an array.

public function shortcode_litters( $args ) {

    $output = '';
    //
    // filter shortcode attributes
    $args = shortcode_atts( 
        array( 'category' => '', 'columns' => '' ), 
        $args 
    );
    if ( strpos( $args['category'], ',' ) !== false ) {
        $terms = explode( ',', $args['category'] );
    } else {
        $terms = $args['category'];
    }

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'order'             => 'asc',
        'numberposts'       => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_category',
                'field'    => 'slug',
                'terms'    => $terms,
            ),
        ),
    );

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

相关推荐

  • editing shortcode for custom post type within a plugin

    My head is spinning on this code. I'm trying to add the ability to an existing plugin's shortcode, to display

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信