如何使用ID自定义页面模板进行编辑和删除?

时间:2017-09-16 04:38:14

标签: php wordpress

我有自定义页面模板ViewAll来显示来自wordpress页面上数据库的记录-DisplayAll,现在我想使用id编辑和删除如果我想使用自定义页面模板进行编辑和删除怎么办?如何传递id从页面模板viewall为此,如何导航到wordpress页面进行编辑,我在其中应用自定义页面模板进行编辑?

<?php
/**
 * Template Name:ViewAll
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site may use a
 * different template.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.0

 */

get_header(); ?>

<div class="wrap">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php
            $newdb=new wpdb('root','','wpdemo1','localhost');



            $rows=$newdb->get_results("Select * From wp_student");
            if($rows)
            {
                foreach ($rows as $obj) 
                {
                    //$path = 'admin.php?page=EditStud&id='.$obj->roll;
                    //$url = admin_url($path);
                    //$id=$obj->roll;
                   ?>
                    <table>

                    <tr>
                     <td><?php echo $obj->roll;?></td>
                     <td><?php echo $obj->name;?></td>
                     <td><?php echo $obj->dept;?></td>

                     <td><a href=""> Delete </a></td>
                     <!--<td><a href="<?php  //admin_url( '/wp-admin/admin.php?page=editstud&edit-id=' . $obj->roll ); ?>">Edit</a></td>-->
                    <td><a href="">Edit</a></td>

                    <!--<td><a href="http://localhost/wptest1/index.php/editstud/?id=<?php //echo  $obj->roll;?>"> Edit </a></td>-->
                    </tr>
                    </table>
                    <?php
                }
            }
            /*while ( have_posts() ) : the_post();

                get_template_part( 'template-parts/page/content', 'page' );

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) :
                    comments_template();
                endif;

            endwhile; */// End of the loop.
            ?>

        </main><!-- #main -->
    </div><!-- #primary -->
</div><!-- .wrap -->

<?php get_footer();

1 个答案:

答案 0 :(得分:0)

试试这个。

    <div class="wrap">
        <div id="primary" class="content-area">
            <main id="main" class="site-main" role="main">

                <?php
                $newdb=new wpdb('root','','wpdemo1','localhost');



                $rows=$newdb->get_results("Select * From wp_student");
                if($rows)
                {
                    foreach ($rows as $obj) 
                    {
                        //$path = 'admin.php?page=EditStud&id='.$obj->roll;
                        //$url = admin_url($path);
                        //$id=$obj->roll;
                       ?>
                        <table>

                        <tr>
                         <td><?php echo $obj->roll;?></td>
                         <td><?php echo $obj->name;?></td>
                         <td><?php echo $obj->dept;?></td>

                         <td><a href="#" data-studentid="<?php //echo  $obj->roll;?>"> Delete </a></td>
                         <!--<td><a href="<?php  //admin_url( '/wp-admin/admin.php?page=editstud&edit-id=' . $obj->roll ); ?>">Edit</a></td>-->
                        <td><a href="#">Edit</a></td>

                        <!--<td><a href="http://localhost/wptest1/index.php/editstud/?id=<?php //echo  $obj->roll;?>"> Edit </a></td>-->
                        </tr>
                        </table>
                        <?php
                    }
                }
                /*while ( have_posts() ) : the_post();

                    get_template_part( 'template-parts/page/content', 'page' );

                    // If comments are open or we have at least one comment, load up the comment template.
                    if ( comments_open() || get_comments_number() ) :
                        comments_template();
                    endif;

                endwhile; */// End of the loop.
                ?>

            </main><!-- #main -->
        </div><!-- #primary -->
    </div><!-- .wrap -->

        <script>
            jQuery(document).on('click', '.delete', function () {
                var id = jQuery(this).id;
                jQuery.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: {"action": "delete_student", "student_id": id},
                    success: function (data) {
                       alert("success");
                    }
                });
            });
        </script>

    <?php get_footer();







    **functions.php**


    function delete_student() {
        global $wpdb;
        $table_name = $wpdb->prefix . 'student';

        if(isset($_POST['student_id']) && !empty($_POST['student_id'])){

            $id = $_POST['student_id'];
            $wpdb->delete( $table_name, array( 'roll' => $id ) );

            echo 'success';
            die;
         }  

    }

    add_action('wp_ajax_delete_student', 'delete_student');
    add_action( 'wp_ajax_nopriv_delete_student', 'delete_student');