首页 文章

Wordpress自定义帖子类型儿童模板

提问于
浏览
1

我在wordpress中创建了一个名为“电影”的分层自定义帖子类型 . 它是分层的,因此它可以有子元素 .

单击“电影”时,wordpress会自动使用名为“single-films.php”的模板 . 这很好,但我希望在点击其中一个电影的子页面时使用不同的模板 . 例如,电影的孩子可能是“按下” . 当点击该电影的新闻链接时,我希望它使用与single-films.php不同的模板 .

我希望有一些方法可以使用像single-children-films.php这样的模板 . 我有什么想法可以更改分层自定义帖子类型的子模板吗?

2 回答

  • 1

    我想我刚做了同样的事情:

    我有一个名为“产品”的自定义帖子类型,父母是“品牌”,孩子本质上是“品牌的项目”这里是我放在我的单品product.php

    这是获取子帖[item]的第一个查询

    <?php $mypages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'product', 'sort_order' => 'desc' ) );
    //So if it is not a parent
    if (empty($mypages)){ ?>
    //use these styles
    <div class="single_item">stuff is happening</div>
    //If it is a parent post so has children
     <?php ; } else { ?>
    //Then Do these things
    <div class="brandstuff">showing extra stuffs</div>
    <div class="morebrandstuffs">maybe do some extra stuff</div>
    //I wanted to add a list of my child pages to my parent page
    <?php $mypages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'product', 'sort_order' => 'desc' ) );
    
    foreach( $mypages as $page ) {      
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;
    
        $content = apply_filters( 'the_content', $content );
    ?><div class="item">
        <a href="<?php echo get_permalink( $page->ID ); ?>">
        <div class="item_thumb"><?php echo get_the_post_thumbnail($page->ID, 'item_thumb'); ?></div>
        <?php echo $page->post_title; ?></a>
          </div>
    
    <?php
    }   
    ?> <?php } ?>
    
  • 0

    我确信我可以帮忙解决这个问题 . 我只需要知道一件事,孩子总是与父母一样的帖子类型吗?或者按照您在示例中所说的“按”自己的单独自定义帖子类型?

相关问题