用PHP只显示来自特定类别的文章

wsewodh2  于 2022-10-30  发布在  PHP
关注(0)|答案(3)|浏览(134)

日安
我使用的主题有一个组合。组合目前显示所有列出的类别。我如何改变它只显示某些类别,即设计和图像?
下面是Portfolio模板页面的代码

<?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();?>.

产品组合-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>

任何帮助都将不胜感激
谢谢你

q8l4jmvw

q8l4jmvw1#

编辑:在看到投资组合页面后,您可以根据需要更改query_post命令。

query_posts( 'cat=id' );

其中“id”是类别的编号id。
有关http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category详细信息,请访问www.example.com。

tnkciper

tnkciper2#

若要仅显示一个类别而不是所有类别:
替换为:

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

与此:

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

bmp9r5qi3#

我找到了这个解决方案&它对我很有效。

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

相关问题