0
votes

I am creating a WordPress template. I added a meta box on front-page.php. I want the user to have a choice to show or not his achievements on the homepage. If he wants to show his achievements, he can choose which achievement he wants to display in the homepage among the achievements present in the portfolio. I specify that capacity_portfolio is custom post type.

<?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');

The problematic part:

<?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; ?>

A strange thing happens: all the meta boxes on the page are duplicated. see figure here.

When I replaced the custom post type capacity_portfolio with the post type page - it works. But, when I replaced it with the post type post, it did not work. I said, that when I erase this piece of code, the metabox works properly.

1
Boo. The code you posted has nothing to do with your question. Please provide useful, relevant code.random_user_name
I added code...user8388657

1 Answers

0
votes
<?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();
?>

The problem is solved...