【问题标题】:Post type category in wordpresswordpress中的帖子类型类别
【发布时间】:2012-07-30 11:28:21
【问题描述】:

我的 wordpress 页面中有 post_type = 投资组合,使用此代码我可以得到每个帖子的类别:

$terms = get_the_terms( $post->ID, 'portfolio_category' );

foreach ( $terms as $term ) {
    $draught_links[] = $term->name;
}

$on_draught = join( ", ", $draught_links );

但它无法正常工作。它显示当前帖子的类别以及之前所有帖子的类别。如何解决?

【问题讨论】:

  • 可能需要更多的上下文来回答这个问题。例如,这段代码是在“The Loop”之内还是在它之外?
  • 如果它在循环中,请考虑在 foreach ( $terms as $term ) 之前初始化 $draught_links =array()
  • @user622018 这似乎是一个很可能的原因。
  • user622018 它正在工作。谢谢;)

标签: wordpress


【解决方案1】:

将此代码用于特定类别

<?php
global $post;
$args = array( 'numberposts' => 5, 'category' => 3 );


$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>

<?php the_title(); ?>
<?php the_content(); ?>

更改帖子数量和类别 ID....

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2017-02-28
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多