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

时间:2011-07-16 作者:toomanyairmiles

我已经创建了一个带有类别和子类别的自定义帖子类型,我需要做的是在页面模板中列出给定子类别或类别的帖子标题和图片。

我已经得到了定制帖子类型中列出的所有项目,但我不确定如何进一步。。。感谢您的帮助。

<?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];
?>

4 个回复
最合适的回答,由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网友:Rajeev Vyas

如果你有/知道类别/子类别slug。。。

只需在args数组中传递它,用于query\\u post。

$args = array( \'post_type\' => \'portfolio\', \'posts_per_page\' => 10 ,\'taxonomy_name\'=>\'slug of category/subcategory\');

query_posts($args); 

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; ?>
我根本没有测试过这个,但希望它能有所帮助!

干杯

戴夫

SO网友:Hobo

Rajeev的答案对我来说很有用。在空白安装中,我执行了以下操作:

创建了一个函数。php只包含portfolio_register 功能,以及add_action 胡克创造了两个画廊——“空中”和“非空中”index.php 仅包含粘贴的第一个代码块。我所做的唯一改变是添加\'galleries\'=>\'aerial\' 到您的$args 变量:

$args = array( \'post_type\' => \'portfolio\', \'posts_per_page\' => 10, \'galleries\'=>\'aerial\' );

仅显示“天线1”和“天线2”。

有没有可能您的代码没有被调用(可能是另一个模板)?

结束

相关推荐

自定义帖子的Category.php模板

我正在尝试使用我的类别。php文件,用于显示具有给定类别的特定自定义帖子类型(例如“公司”)的所有帖子。然而,当我试图通过导航到域来使用它时。com/category/company/category1是由wp\\u list\\u categories()自动生成的链接,不会出现帖子。我使用的代码是:<?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( \'type-company\', g