Tiago Silva Tiago Silva - 2 months ago 7
PHP Question

Show last portfolio items by category

I want to show my last items in portfolio filter by one category, I have already added this code that display all my latest portfolio items, I just need to filter that by a category id in my portfolio categories.

<?php if (_sg('Home')->showLatestP()) { ?>
<?php
$args = array();
$args['post_type'] = 'portfolio';
$args['posts_per_page'] = 20;
$args['meta_key'] = '_thumbnail_id';
query_posts($args);
?>

<div class="divider"></div>
<?php if (have_posts()) { ?>
<div class="<?php _sg('Home')->eLatestPType(); ?>">
<?php $i = 0; ?>
<ul class="slides">
<?php while (have_posts()) : the_post(); $i++; ?>
<?php if ($i == 1 OR $i % 5 == 0) echo '<li><ul>'; ?>
<li class="col1-4<?php if ($i % 4 == 0) echo ' omega'; ?>">
<div class="proj-img<?php if (_sg('PortfolioPost', TRUE)->getType(get_the_ID()) == 'video') echo ' proj-video'; ?>">
<?php the_post_thumbnail('sg_portfolio4', array('alt' => get_the_title())); ?>
<?php if (_sg('Home')->getLatestPImgType() == 'large') { ?>
<?php $big = (_sg('PortfolioPost', TRUE)->getType(get_the_ID()) == 'video') ? _sg('PortfolioPost', TRUE)->getVideoUrl(get_the_ID()) : wp_get_attachment_url(get_post_thumbnail_id()); ?>
<a href="<?php echo $big; ?>" class="prettyPhoto zoom" title="<?php the_title(); ?>"></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"></a>
<?php } ?>
</div>
<div class="proj-info"><h4><a class="enter-icn" href="<?php the_permalink(); ?>"><?php echo sg_text_trim(get_the_title(), 30); ?></a></h4>
<?php if (_sg('Home')->getLatestPText() == 'categories') { ?>
<p><?php echo sg_the_tag_list('portfolio_category', ', ', FALSE); ?></p>
<?php } else { ?>
<p><?php echo sg_text_trim(get_the_excerpt(), 30); ?></p>
<?php } ?>
</div>
</li>
<?php if ($i % 4 == 0) echo '</ul></li>'; ?>
<?php endwhile; ?>
<?php if ($i % 4 != 0) echo '</ul></li>'; ?>
</ul>
</div>
<?php } else {
$empty_extras = __('Portfolio is empty', SG_TDN);
echo sg_message($empty_extras);
} ?>
<?php } ?>


Thanks for your help.

Answer

User something like

$args = array(
    'post_type' => 'portfolio',
    'cat' => 'category_id',
    'posts_per_page' => 20,
    'order' => 'DESC'
    );

Or try

$args = array(
    'post_type' => 'portfolio',
    'category_name' => 'slug_category',
    'posts_per_page' => 20,
    'order' => 'DESC'
    );

The order key, determine the order on show your posts. You could find extre reference here

Comments