0

我已经开发了一个函数functions.php来根据页面模板或父页面模板(如果它是子页面)将特定 id 添加到正文标记。

function get_body_id() {

    global $post;
    $parents = get_post_ancestors( $post->ID );
    $parent_id = ($parents) ? $parents[count($parents)-1]: $post->ID;

    $temp = get_page_template_slug($parent_id);
    $path = pathinfo($temp);
    $temp = $path['filename'];

    switch ( $temp ):
        case 'page-recipe-landing':
            $id = 'bg-green';
            break;

        case 'page-home':
        default:
            $id = 'bg-pink';
            break;
    endswitch;

    echo "id='".$id."'";
}

这适用于页面及其子项,但对于我的自定义帖子类型事件/新闻和食谱,这显然不会为get_post_ancestors( $post->ID ).

有没有办法改变我的功能以迎合这些自定义帖子类型或将自定义帖子类型分配给特定的“页面”?

感谢您对此的帮助。

4

1 回答 1

0
<?php
    if(empty($parents)) {
        $loop = new WP_Query(array('post_type' => 'NAME_OF_TYPE', 'post_status' => 'publish'));
        while($loop->have_posts()): $loop->the_post();
            the_ID();
        endwhile;
    }
?>

This code should at least output the ID of each post with a custom type of NAME_OF_TYPE. If you wanted to store those in an array and work with them from there, the premise would be pretty much the same. I haven't tested the code, but it should work with custom post types.

于 2012-11-02T17:25:25.207 回答