Custom Post Type

时间:2015-11-23 作者:DJP2019

我有以下代码,带来了3个职位或项目职位;

但我希望它只显示当前帖子类别中的帖子;

<?php get_header(); ?>
<article class="post">
  <div class="wrapper">
    <?php
while ( have_posts() ) : the_post();
    get_template_part( \'content-portfolio\' );


rainy_projectbottom_nav();
endwhile;
?>
</div>
</article>
<div class="wrapper">
<header class="header">
<h2 class="subtitle">Check out more projects</h2>
</header>
<div class="grid"><?php
while ( have_posts() ) : the_post();

endwhile;

$cats = get_post_meta( $post->ID, \'rainy_multicheck\', false );

$terms_args = array(
\'exclude\' => $cats
);

$terms = get_terms( \'project-category\', $terms_args );

$query_args = array(
\'post_type\' => \'project\',
\'project-category\' => \'Print\',
\'tax_query\' => array(
 array(
    \'taxonomy\' => \'project-category\',
    \'field\' => \'id\',
    \'terms\' => $cats,
    \'post__not_in\' => array($post->ID),
    \'operator\' => \'NOT IN\'
   )
  ),
  \'posts_per_page\' => 3
  );

   $portfolio_query = new WP_Query( $query_args );

  if ( $portfolio_query->have_posts() ) :
  while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
    get_template_part( \'content-related\' );
 endwhile;
  wp_reset_postdata();

  else :
  get_template_part( \'content-none\' );
 endif;
  ?></div> </div></main> <?php get_footer(); ?>

1 个回复
SO网友:s_ha_dum

您可以使用wp_get_post_terms() 拉条款。

$taxonomy = \'project-category\';
$terms = wp_get_post_terms( $post->ID, $taxonomy); 
var_dump($terms);
wp_get_post_terms() 将返回术语数组,因为可能会将多个术语分配给一篇文章。这意味着您可能会得到一组术语,而不仅仅是一个术语,因此您需要选择使用哪个术语。也就是说,第一个条款?最后一学期?第一个是$terms[0]->slug 例如您的“模式”就是这样:

\'post_type\' => \'project\',
\'project-category\' => \'Print\',

You can alternately use a tax_query as found in the Codex:

    array(
        \'taxonomy\' => \'project-category\',
        \'field\'    => \'term_id\',
        \'terms\'    => array( 103, 115, 206 ),
    ),
在您的情况下,它看起来像:

$taxonomy = \'project-category\';
$terms = wp_get_post_terms( $post->ID, $taxonomy,array("fields" => "ids")); 

  $query_args = array(
    \'post_type\' => \'project\',
    \'tax_query\' => array(
        array(
          \'taxonomy\' => \'project-category\',
          \'field\'    => \'term_id\',
          \'terms\'    => $terms,
        )
    ),
    \'posts_per_page\' => 2
);

相关推荐

是否可以取消对特定帖子类型的POSTS_PER_PAGE限制?

我想知道我是否可以取消特定帖子类型的posts\\u per\\u页面限制。在存档中。php页面我显示不同的帖子类型,对于特定的“出版物”帖子类型,我想显示所有帖子。我如何在不影响传统“post”类型的情况下实现这一点?