Тип пользовательского сообщения Loop в мета-окне

an _user спросил: 28 марта 2018 в 01:48 в: wordpress

Я создаю шаблон 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; ?>

Странная вещь происходит: все мета-поля на странице дублируются. .

Когда я заменил пользовательский тип сообщения capacity_portfolio с типом сообщения page - он работает. Но когда я заменил его типом post post, он не работал. Я сказал, что когда я стираю этот кусок кода, метабокс работает правильно.

1 ответ

an _user ответил: 28 марта 2018 в 05:31
<?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();
?>

Проблема решена ...