在表单Submit上提交带有WordPress变量的数组


Submit array with WordPress variables on form submit

我有一个模板,它从数据库中选择wordpress post变量,并将它们加载到我设计的html模板上。我想要的是让用户选择他们喜欢的文章,然后将选择提交给一个模板,该模板将成为一份时事通讯。

到目前为止,我已经有了从数据库中提取数据并将其排列在模板中的代码。代码如下:

        <?php /*
    Template Name: News Selector Template
    */
      //Pull the data
    $posts = $wpdb->get_results
    ("
    SELECT
        p1.*,
        wm2.meta_value 
    FROM
        mp_posts p1 
    LEFT JOIN
        mp_postmeta wm1
        ON (
            wm1.post_id = p1.id
            AND wm1.meta_value IS NOT NULL
            AND wm1.meta_key = '_thumbnail_id'
        )
    LEFT JOIN
        mp_postmeta wm2
        ON (
            wm1.meta_value = wm2.post_id
            AND wm2.meta_key = '_wp_attached_file'
            AND wm2.meta_value IS NOT NULL
        )
    WHERE
        p1.post_status='publish'
        AND p1.post_type='post'
    ORDER BY
        p1.post_date DESC LIMIT 0,30
    ");
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
      <!--
      body {
        font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
        background-color: #42413C;
        margin: 0;
        padding: 0;
        color: #000;
      }
      /* ~~ Element/tag selectors ~~ */
      ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
        padding: 0;
        margin: 0;
      }
      h1, h2, h3, h4, h5, h6, p {
        margin-top: 0;   /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
        padding-right: 15px;
        padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
      }
      a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
        border: none;
      }
      /* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
      a:link {
        color: #42413C;
        text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
      }
      a:visited {
        color: #6E6C64;
        text-decoration: underline;
      }
      a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
        text-decoration: none;
      }
      /* ~~ this fixed width container surrounds the other divs ~~ */
      .container {
        width: 960px;
        background-color: #FFF;
        margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout */
      }
      /* ~~ the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo ~~ */
      .header {
        background-color: #FFF;
      }
      /* ~~ This is the layout information. ~~ 
      1) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.
      */
      .content {
        padding: 10px 0;
      }
      /* ~~ The footer ~~ */
      .footer {
        padding: 10px 0;
        background-color: #000;
        color:#FFF
      }
      /* ~~ miscellaneous float/clear classes ~~ */
      .fltrt {  /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
        float: right;
        margin-left: 8px;
      }
      .fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
        float: left;
        margin-right: 8px;
      }
      .clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
        clear:both;
        height:0;
        font-size: 1px;
        line-height: 0px;
      }
      -->
    </style></head>
    <body>
    <div class="container">
      <div class="header"><a href="example.com"><img src="/example/images/logo_main_300x100px.png" alt="logo" name="example_logo" width="300" height="100" id="logo" style="background-color: #FFF; display:block;" /></a> 
        <!-- end .header --></div>
      <div class="content">
        <h1>example Top 30 Articles</h1>
        <p>Select the articles you want to add to the newsletter</p>
        <table width="960" border="2" summary="A collection of all the articles">
          <caption>
          Article Selection
          </caption>
          <form action="http://samples.net/brands/design/newsletter/"  method="post">
          <tr>
        <th scope="col">Select</th>
        <th scope="col">Title</th>
        <th scope="col">Description</th>
        <th scope="col">Cover</th>
        <th scope="col">link</th>
          </tr>
          <?php
        if (have_posts($posts)) {
          // var_dump($posts); die();
          foreach($posts as $post) {            
            ?>
            <tr>
              <th scope="row"><input type="checkbox" name="article" value="<?php echo $post->ID; ?>" /></th>
              <td><?php echo $post->post_title; ?></td>
              <td><?php echo $post->post_excerpt; ?></td>
              <td><?php echo '<img src="https:sample.net/wp-content/uploads/'.$post->meta_value.'" width="100px" alt="" />'?></td>
              <td><?php echo $post->guid; ?></td>
            </tr>
            <?php 
          }
        }
          ?>
        </table>
        <div></div>
        <p><tr>
        <td>
        <div> <input name="Generate" type="submit" id="submitt" target="_blank" /></div>
        </form>

        </td>
        </tr></p>
        <p>&nbsp;</p>
        <!-- end .content --></div>
      <div class="footer">
        <p>samples Management System</p>
        <!-- end .footer --></div>
      <!-- end .container --></div>
    </body>
    </html>

我想知道的是如何提交一个数组,该数组包含循环中的数据项(post_title、post_excerpt、meta_value、guid)。

使用隐藏输入,并将数组内爆到输入中

<input type="hidden" name="metas" value="<?php echo implode(',', $posts);?>">

然后在后端,您可以使用爆炸来检索值:

$posts = explode(',', $_POST['metas']);