在模板页面中仅显示当前类别

时间:2013-02-08 作者:Lukasz

显示筛选的类别列表页面时出现问题。我希望能够只显示当前类别,而不是“我的类别”中的所有帖子。i、 e.www。。。com/mycategories/category-3/只会在分类页面中显示帖子。这是我所拥有的,但它似乎显示了taxonomy-mycategories.php

Template page:

  <?php 
            $wp_query = null; $wp_query = $temp;
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query();
            $wp_query->query("post_type=business&category_name=" . $_GET["category"] . "&". $catinclude ."&paged=".$paged.\'&showposts=20\'); 
            if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                <?php the_title() ?>
  <?php endwhile; ?>

functions.php:

/******************
//  Business
******************/

function my_custom_post_business() {
    $labels = array(
        \'name\'               => _x( \'business\', \'post type general name\' ),
        \'singular_name\'      => _x( \'business\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New business\' ),
        \'edit_item\'          => __( \'Edit business\' ),
        \'new_item\'           => __( \'New business\' ),
        \'all_items\'          => __( \'All business\' ),
        \'view_item\'          => __( \'View business\' ),
        \'search_items\'       => __( \'Search business\' ),
        \'not_found\'          => __( \'No business found\' ),
        \'not_found_in_trash\' => __( \'No business found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'business\'
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Holds our business and business specific data\',
        \'public\'        => true,
        \'hierarchical\'      => true,
        \'show_ui\'           => true,
        \'show_in_nav_menus\' => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
        \'has_archive\'   => true,
        \'rewrite\' => array(
            \'slug\' => \'business\',
            \'with_front\' => false
            ),
        //\'taxonomies\' => array(\'category\'),
    );
    register_post_type( \'business\', $args );

}
add_action( \'init\', \'my_custom_post_business\' );



/** add categories for custom post type */
add_action( \'init\', \'build_taxonomies\', 0 );
function build_taxonomies() {
    register_taxonomy( \'mycategories\', \'business\', array( \'hierarchical\' => true, \'label\' => \'Business Categories\', \'query_var\' => true, \'rewrite\' => true ) );
}

4 个回复
SO网友:Genxer

使用以下代码:

<?php
if (have_posts()):
    while (have_posts()):
        the_post();
?>


<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<?php
        if ($content != \'no\') {
            the_excerpt();
        }
    endwhile;
endif;
?>

SO网友:Dexter0015

我不确定我是否清楚地理解了你的问题,但如果你需要在分类页面上显示某个类别的儿童帖子,你可以使用get_posts 有了正确的论点,我相信:

在你的类别中{id}。php模板:


    $args = array(
    \'category\'          => get_queried_object_id(), // Current category id
    \'category__in\'      => array( get_queried_object_id() ), // Only posts direct childs of the current category
    \'orderby\'           => \'post_date\',
    \'order\'             => \'DESC\',
);
$cat_posts = get_posts( $args );

SO网友:Brad Dalton

很难确切理解您想要什么,但为什么不能使用pre_get_posts 并自定义conditionals.

add_action( \'pre_get_posts\', \'cpt_portfolio_items\' );
function cpt_portfolio_items( $query ) {

if( $query->is_main_query() && !is_admin() && is_post_type_archive( \'business\' ) ) {
    $query->set( \'posts_per_page\', \'1\' );
    }

}
如果我真的明白你想要什么

add_action( \'pre_get_posts\', \'tax_portfolio_items\' );
function tax_portfolio_items( $query ) {

if( $query->is_main_query() && !is_admin() && is_tax( \'tax-name\' ) ) {
    $query->set( \'posts_per_page\', \'1\' );
    }

}

SO网友:montrealist

你的解释还很不清楚。这就是你想要的吗?假设代码属于该类别。php页面。

<?php

$cat = get_query_var(\'cat\'); // get current category
$yourcat = get_category($cat);

// now get all \'business\' posts that are in the current custom taxonomy 
query_posts( array( \'post_type\' => \'business\', \'mycategories\' => $yourcat->slug ) ); 

if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
<?php endwhile; endif; wp_reset_query(); ?>
Note: 未测试的代码。

结束

相关推荐

Grouping post-types in loop

当用户从下拉列表中选择特定类别时,将显示多个帖子类型的列表。我想用标题中的帖子类型标题分组显示数据。我不确定如何划分为岗位类型组。我花了一点时间寻找,但没有找到我真正需要的东西。我还试图添加<?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> 但这只是重复(显然)。调用循环<div id=\"content\" role=\"main\" style=\"float:right; width:765px