在页面模板中按类别获取自定义帖子类型

too*_*les 0 php wordpress wordpress-theming

我已经创建了一个包含类别和子类别的自定义帖子类型,我需要做的是列出页面模板中给定子类别或类别的帖子标题和图像.

我已经获得了自定义帖子类型中所有列出的项目,但我不确定如何进一步...任何帮助赞赏.

<?php 
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
Run Code Online (Sandbox Code Playgroud)

Fou*_*cks 9

我认为下面的代码可能不起作用,因为它使用了一个不推荐使用的参数(我认为在3.1中不推荐使用caller_get_posts)

认为下面应该解决问题:

$loop = new WP_Query( array( 
    'post_type' => 'portfolio', 
    'cat' => 5, // Whatever the category ID is for your aerial category
    'posts_per_page' => 10,
    'orderby' => 'date', // Purely optional - just for some ordering
    'order' => 'DESC' // Ditto
) );

while ( $loop->have_posts() ) : $loop->the_post(); ?>
Run Code Online (Sandbox Code Playgroud)

还要考虑几件事情(抱歉,如果这会进入'明显'领域!):

1)您的自定义帖子类型是注册使用内置类别还是它正在使用的自定义分类?如果前者然后上面应该工作,如果是后者那么你需要使用'your-taxonomy-name'=>'your-taxonomy-term'来代替cat => 5参数

http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

2)页面上是否还有其他循环?如果是这样,他们将需要

<?php wp_reset_query(); ?>
Run Code Online (Sandbox Code Playgroud)

在它们之后,以便后续循环正常工作

http://codex.wordpress.org/Function_Reference/wp_reset_query