在元框中循环自定义帖子类型

时间:2018-04-21 15:28:09

标签: wordpress custom-post-type meta-boxes

我正在创建一个WordPress模板。我在front-page.php上添加了一个元框。我希望用户可以选择在主页上显示或不显示他的成就。如果他想展示他的成就,他可以在主页中选择他想要在投资组合中显示的成就中显示的成就。我指定capacity_portfolio是自定义帖子类型。

<?php
    function capacity_page_add_custom_box() {
        $screens = ['page', 'capacity_service', 'capacity_portfolio'];
        foreach ($screens as $screen) {
            add_meta_box(
            'capacity_page_custom_box',  // Unique ID
            'Customize this page',  // Box title
            'capacity_page_box_html',  // Content callback, must be of type callable
            $screen    // Post type
            );
        }
    }
add_action('add_meta_boxes', 'capacity_page_add_custom_box');

function capacity_page_box_html($post) {
  require get_parent_theme_file_path( '/template-parts/page-excluded-blog/page-excluded-blog.php' );
?>
    <div class="capacity-metabox-container">
        <table class="form-table capacity-metabox-table">
            <tbody>
                <?php $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;?>
                <?php if ($post_id == get_option( 'page_on_front' )) { ?>
                    <tr>
                        <td class="capacity-metabox-td-description">
                            <label>Display services</label>
                        </td>
                        <td class="capacity-meta-box-style">
                            <input type="radio" name="capacity_meta_box_front_page_services_show" value="yes" id="capacity_meta_box_front_page_services_show" class="capacity_meta_box_front_page_services" <?php if ($capacity_box_front_page_services_show=='yes'){echo checked;}?>> <label for="capacity_meta_box_front_page_services_show" >Yes</label>
                            <div>
                                <input type="radio" name="capacity_meta_box_front_page_services_show" value="no" id="capacity_meta_box_front_page_services_hide" class="capacity_meta_box_front_page_services" <?php if ($capacity_box_front_page_services_show=='no'){echo checked;}?>> <label for="capacity_meta_box_front_page_services_hide">No</label>
                            </div>
                        </td>
                    </tr>
                    <tr class="capacity-metabox-tr-front_page-hidden">
                        <td class="capacity-metabox-td-description">
                            <label>Services to display</label>
                        </td>
                        <td>
                            <ul>
                                <?php $iterator_service_index=1;?>
                                <?php while(true==(get_post_meta($capacity_service_index_id, '_capacity_meta_box_service_index_'.$iterator_service_index, true)) or $iterator_service_index==1){ ?> 
                                    <?php $capacity_box_service_index = (true==(get_post_meta($capacity_service_index_id, '_capacity_meta_box_service_index_'.$iterator_service_index, true))) ? (get_post_meta($capacity_service_index_id, '_capacity_meta_box_service_index_'.$iterator_service_index, true)) : ('');?>
                                    <?php $capacity_meta_box_front_page_service = (true==(get_post_meta($post->ID, '_capacity_meta_box_front_page_service', true))) ? (get_post_meta($post->ID, '_capacity_meta_box_front_page_service', true)) : ('no_array'); ?>
                                    <li> 
                                        <input type="checkbox" name="<?php echo 'capacity_meta_box_front_page_service[_capacity_meta_box_service_index_'.$iterator_service_index.']'; ?>" id="<?php echo 'capacity_meta_box_front_page_service_'.$iterator_service_index; ?>" <?php if ($capacity_meta_box_front_page_service['_capacity_meta_box_service_index_'.$iterator_service_index]=='on' or $capacity_meta_box_front_page_service=='no_array') echo 'checked';  ?>> 
                                        <label for="<?php echo 'capacity_meta_box_front_page_service_'.$iterator_service_index; ?>">
                                            <span class="capacity-meta-box-icon"><span class="fas fa-<?php echo $capacity_box_service_index[icon]; ?>"> </span></span>
                                            <span> <?php echo $capacity_box_service_index[title]; ?> </span>
                                        </label>
                                    </li>
                                    <?php $iterator_service_index++; ?>
                                <?php } ?>
                            </ul>
                        </td>
                    </tr>
                    <tr class="capacity-metabox-tr-front_page-hidden">
                        <td class="capacity-metabox-td-description">
                            <label>Services background color</label>
                        </td>
                        <td>
                            <input type="color" name="capacity_meta_box_front_page_services_background_color" value="<?php echo $capacity_box_front_page_services_background_color; ?>" class="capacity_meta_box_front_page_services_background_color">
                        </td>
                    </tr>
                    <tr>
                        <td class="capacity-metabox-td-description">
                            <label>Display our team</label>
                        </td>
                        <td class="capacity-meta-box-style">
                            <input type="radio" name="capacity_meta_box_front_page_team_show" value="yes" id="capacity_meta_box_front_page_team_show" class="capacity_meta_box_front_page_team" <?php if ($capacity_box_front_page_team_show=='yes'){echo checked;}?>> <label for="capacity_meta_box_front_page_team_show" >Yes</label>
                            <div>
                                <input type="radio" name="capacity_meta_box_front_page_team_show" value="no" id="capacity_meta_box_front_page_team_hide" class="capacity_meta_box_front_page_team" <?php if ($capacity_box_front_page_team_show=='no'){echo checked;}?>> <label for="capacity_meta_box_front_page_team_hide">No</label>
                            </div>
                        </td>
                    </tr>
                    <tr class="capacity-metabox-tr-front_page-team-hidden">
                        <td class="capacity-metabox-td-description">
                            <label>Members to display</label>
                        </td>
                        <td>
                            <ul>
                                <?php $iterator_team_index=1;?>
                                <?php while(true==(get_post_meta($capacity_team_id, '_capacity_meta_box_our_team_'.$iterator_team_index, true))){ ?>    
                                    <?php $capacity_box_member = (true==(get_post_meta($capacity_team_id, '_capacity_meta_box_our_team_'.$iterator_team_index, true))) ? (get_post_meta($capacity_team_id, '_capacity_meta_box_our_team_'.$iterator_team_index, true)) : ('');?>
                                    <?php $capacity_meta_box_front_page_team = (true==(get_post_meta($post->ID, '_capacity_meta_box_front_page_team', true))) ? (get_post_meta($post->ID, '_capacity_meta_box_front_page_team', true)) : ('no_array'); ?>
                                    <li> 
                                        <input type="checkbox" name="<?php echo 'capacity_meta_box_front_page_team[_capacity_meta_box_our_team_'.$iterator_team_index.']'; ?>" id="<?php echo 'capacity_meta_box_front_page_team_'.$iterator_team_index; ?>" <?php if ($capacity_meta_box_front_page_team['_capacity_meta_box_our_team_'.$iterator_team_index]=='on' or $capacity_meta_box_front_page_team=='no_array') echo 'checked';  ?>> 
                                        <label for="<?php echo 'capacity_meta_box_front_page_team_'.$iterator_team_index; ?>" class="capacity-meta-box-front-page-label">
                                            <span > <?php echo $capacity_box_member[name].' '.$capacity_box_member[forname]; ?> </span>
                                        </label>
                                    </li>
                                    <?php $iterator_team_index++; ?>
                                <?php } ?>
                            </ul>
                        </td>
                    </tr>
                    <tr class="capacity-metabox-tr-front_page-team-hidden">
                        <td class="capacity-metabox-td-description">
                            <label>Our team background color</label>
                        </td>
                        <td>
                            <input type="color" name="capacity_meta_box_front_page_team_background_color" value="<?php echo $capacity_box_front_page_team_background_color; ?>" class="capacity_meta_box_front_page_team_background_color">
                        </td>
                    </tr>
                    <tr>
                        <td class="capacity-metabox-td-description">
                            <label>Display Portfolio</label>
                        </td>
                        <td class="capacity-meta-box-style">
                            <input type="radio" name="capacity_meta_box_front_page_portfolio_show" value="yes" id="capacity_meta_box_front_page_portfolio_show" class="capacity_meta_box_front_page_portfolio" <?php if ($capacity_box_front_page_portfolio_show=='yes'){echo checked;}?>> <label for="capacity_meta_box_front_page_portfolio_show" >Yes</label>
                            <div>
                                <input type="radio" name="capacity_meta_box_front_page_portfolio_show" value="no" id="capacity_meta_box_front_page_portfolio_hide" class="capacity_meta_box_front_page_portfolio" <?php if ($capacity_box_front_page_portfolio_show=='no'){echo checked;}?>> <label for="capacity_meta_box_front_page_portfolio_hide">No</label>
                            </div>
                        </td>
                    </tr>
                    <tr class="capacity-metabox-tr-front_page-portfolio-hidden">
                        <td class="capacity-metabox-td-description">
                            <label>Portfolio categories to display</label>
                        </td>
                        <td>
                            <ul>
                                <?php $iterator_portfolio_index=1;?>
                                <?php while(true==(get_post_meta($capacity_portfolio_id, '_capacity_meta_box_portfolio_category_'.$iterator_portfolio_index, true))){ ?>
                                    <?php $capacity_box_category = (true==(get_post_meta($capacity_portfolio_id, '_capacity_meta_box_portfolio_category_'.$iterator_portfolio_index, true))) ? (get_post_meta($capacity_portfolio_id, '_capacity_meta_box_portfolio_category_'.$iterator_portfolio_index, true)) : ('');?>
                                    <?php $capacity_meta_box_front_page_portfolio_categories = (true==(get_post_meta($post->ID, '_capacity_meta_box_front_page_portfolio_categories', true))) ? (get_post_meta($post->ID, '_capacity_meta_box_front_page_portfolio_categories', true)) : ('no_array'); ?>
                                    <li> 
                                        <input type="checkbox" name="<?php echo 'capacity_meta_box_front_page_portfolio_categories[_capacity_meta_box_portfolio_category_'.$iterator_portfolio_index.']'; ?>" id="<?php echo 'capacity_meta_box_front_page_portfolio_categories'.$iterator_portfolio_index; ?>" <?php if ($capacity_meta_box_front_page_portfolio_categories['_capacity_meta_box_portfolio_category_'.$iterator_portfolio_index]=='on' or $capacity_meta_box_front_page_portfolio_categories=='no_array') echo 'checked';  ?>> 
                                        <label for="<?php echo 'capacity_meta_box_front_page_portfolio_categories'.$iterator_portfolio_index; ?>" class="capacity-meta-box-front-page-label">
                                            <span > <?php echo $capacity_box_category; ?> </span>
                                        </label>
                                    </li>
                                    <?php $iterator_portfolio_index++; ?>
                                <?php } ?>
                            </ul>
                        </td>
                    </tr>
                    <tr class="capacity-metabox-tr-front_page-portfolio-hidden">
                        <td class="capacity-metabox-td-description">
                            <label>Portfolio realisations to display</label>
                        </td>
                        <td>
                            <div>
                                <?php $args = array( 'post_type' => 'capacity_portfolio'); ?>
                                <?php $loop = new WP_Query( $args ); ?>
                                <?php if ($loop->have_posts()) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
                                <?php the_title(); ?><br>
                                <?php endwhile; ?>
                                <?php endif; ?>
                            </div>
                        </td>
                    </tr>
                <?php }?>
            </tbody>
        </table>
    </div>
    <?php
    }

    function capacity_save_page_data($post_id) {
        if (array_key_exists('capacity_meta_box_front_page_services_show', $_POST)) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_services_show',
            $_POST['capacity_meta_box_front_page_services_show']
            );
        }
        if (array_key_exists('capacity_meta_box_front_page_team_show', $_POST)) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_team_show',
            $_POST['capacity_meta_box_front_page_team_show']
            );
        }

        if (array_key_exists('capacity_meta_box_front_page_portfolio_show', $_POST)) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_portfolio_show',
            $_POST['capacity_meta_box_front_page_portfolio_show']
            );
        }

        if (array_key_exists('capacity_meta_box_front_page_services_background_color', $_POST)) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_services_background_color',
            $_POST['capacity_meta_box_front_page_services_background_color']
            );
        }

        if (array_key_exists('capacity_meta_box_front_page_team_background_color', $_POST)) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_team_background_color',
            $_POST['capacity_meta_box_front_page_team_background_color']
            );
        }

        if (array_key_exists('capacity_meta_box_front_page_services_show', $_POST) and $_POST['capacity_meta_box_front_page_services_show']=='yes' ) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_service',
            $_POST['capacity_meta_box_front_page_service']
            );
        }

        if (array_key_exists('capacity_meta_box_front_page_team_show', $_POST) and $_POST['capacity_meta_box_front_page_team_show']=='yes' ) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_team',
            $_POST['capacity_meta_box_front_page_team']
            );
        }

        if (array_key_exists('capacity_meta_box_front_page_portfolio_show', $_POST) and $_POST['capacity_meta_box_front_page_portfolio_show']=='yes' ) {
            update_post_meta(
            $post_id,
            '_capacity_meta_box_front_page_portfolio_categories',
            $_POST['capacity_meta_box_front_page_portfolio_categories']
            );
        }
    }

add_action('save_post', 'capacity_save_page_data');

有问题的部分:

<?php $args = array( 'post_type' => 'capacity_portfolio'); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php if ($loop->have_posts()) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title(); ?><br>
<?php endwhile; ?>
<?php endif; ?>

发生了一件奇怪的事情:页面上的所有元框都是重复的。 see figure here

当我使用帖子类型capacity_portfolio替换自定义帖子类型page时 - 它可以正常工作。但是,当我用帖子类型post替换它时,它不起作用。 我说,当我删除这段代码时,metabox正常工作。

1 个答案:

答案 0 :(得分:0)

<?php
    $args = array('post_type' => 'capacity_portfolio', 'orderby' => 'modified'); 
    $pages = get_posts( $args );
    foreach ( $pages as $page ) : setup_postdata( $page ); 

    echo $page->post_title; 

    endforeach; 
    wp_reset_postdata();
?>

问题解决了......