2017-09-16 67 views
1

我有自定義頁面模板ViewAll來顯示記錄從wordpress頁面上的數據庫稱爲-DisplayAll,現在我想編輯和刪除使用ID如何做到這一點,如果我想使用自定義頁面模板進行編輯和刪除?頁面模板viewall爲此,我如何導航到wordpress頁面進行編輯,我在其中應用自定義頁面模板進行編輯?如何使用自定義頁面模板編輯和刪除ID?

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

回答

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