自定义分类列表不返回任何帖子

时间:2014-02-28 作者:asaunders

我遇到了一个使用相同url的自定义分类法和自定义帖子类型的问题,现在已经解决了,但它创建了另一个bug。不显示使用自定义分类法标记的帖子。

我的帖子类型使用的url是/news,然后我有一个分类法,我需要有/news/cars

我可以获取自定义帖子类型的所有帖子,以便在索引模板上输出。

我还可以使用单个分类法页面,但我的查询不会显示使用自定义分类法标记的帖子,例如所有汽车帖子。

以下是我目前掌握的代码:

    function register_custom_post_types() {
    $args = array(
        \'labels\' => array(
            \'name\' => __( \'News\' ),
            \'singular_name\' => __( \'News\' )
        ),
        \'menu_position\' => 5,
        \'public\' => true,
    \'query_var\' => \'news\',
        \'has_archive\' => \'news\',
        \'hierarchical\' => true,
        \'taxonomies\' => array(\'news_categories\'),
    \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'rewrite\' => array(\'with_front\' => false, \'slug\' => \'news/%news_categories%\'),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );
    register_post_type( \'news\', $args);

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

add_action( \'init\', \'create_my_taxonomies\', 0 );

    function create_my_taxonomies() {
        register_taxonomy(
            \'news_categories\',
            \'news\',
            array(
                \'labels\' => array(
                    \'name\' => \'News Categories\',
                    \'add_new_item\' => \'Add New Category\',
                    \'new_item_name\' => "New Category"
                ),
                \'query_var\' => true,
                \'show_ui\' => true,
                \'show_tagcloud\' => false,
                \'hierarchical\' => true,
                \'rewrite\' => array(
                    \'with_front\' => false,
                    \'slug\' => \'news\'
                )
            )
        );
    }

    add_filter(\'post_type_link\', \'news_term_permalink\', 10, 4);
    function news_term_permalink($post_link, $post, $leavename, $sample) {
        if ( false !== strpos( $post_link, \'%news_categories%\' ) ) {
            $glossary_letter = get_the_terms( $post->ID, \'news_categories\' );
            $post_link = str_replace( \'%news_categories%\', array_pop( $glossary_letter )->slug, $post_link );
        }
        return $post_link;

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

答案已从答案中的编辑更改为真实答案

这是我的问题

<?php
                        $taxonomy = (get_post_type() == \'post\' ? \'category\' : (get_post_type() == \'news\' || is_tax(\'news_categories\') ? \'news_categories\' : \'category\'));

                        $tax_terms = get_terms($taxonomy, array(\'hide_empty\'=> false) );
                        global $wp_query;

                        if( isset($_GET[\'s\']) ):
                        $wp_query->is_search = true;
                    endif;

                    // var_dump($activeCategoryName = $wp_query->queried_object);

                        if(is_tax(\'news_categories\')) {
                                $activeCategoryID = $wp_query->queried_object->term_id;
                                $allLink = get_bloginfo(\'url\') . \'/news\';
                                $activeCategoryName = $wp_query->queried_object->name;
                        } elseif(is_category() || is_search()) {
                            $activeCategoryID = $wp_query->queried_object->term_id;
                            $activeCategoryName = $wp_query->queried_object->name;
                            $allLink = get_permalink(get_option(\'page_for_posts\'));
                        } elseif (!is_search() && is_home()) {
                            $activeCategoryName = \'All Blog\';
                        } elseif(!is_search() && is_post_type_archive(\'news\') ) {
                            $activeCategoryName = \'All News\';
                        }

                    ?>
                    <ul>
                        <?php

                        ?>
                        <li><a <?php echo ( !is_search() ? ( is_post_type_archive(\'news\') || is_home() ? \'class="active_category"\' : \'\' ) : \'\' ); ?> title="View all posts in news" href="<?php echo $allLink; ?>">All</a></li>
                        <?php
                            foreach ($tax_terms as $tax_term) {
                                echo \'<li><a \' . ($activeCategoryID == $tax_term->term_id ? \'class="active_category"\' : \'\') . \' title="View all posts in \' . $tax_term->name . \'" href="\' . esc_attr(get_term_link($tax_term, $taxonomy)) . \'">\' . $tax_term->name . \'</a></li>\';
                            }
                        ?>
                    </ul>
                </div>
                <div id=\'search\' class=\'col-lg-3 col-md-3\'>
                    <?php get_search_form(); ?>
                </div>
            </div>
        </div>
    </section>
    <div id=\'blog_listing\'>
        <div class="container">
            <div class="row">

                    <!-- <h1><?php echo wp_title(\'\', false); ?></h1> -->
                    <?php
                        if(!is_search()) {
                    ?>
                        <h1>Currently Viewing <?php echo $activeCategoryName; ?> Articles</h1>
                    <?php
                        } else {
                    ?>
                        <h1>Search Results for <?php echo get_search_query(); ?></h1>
                    <?php
                        }
                    ?>
                    <?php
                        $postType = (is_home() ? \'post\' : (is_post_type_archive(\'news\') ? \'news\' : \'post\' ) );

                        $args = array(
                            \'posts_per_page\' => 6,
                            \'post_type\' => $postType,

                        );
                        $searchQuery = get_search_query();
                        if(!empty($searchQuery)) {
                            $args[\'s\'] = $searchQuery;
                        };

                        if($postType == \'news\') {
                            $args[\'news_categories\'] = $activeCategoryID;
                        } elseif($postType == \'post\') {
                            $args[\'cat\'] = $activeCategoryID;
                        }

                        $wp_query = new WP_Query($args);

                        if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : the_post();
                        $date = get_the_date();
                        $published_posts = $wp_query->found_posts;

结束

相关推荐

Posts & page twice display

我安装了一个新的博客并添加了一些帖子&;第页。使用普通模板/主题,当我更改主题时,页面和帖子可以显示两次。问题在于模板,但我不知道,循环中是否存在任何问题或其他问题?其他信息:主题是Sahifa,一个多用途杂志主题。如果您需要任何文件的代码示例。我会给你看的。