简体   繁体   English

仅使用 PHP 显示特定类别的投资组合帖子

[英]Display Portfolio Posts only from certain category with PHP

Good Day.再会。

I am using a theme that has a Portfolio.我正在使用一个有投资组合的主题。 The portfolio currently displays all categories listed.投资组合当前显示列出的所有类别。 How can I change it to display only certain categories ie.如何将其更改为仅显示某些类别,即。 Design and Images?设计和图像?

Here is the code of the Portfolio Template Page这是投资组合模板页面的代码

<?php
/*
Template Name: Portfolio page
*/
?>

<?php get_header(); ?>
<?php
global $wp_query;
$post = $wp_query->post;
$gogo_select_portfolio_cat = get_post_meta($post->ID, 'gogo_select_portfolio_cat', true);
$gogo_portfolio_items_order = get_post_meta($post->ID, 'gogo_portfolio_items_order', true);
$gogo_portfolio_text_no_posts = get_post_meta($post->ID, 'gogo_portfolio_text_no_posts', true);
$gogo_select_portfolio_template = get_post_meta($post->ID, 'gogo_select_portfolio_template', true);
$gogo_select_portfolio_sidebar_position = get_post_meta($post->ID, 'gogo_select_portfolio_sidebar_position', true);
?>

<div class="block clearfix">

<header class="box-headline">
    <h4 class="main-headline"><?php the_title(); ?></h4>
</header>

<?php if ($gogo_select_portfolio_template=='portfolio-fourthcol') { 
    load_template(TEMPLATEPATH . '/lib/includes/portfolio/portfolio-4col.php');
} ?>

<?php if ($gogo_select_portfolio_template=='portfolio-threecol') { 
    load_template(TEMPLATEPATH . '/lib/includes/portfolio/portfolio-3col.php');
} ?>

<?php if ($gogo_select_portfolio_template=='portfolio-twocol') { 
    load_template(TEMPLATEPATH . '/lib/includes/portfolio/portfolio-2col.php');
} ?>

</div>

<?php get_footer();?>.

Portfolio-4col.php content: Portfolio-4col.php 内容:

<ul id="filterOptions" class="horizontal" data-option-key="filter">
    <li><a class="selected" href="#filter" data-option-value="*">Show all</a></li>
    <?php
    $categories=  get_categories('taxonomy=portfolio_cat&title_li='); 
    foreach ($categories as $category){ ?>
    <li><a href="#filter" data-option-value=".<?php echo $category->slug;?>" title="Filter by <?php echo $category->name;?>"><?php echo $category->name;?></a></li>
    <?php }?> 
</ul>

<ul class="isotope-holder horizontal four-columns">
<?php rewind_posts(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array( 
                     'post_type' => 'portfolio', 
                     'posts_per_page' => -1,
                     'ignore_sticky_posts' => 1
                     )
              );
$postcount = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); $postcount++;?>

<!--Retrieve posts meta from custom post types -->
<?php
$gogo_portfolio_short_desc = get_post_meta($post->ID, 'gogo_portfolio_short_desc', true);
$gogo_portfolio_video_url = get_post_meta($post->ID, 'gogo_portfolio_video_url', true);
$gogo_portfolio_custom_link = get_post_meta($post->ID, 'gogo_portfolio_custom_link', true);
$gogo_portfolio_display_image_link = get_post_meta($post->ID, 'gogo_portfolio_display_image_link', true);
$gogo_portfolio_display_article_link = get_post_meta($post->ID, 'gogo_portfolio_display_article_link', true);
$gogo_portfolio_display_external_link = get_post_meta($post->ID, 'gogo_portfolio_display_external_link', true); ?>

<li class="element <?php $terms = wp_get_post_terms($post->ID,'portfolio_cat'); foreach ($terms as $term) {  echo ' ' .$term->slug. ' '; } ?>">
<div class="view view-sixth">
<?php if (has_post_thumbnail()) { ?>
<figure>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&amp;w=218&amp;h=200&amp;zc=1&amp;q=100&amp;s=1" alt="'.get_the_title().'" />';?>
</figure>
<?php } ?>

<div class="mask">
        <header class="box-headline">
            <?php echo '<h4><a href="'.get_permalink().'">'; echo ''.get_the_title().''; echo '</a></h4>'; ?>
        </header>
    <div class="content">
        <p><?php echo $gogo_portfolio_short_desc; ?></p>
        <a href="<?php echo get_permalink(); ?>" class="info"><span>Read More &raquo;</span></a>
    </div>
</div>

</div>
</li>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</ul>

<script type="text/javascript">
var $j = jQuery.noConflict();
$j(window).load(function() {

var $jcontainer = $j('ul.isotope-holder');

$jcontainer.isotope({
  itemSelector : '.element',
  resizable: false, // disable normal resizing
  // set columnWidth to a percentage of container width
  masonry: { columnWidth: $jcontainer.width() / 4 }
});

// update columnWidth on window resize
$j(window).smartresize(function(){
  $jcontainer.isotope({
    // update columnWidth to a percentage of container width
    masonry: { columnWidth: $jcontainer.width() / 4}
  });
});

var $joptionSets = $j('#filterOptions'),
    $joptionLinks = $joptionSets.find('a');

$joptionLinks.click(function(){
  var $jthis = $j(this);
  // don't proceed if already selected
  if ( $jthis.hasClass('selected') ) {
    return false;
  }
  var $joptionSet = $jthis.parents('#filterOptions');
  $joptionSet.find('.selected').removeClass('selected');
  $jthis.addClass('selected');

  // make option object dynamically, i.e. { filter: '.my-filter-class' }
  var options = {},
      key = $joptionSet.attr('data-option-key'),
      value = $jthis.attr('data-option-value');
  // parse 'false' as false boolean
  value = value === 'false' ? false : value;
  options[ key ] = value;
  if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
    // changes in layout modes need extra logic
    changeLayoutMode( $jthis, options )
  } else {
    // otherwise, apply new options
    $jcontainer.isotope( options );
  }

  return false;
});
});

</script>

Any help will be appreciated任何帮助将不胜感激

Thank you谢谢

edit: after seeing the portfolio pages.编辑:看到投资组合页面后。 you can change the query_post command according your needs.您可以根据需要更改 query_post 命令。

query_posts( 'cat=id' );

where 'id' is the number id of your category.其中“id”是您的类别的编号 id。

See http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category for more info.有关详细信息,请参阅http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category

To only show one category instead of all of them:只显示一个类别而不是所有类别:

Replace this:替换这个:

$categories =  get_categories('taxonomy=portfolio_cat&title_li=');

with this:有了这个:

$categories[] = get_category('cat=4'); // replace 4 with category ID you're looking for

I found this solution & it worked for me.我找到了这个解决方案,它对我有用。

$args = array(
    'post_type' => 'portfolio',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'portfolio-cats' => 'YOUT PORTFOLIO CATEGORY SLUG',
    'orderby' => 'ID',
    'order' => 'DESC'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM