Can Someone Help me to Correct my shortcode for displaying a category?

Actually the problem is in this code that is, If I put the id directly in “terms” of any post type like; [arts-list 10], then this type of post which contains this id displays the posts on the frontend but if I put something else like terms => 'type' and then I try to display a specific type posts like; [arts-list type=10], no post displays on the frontend, actually in terms I think I have to pass parameter of shortcode but how I do, I can’t debug this, that’s why I asked you. I hope now you understand what is my problem.

function diwp_create_shortcode_arts_post_type(){
    
    $atts = shortcode_atts( array(
        'type' => ' ',
    ), $atts );
 
    $args = array(
                    'post_type'      => 'arts',
                    'posts_per_page' => '10',
                    'post_status' => 'publish',
                    'tax_query'     => array( array(
                                'taxonomy'  => 'Types',
                                'field'     => 'term_id',
                                'terms'     =>  '10',
                            ) )
                 );
 
    $query = new WP_Query($args);
    if($query->have_posts()) :
        while($query->have_posts()) :
            $query->the_post() ;      
        $result .= '<div class="art-item">';
        $result .= '<div class="art-image">' . get_the_post_thumbnail() . '</div>';
        $result .= '<div class="art-name">' . get_the_title() . '</div>';
        $result .= '<div class="art-desc">' . get_the_content() . '</div>';
        $result .= '</div>';
        endwhile;
        wp_reset_postdata();
    endif;    
    return $result;            
}
add_shortcode( 'arts-list', 'diwp_create_shortcode_arts_post_type' );

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think I have to pass parameter of shortcode, but how I do

Yes, that’s correct, and you already are passing the parameter to your shortcode when you do [arts-list type=10] — although you should enclose the parameter value in quotes, i.e. type="10".

But the $atts in your shortcode function is not defined before the shortcode_atts() is called, so the function should be defined as follows:

function diwp_create_shortcode_arts_post_type( $atts )

And because the function is already parsing the parameters which are stored in the $atts array, then you just need to use the parameters in your tax_query (i.e. taxonomy query) clause:

'tax_query' => array(
    array(
        'taxonomy' => 'Types',
        'field'    => 'term_id',
        'terms'    =>  wp_parse_id_list( $atts['type'] ),
    ),
),

Note: In the above example, I used wp_parse_id_list() so that we can pass a list of term IDs, e.g. [arts-list type="10,11,12"]. But more importantly, make sure the IDs point to valid terms in the Types taxonomy.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x