admin管理员组

文章数量:1312758

I'm using this code which works really fine to list quite everything I need as Featured image and title, from my custom posts "portfolio".

<?php
    $args  = array(
    'posts_per_page'  => 5000,
    'offset'          => 0, 
    'orderby'         => 'title',
    'order'           => 'ASC',
    'post_type'       => 'portfolio',
    'post_status'     => 'publish',
    'suppress_filters' => true ); 
    $posts = get_posts($args);
    foreach ($posts as $post) :
    ?>
    
    <div class="grid-item">
        <a href="<?php the_permalink();?>">
            <div class="grid-image <?php echo get_the_category()?>">
                <h2 class="grid-title"><?php echo the_title();?></h2>
                <?php echo the_post_thumbnail(array(360,360));?>
            </div>
        </a>
    </div> 

But now, I'd like to list only posts from a choosen category. I tried to add these following terms in args but nothing seems to work.

'category'      => 'category-slug',
'category'      => category-id, 

Any advice on this ? Many Thanks

I'm using this code which works really fine to list quite everything I need as Featured image and title, from my custom posts "portfolio".

<?php
    $args  = array(
    'posts_per_page'  => 5000,
    'offset'          => 0, 
    'orderby'         => 'title',
    'order'           => 'ASC',
    'post_type'       => 'portfolio',
    'post_status'     => 'publish',
    'suppress_filters' => true ); 
    $posts = get_posts($args);
    foreach ($posts as $post) :
    ?>
    
    <div class="grid-item">
        <a href="<?php the_permalink();?>">
            <div class="grid-image <?php echo get_the_category()?>">
                <h2 class="grid-title"><?php echo the_title();?></h2>
                <?php echo the_post_thumbnail(array(360,360));?>
            </div>
        </a>
    </div> 

But now, I'd like to list only posts from a choosen category. I tried to add these following terms in args but nothing seems to work.

'category'      => 'category-slug',
'category'      => category-id, 

Any advice on this ? Many Thanks

Share Improve this question asked Dec 17, 2020 at 22:15 Alexandre TourretAlexandre Tourret 1 2
  • 2 That category parameter is for the default category taxonomy, so does your custom post type support that taxonomy? – Sally CJ Commented Dec 17, 2020 at 23:29
  • Thats a good question Sally, I'm using these custom posts through the portfolio plugin I got with Brooklyn Theme. I can choose catgroies from there, since then I think it does.. I can find the categories I associated with the custom post inside my tables under wp_terms, wp_term_taxonomy, and in wp_term_relationships where they should be related. Is there a way I can get these categories from there ? – Alexandre Tourret Commented Dec 28, 2020 at 20:03
Add a comment  | 

1 Answer 1

Reset to default 1

This is my loop, try using it.

<!-- Post -->
<?php
$paged= (get_query_var('paged' )) ? get_query_var('paged'):1; 
global $query_string;
$myquery = wp_parse_args($query_string);
$myquery = array(
    'paged' => $paged,
    'posts_per_page'=>6,
    'post_type' => 'portfolio'.
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'terms' => array('picture'),
            'field' => 'slug',
        ),
    ),
 );
query_posts($myquery);

while ( have_posts() ) : the_post();
?>

本文标签: categoriesHow to loop custom posttype from (a) specific category(ies)