我已经创建了一个带有类别和子类别的自定义帖子类型,我需要做的是在页面模板中列出给定子类别或类别的帖子标题和图片。
我已经得到了定制帖子类型中列出的所有项目,但我不确定如何进一步。。。感谢您的帮助。
<?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;
?>
创建自定义帖子类型和分类的函数如下所示:-
<?php
// CUSTOM POST TYPE 1
add_action(\'init\', \'portfolio_register\');
function portfolio_register() {
$args = array(
\'label\' => __(\'Portfolio\'),
\'singular_label\' => __(\'Portfolio\'),
\'public\' => true,
\'show_ui\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'rewrite\' => true,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\')
);
register_taxonomy("galleries", array("portfolio"), array(
"hierarchical" => true,
"label" => "Galleries",
"singular_label" => "Galleries",
"rewrite" => true)
);
register_post_type( \'portfolio\' , $args );
}
add_action("admin_init", "admin_init");
add_action(\'save_post\', \'save_portfolio_options\');
add_action(\'save_post\', \'save_portfolio_single_options\');
function admin_init(){
add_meta_box("gallerymeta", "Gallery Options", "portfolio_meta_options", "portfolio", "normal", "low");
add_meta_box("portfoliometa", "Portfolio Item Options", "portfolio_single_meta_options", "portfolio", "side", "low");
}
function portfolio_meta_options(){
global $post;
$custom = get_post_custom($post->ID);
$excerpt = $custom["excerpt"][0];
$info = $custom["info"][0];
$linkto = $custom["linkto"][0];
?>
最合适的回答,由SO网友:Jeff Sebring 整理而成
这是我正在处理的框架中使用的函数的一个版本,示例html替换了另一个包含类似函数的函数。
// Custom Loop
function arrr_custom_loop( $r_type = \'post\', $r_post_num, $r_tax = \'category\', $r_terms = \'featured\' ) {
$args = array(
\'showposts\' => $r_post_num,
\'tax_query\' => array(
array(
\'post_type\' => $r_type,
\'taxonomy\' => $r_tax,
\'field\' => \'slug\',
\'terms\' => array(
$r_terms
),
)
)
);
query_posts( $args );
if (have_posts())
while ( have_posts() ) : the_post();
$more = 0;
?>
<article>
<header class="pagetitle">
<?php if ( is_singular() ) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h2 class="entry"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php } ?>
</header>
<div class="content_wrapper">
<div class="content">
<?php the_content(); ?>
</div>
</div>
<?php if ( comments_open() && ! post_password_required() ) { ?>
<div class="comments_wrapper">
<div class="comments">
<?php comments_template(); ?>
</div>
</div>
<?php } ?>
</article>
<?php endwhile;
wp_reset_query();
}
因此,您只需将函数与创建的参数一起使用:
arrr_custom_loop( \'portfolio\', 10, \'galleries\', \'pirates\' );
SO网友:daveaspinall
我以前用过这个来做更多的事情,但如果删除第二个分类法,它会为您列出给定类别中的所有帖子:
<?php
// set up taxonomies
$tax_one = \'project_category\';
$tax_two = \'brand\';
$post_type = \'project\';
$categories = get_categories( array(
\'type\' => $post_type,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 0,
\'hierarchical\' => 1,
\'taxonomy\' => $tax_one ));
foreach ( $categories as $category ) : // list all categories
echo \'<li><a href="\'.get_term_link( $category->slug, $tax_one ).\'">\'.$category->name.\'</a><ul>\'?>
<?php $terms = get_terms( $tax_two, array( \'hide_empty\' => 0 ) );
foreach ( $terms as $term ) : // list all brands in each category
$myquery[\'tax_query\'] = array(
array(
\'taxonomy\' => $tax_one,
\'terms\' => array($category->slug),
\'field\' => \'slug\',
),
array(
\'taxonomy\' => $tax_two,
\'terms\' => array($term->slug),
\'field\' => \'slug\',
)
);
$the_posts = new WP_Query($myquery);
if ( $the_posts->have_posts() ) : // if there are posts in the current brand and category then display it
echo \'<li><a href="\'.get_term_link( $term->slug, $tax_two ).\'">\'.$term->name.\'</a></li>\';
endif;
endforeach; ?>
<?php echo \'</ul></li>\';
endforeach; ?>
因此,根据您的情况调整它:
<?php
// set up taxonomies
$tax_one = \'category\';
$post_type = \'portfolio\';
$categories = get_categories( array(
\'type\' => $post_type,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 0,
\'hierarchical\' => 1,
\'taxonomy\' => $tax_one ));
foreach ( $categories as $category ) : // list all categories
echo \'<li><a href="\'.get_term_link( $category->slug, $tax_one ).\'">\'.$category->name.\'</a><ul>\'?>
<?php $the_posts = new WP_Query($myquery);
if ( $the_posts->have_posts() ) : // if there are posts in the current category then display it
foreach($the_posts as $post) :
echo \'<li>Your Post stuff here</li>\';
endforeach;
endif;
endforeach; ?>
<?php echo \'</ul></li>\';
endforeach; ?>
我根本没有测试过这个,但希望它能有所帮助!
干杯
戴夫