让自定义帖子类型显示在自定义分类页面上的诀窍?

时间:2010-11-16 作者:Travis Northcutt

我已经为该帖子类型设置了一个自定义帖子类型和一个自定义分类法,但我有一个问题-在自定义分类法permalink页面上,没有显示应该存在的自定义帖子的内容。然而,查看页面源代码,我可以看到有一个div应该包含帖子内容。

对如何显示整个内容有何建议?

编辑:自定义帖子类型UI插件用于创建自定义帖子类型和自定义分类法。

编辑2:原来这是一个论文问题。“设计选项”>“显示选项”>“存档”设置为“仅标题”,这样可以防止显示帖子的内容。

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

默认情况下,标准查询中不包括自定义帖子类型。您需要在分类法页面中手动创建该帖子类型的查询。

SO网友:Norcross

尝试一下,将其添加到函数文件中

add_filter( \'pre_get_posts\', \'include_tax_posts\' );

function include_tax_posts( $query ) {

    if ( is_tax()  && false == $query->query_vars[\'suppress_filters\'] )
        $query->set( \'post_type\', array( \'post\', \'page\', \'YOUR_POST_TYPE\' ) );

    return $query;
}

SO网友:Norcross

好的,做了一些挖掘,似乎在3.1版本中有解决这个问题的方法。在此之前,您可能想尝试使用此插件:http://wordpress.org/extend/plugins/simple-custom-post-type-archives/

另一种选择是使用他们的自定义循环API,并创建一个查询,以便在根据该分类法/帖子类型设置的所有页面上运行。

SO网友:Adam

我不知道这是否有助于您使用插件创建自定义的帖子类型和分类法(从来没有像这样)。这就是我的开发人员从自定义分类中获取帖子,以便在slug匹配时显示在自定义帖子页面上。与您所做的相似,但技术上相反。。。

In Functions.php...

Create custom post type to custom taxonomy relation

    global $post_tax;
    $post_tax = array(
        \'store\' => array(\'post\' => \'stores\', \'tax\' => \'store\'),
        \'state\' => array(\'post\' => \'states\', \'tax\' => \'state\')
    );

Register post types

function post_types_custom_init() {

global $post_tax;

register_post_type($post_tax[\'store\'][\'post\'], array(\'label\' => \'Stores\',\'public\' => true,\'show_ui\' => true,\'_builtin\' => false, \'capability_type\' => \'post\',\'hierarchical\' => true,\'rewrite\' => array(\'slug\' => \'stores\'),\'query_var\' => true,\'supports\' => array(\'title\',\'editor\',\'excerpt\',\'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\')) );

register_post_type($post_tax[\'state\'][\'post\'], array(\'label\' => \'States\',\'public\' => true,\'show_ui\' => true,\'_builtin\' => false, \'capability_type\' => \'post\',\'hierarchical\' => true,\'rewrite\' => array(\'slug\' => \'states\'),\'query_var\' => true,\'supports\' => array(\'title\',\'editor\',\'excerpt\',\'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\')) );

}

Hook into the init action and call create_post_type_taxonomies when it fires

add\\u action(\'init\',\'create\\u beer\\u taxonomies\',0);

Create taxonomies for their corresponding post types

function create_beer_taxonomies() 
{

global $post_tax;

// Add new taxonomy
  $labels = array(
    \'name\' => _x( \'Stores\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Store\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Stores\' ),
    \'all_items\' => __( \'All Stores\' ),
    \'parent_item\' => __( \'Parent Store\' ),
    \'parent_item_colon\' => __( \'Parent Store:\' ),
    \'edit_item\' => __( \'Edit Store\' ), 
    \'update_item\' => __( \'Update Store\' ),
    \'add_new_item\' => __( \'Add New Store\' ),
    \'new_item_name\' => __( \'New Store Name\' ),
  );    

// Make it hierarchical (like categories) and assign to specific post types
  register_taxonomy($post_tax[\'store\'][\'tax\'],
    array(\'post\',$post_tax[\'review\'][\'post\']),
    array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'stores-bp\' ),
  ));

  $labels = array(
    \'name\' => _x( \'States\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'State\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search States\' ),
    \'all_items\' => __( \'All States\' ),
    \'parent_item\' => __( \'Parent State\' ),
    \'parent_item_colon\' => __( \'Parent State:\' ),
    \'edit_item\' => __( \'Edit State\' ), 
    \'update_item\' => __( \'Update State\' ),
    \'add_new_item\' => __( \'Add New State\' ),
    \'new_item_name\' => __( \'New State Name\' ),
  );

  register_taxonomy($post_tax[\'state\'][\'tax\'],
        array(\'post\',$post_tax[\'beer\'][\'review\']),
    array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'states-bp\' ),
  ));

}
In Template/Loop

function meta_loop_beers($queried_tax,$queried_term){
    //$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

    $queried_page = get_query_var(\'paged\');
    $paged = $queried_page ? $queried_page : 1;

    $post_type = \'\';

    global $post_tax;
    foreach($post_tax as $relation){
        if( $relation[\'tax\'] == $queried_tax ){
            $post_type = $relation[\'post\'];
            break;
        }

    }


        $args=array(
          \'post_type\' => \'post\',
          $queried_tax => $queried_term,
          \'post_status\' => \'publish\',
          \'posts_per_page\' => -1,
          \'caller_get_posts\'=> 1
        );

        $my_query = null;
        $my_query = new WP_Query($args);

<PUT LOOP(S) HERE>

        wp_reset_query();


}
我相信这就是一切。您将无法复制粘贴该代码并使其自动为您工作,但它有望为您提供如何使其工作的想法。这是当时最前沿的东西,大约在6个月前完成,我猜在某些地方有更好的实现。不幸的是,该网站还没有直播,所以目前没有什么可显示的。

我们还必须重定向(htaccess)这些slug以用于分类法,因为我们希望那些查找存储(post-type)和存储存档(taxonomy)的slug落在存储的“配置文件页”同时显示两者的同一页面上。

因此,最终的结果是域的归档页面。com/stores bp/walmart将重定向到域。com/stores/walmart。

太令人困惑了!我可以试着在需要的地方澄清。

结束

相关推荐