0

我是 PHP 和 WordPress 的新手。

我正在使用Types插件来创建我的自定义类型(更多信息在这里)。

这是我的代码:

    <?php
        $args = array('post_type' => 'project-list');
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : 
    ?>
    <span><?php echo $post -> post_title; ?></span>
    <ul class="project-list">
        <?php
            $argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
            $mypostsChild = types_child_posts('project', $argsChild);
            foreach ( $mypostsChild as $postChild ) :
        ?>
        <li>
            <p class="project-cost"><?php echo $postChild -> post_title; ?></p>
            <span class="project-name"><?php echo $postChild -> fields['project-name']; ?></span>
            <hr />
            <p class="project-type"><?php echo $postChild -> fields['project-type']; ?></p>
            <hr />
            <span class="project-location"><?php echo $postChild -> fields['project-location']; ?></span>
        </li>
        <?php endforeach; ?>

    </ul>
    <?php endforeach; ?>

我的期望: 我需要按项目类型加载项目列表。例如,我需要最终输出看起来像这样。

项目组名称-1

项目一

项目-2

项目 3

项目组名称-2

项目一

项目-2

项目 3


我这里有两个问题:

1:我不知道将父帖子“ID”放在哪里,这段代码现在正在为我呈现所有项目列表中的所有项目。我需要为每个项目列表分别加载项目(如上)。

问:我该怎么做?

2:我无法按帖子的发布顺序(这是自定义字段类型)对帖子进行排序。因此,我注意到代码的以下部分无法正常工作。

$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');

问:如何根据自定义字段类型对子帖子进行排序,在我的情况下是“post-order”?

多谢你们!

4

1 回答 1

1

帖子没有任何父母或孩子。帖子是在类别下创建的。您应该在类别下创建一些类别和子类别。IE

类别:项目组名称-1
- 子类别: pro 1
- 子类别: pro 2

CAT:项目组名称-2
- 子类别: pro 1
- 子类别: pro 2

此外,您已经创建了自定义帖子类型。根据您的规范,有很多方法可以检索自定义帖子类型的条款。但是,请尝试以下代码以获得一些意义:

<?php
//list terms in a given taxonomy
$taxonomy = 'project-list';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
 echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '"     title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' .   $tax_term->name.'</a></li>';
}
?>
</ul>

了解有关自定义帖子类型术语分类的更多信息。并阅读此链接,您将获得更多与您的需求相关的代码。这些会帮助你。

仅供参考:页面有父页面或子页面

于 2013-11-10T17:22:17.683 回答