Blogger Information
Blog 263
fans 3
comment 2
visits 113289
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
wordpress 元数据框获取二级子页面筛选并显示到页面(自写)
福哥的博客
Original
929 people have browsed it

后台元数据框代码

function myplugin_add_meta_box() {
    $screens = array( 'post', 'page','osetin_recipe' );
    add_meta_box(
        'myplugin_sectionid',
        'Best work with',
        'myplugin_meta_box_callback',
        $screens
    );
}

add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

function myplugin_meta_box_callback( $post ) {
    wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
    
         $value = get_post_meta( $post->ID, '_zzurl', true );
         $value = explode("#",$value);
    ?>
    <div style="overflow: hidden">
    
            <?php    

                $args = array(        'post_status' => 'publish',
                          'post_type' => 'page',        
                          'post_parent' => 1525,        
                          'orderby' => 'menu_order',        
                          'order' => 'ASC',        
                          'nopaging' => true,    );   
    $child_pages = get_posts($args); 

                $count = count($child_pages);
        if ( $count > 0 ){
    
        echo "<ul>";
        
        foreach ($child_pages as $post) {        
    setup_postdata($post);      
    $title = $post->post_title;  
    $id = $post->ID; 
        ?>

        <div class="acf-field acf-taxonomy-field" style="width: 33.33%; float: left; padding: 9px 12px 0 0;">
                    <div class="acf-label"><label><?php echo $title ?></label></div>
                    <div class="categorychecklist-holder">
                        <ul class="acf-checkbox-list acf-bl">
                        <?php 
                            
                                $args1 = array(        'post_status' => 'publish',
                          'post_type' => 'page',        
                          'post_parent' => $id,        
                          'orderby' => 'menu_order',        
                          'order' => 'ASC',        
                          'nopaging' => true,    );   
    $child_pages1 = get_posts($args1); 

            
        $count1 = count($child_pages1);
        if ( $count1 > 0 ){
    
        echo "<ul>";
        //static $j = 0;    
        foreach ($child_pages1 as $post1) {        
    setup_postdata($post1);      
    $title1 = $post1->post_title; 
    $id1 = $post1->ID;     
    //$value[$j]==
                        
                            ?>    
                            <li><label><input id="_zzurl[]" name="_zzurl[]" value='<?php echo $id1 ; ?>' <?php if(in_array($id1,$value)){echo "checked";};?> type="checkbox"> <span><?php echo $title1 ?></span></label></li>
                            <?php //$j++;
                            } 
                            }?>
                        </ul>
                    </div>
                </div>
        <?php }
        echo "</ul>";
        }?>
    </div>    
    <?php
}

function myplugin_save_meta_box_data( $post_id ) {
    if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
        return;
    }
    if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }
    } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }
    // if ( ! isset( $_POST['_zzurl'] ) ){
        // return;
    // }
    $my_data=array();
    for($z=0;$z<count($_POST['_zzurl']);$z++){
    $my_data[$z] =  $_POST['_zzurl'][$z];
    
    }
    
    $data = implode("#",$my_data);
    $data = sanitize_text_field( $data );
    if ( ! isset( $_POST['_zzurl'] ) ) {
        $data = "";
    }
    update_post_meta( $post_id, '_zzurl', $data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );

前台页面显示代码

<?php 


 $value = get_post_meta( $post->ID, '_zzurl', true );
 $value = explode("#",$value);
 $countv = count($value);
if( !empty($value[0]) ){

 for ($i=0;$i<$countv;$i++){
           
    $page = get_post($value[$i]); 
                $count = count($page);
        if ( $count > 0 ){


    $title = $page->post_title;  
    $id = $value[$i]; 


?>
 <div>     
        图:<?php echo get_the_post_thumbnail($id); ?>
        链接<a href="<?php echo get_page_link($id); ?>"><?php echo $title; ?></a>
    </div>    
<?php }
            }
 
 };
 ?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post