web-dev-qa-db-fra.com

Enregistrement de deux catégories à partir de deux listes déroulantes dans le formulaire de publication frontale

Alors, voici le deal:

J'ai deux listes déroulantes de catégories, la première pour les catégories parents et la seconde pour les catégories enfants. Ils forment une chaîne de sorte que lorsque vous sélectionnez la catégorie parent, la deuxième liste déroulante est renseignée avec ses catégories enfants. Voici le code dans le formulaire lui-même:

    <script type="text/javascript">
    $(function()
    {
    $('#main_cat').change(function()
    {
        var $mainCat=$('#main_cat').val();

        // call ajax
        $("#sub_cat").empty();
        $.ajax
        (
            {
                url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                type:'POST',
                data:'action=my_special_ajax_call&main_catid=' + $mainCat,

                success:function(results)
                {
                    //  alert(results);
                    $("#sub_cat").removeAttr("disabled");       
                    $("#sub_cat").append(results);  
                }
            }
        );                                    
    });
    });
    </script>
    <fieldset class="site-url" class="fieldset-first">
    <?php wp_dropdown_categories('show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Main Categories&name=main_cat&taxonomy=category');
    ?>
</fieldset>
<fieldset class="site-url" class="fieldset-first">
<select name="sub_cat" id="sub_cat" disabled="disabled"></select>
</fieldset>

Voici le code dans functions.php:

function implement_ajax() {
if(isset($_POST['main_catid']))
    {
    $categories=  get_categories('child_of='.$_POST['main_catid'].'&hide_empty=0'.'&taxonomy=category'.'&show_count=0');
      foreach ($categories as $cat) {
        $option .= '<option value="'.$cat->term_id.'">';
        $option .= $cat->cat_name;
        $option .= ' ('.$cat->category_count.')';
        $option .= '</option>';
      }
      echo '<option value="-1" selected="selected">Sub Categories</option>'.$option;
    die();
    } // end if
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');

Le problème est que lorsque vous enregistrez les valeurs comme ceci:

'post_category' =>   array($_POST['main_cat']),

Je ne ferai enregistrer que la catégorie parente pour le poste. Comment diable puis-je avoir le parent ET les catégories enfants à la fois sauvés? N'oubliez pas qu'une seule catégorie doit être sélectionnée dans chaque liste déroulante, pas de multiples.

À votre santé!

MISE À JOUR AVEC TOUT _ POST CODE D'AFFICHAGE/D'ÉCONOMIE CI-DESSOUS:

<?php       
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content

    require_once('recaptchalib.php');
        $privatekey="Oh dear I shouldn't have put my super private API key here for all to see!";

        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["my_recapthca_challange_field"],
                                        $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {

    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
        $_SESSION['form_values']['title'] = $_POST['title'];
    } 

    if (isset ($_POST['site-link'])) {
        $siteurl =  $_POST['site-link'];
        $_SESSION['form_values']['site-link'] = $_POST['site-link'];
    }

if (isset ($_POST['first-url'])) {
        $firsturl =  $_POST['first-url'];
        $_SESSION['form_values']['first-url'] = $_POST['first-url'];
    }

if (isset ($_POST['price'])) {
        $price =  $_POST['price'];
        $_SESSION['form_values']['price'] = $_POST['price'];
    }

    if (isset ($_POST['name'])) {
        $devname =  $_POST['name'];
        $_SESSION['form_values']['name'] = $_POST['name'];   
    } 

    if (isset ($_POST['second-url'])) {
        $secondurl =  $_POST['second-url'];
         $_SESSION['form_values']['second-url'] = $_POST['second-url'];
    } 

    if (isset ($_POST['country'])) {
        $devcountry =  $_POST['country'];
         $_SESSION['form_values']['country'] = $_POST['country'];
    }

    if ($_POST['Twitter']=="Twitter Username" || $_POST['Twitter']=="Not Available")  {
        $Twitter =  "Not Available";
         $_SESSION['form_values']['Twitter'] = $Twitter;
    }else  {
        $Twitter = $_POST['Twitter'];
        $_SESSION['form_values']['Twitter'] = $_POST['Twitter'];
    }

    $design =  $_POST['design'];
       if ($design=="yes"){$design = "Yes, this is new!";}
        else {$design="No, this is old!";}


        $related =  $_POST['related'];
       if ($related=="yes"){$related = "Yes, this is related!";}
        else {$related="No, this is not related!";}

$responds =  $_POST['responds'];
       if ($responds=="yes"){$responds = "Yes";}
        else {$responds="No";}

$locale =  $_POST['locale'];
       if ($locale=="yes"){$locale = "Yes";}
        else {$locale="No";}

$widgets =  $_POST['widgets'];
       if ($widgets=="yes"){$widgets = "Yes";}
        else {$widgets="No";}

$skins =  $_POST['skins'];
       if ($skins=="yes"){$skins = "Yes";}
        else {$skins="No";}

$contact =  $_POST['contact'];
       if ($contact=="yes"){$contact = "Yes";}
        else {$contact="No";}

$press =  $_POST['press'];
       if ($press=="yes"){$press = "Yes";}
        else {$press="No";}

$comm =  $_POST['comm'];
       if ($comm=="yes"){$comm = "Yes";}
        else {$comm="No";}

$page =  $_POST['page'];
       if ($page=="yes"){$page = "Yes";}
        else {$page="No";}

$psd =  $_POST['psd'];
       if ($psd=="yes"){$psd = "Yes";}
        else {$psd="No";}

$format =  $_POST['format'];
       if ($format=="yes"){$format = "Yes";}
        else {$format="No";}

$codes =  $_POST['codes'];
       if ($codes=="yes"){$codes = "Yes";}
        else {$codes="No";}


     if ($_FILES['image']['error']==0) { 

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    'post_title'    =>   $title,
'post_category' =>   array($_POST['main_cat']),
    'post_status'   =>   'draft',
    'post_type' =>   'post'
    );

    $pid = wp_insert_post($new_post);
    add_post_meta($pid, 'URL', $siteurl, true);
    add_post_meta($pid, 'Name', $name, true);
    add_post_meta($pid, 'First URL', $firsturl, true);
    add_post_meta($pid, 'Country', $country, true);
    add_post_meta($pid, 'Twitter', $Twitter, true);
    add_post_meta($pid, 'Redesign', $redesign, true);
    add_post_meta($pid, 'Related', $related, true);
add_post_meta($pid, 'Responds', $responds, true);
add_post_meta($pid, 'Locale', $locale, true);
add_post_meta($pid, 'Widgets', $widgets, true);
add_post_meta($pid, 'Skins', $skins, true);
add_post_meta($pid, 'Contact', $contact, true);
add_post_meta($pid, 'Press', $press, true);
add_post_meta($pid, 'Comm', $comm, true);
add_post_meta($pid, 'Page', $page, true);
add_post_meta($pid, 'PSD', $psd, true);
add_post_meta($pid, 'Format', $format, true);
add_post_meta($pid, 'Second URL', $secondurl, true);
add_post_meta($pid, 'Price', $price, true);
add_post_meta($pid, 'Codes', $codes, true);

wp_set_post_categories($post_id, $_POST['main_cat']);
    wp_set_post_tags($post_id, $_POST['post_tags']);


    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);

    }
    $imagedata = simplexml_load_string(get_the_post_thumbnail($pid));
    $post_img = $imagedata->attributes()->src;

    include_once("script/colors.inc.php");
    $image = $post_img;
    $ex=new GetMostCommonColors();
    $ex->image = $image;
    $colors=$ex->Get_Color();
    $how_many=10;
    $colors_key=array_keys($colors);
    $colortag = $colors_key[1];
    for ($i = 2; $i <= $how_many; $i++)
    {
    $colortag = $colortag.",".$colors_key[$i];
    }

    wp_set_post_tags($pid, $colortag);
     $_SESSION['error_message'] = 'Submitted successfully.';
     $_SESSION['form_values'] = array();
     do_action('wp_insert_post', 'wp_insert_post');

    }

} else {
    if (isset ($_POST['title'])) {
        $name =  $_POST['title'];
        $_SESSION['form_values']['title'] = $_POST['title'];
    } 

    if (isset ($_POST['site-link'])) {
        $siteurl =  $_POST['site-link'];
        $_SESSION['form_values']['site-link'] = $_POST['site-link'];
    }

if (isset ($_POST['first-url'])) {
        $firsturl =  $_POST['first-url'];
        $_SESSION['form_values']['first-url'] = $_POST['first-url'];
    }

if (isset ($_POST['price'])) {
        $price =  $_POST['price'];
        $_SESSION['form_values']['price'] = $_POST['price'];
    }

    if (isset ($_POST['name'])) {
        $name =  $_POST['name'];
        $_SESSION['form_values']['name'] = $_POST['name'];

    }

    if (isset ($_POST['second-url'])) {
        $secondurl =  $_POST['dev-url'];
         $_SESSION['form_values']['second-url'] = $_POST['second-url'];
    }

    if (isset ($_POST['country'])) {
        $country =  $_POST['country'];
         $_SESSION['form_values']['country'] = $_POST['country'];
    }

    if ($_POST['Twitter']=="Twitter Username" || $_POST['Twitter']=="Not Available")  {
        $Twitter =  "Not Available";
         $_SESSION['form_values']['Twitter'] = $Twitter;
    }else  {
        $Twitter = $_POST['Twitter'];
        $_SESSION['form_values']['Twitter'] = $_POST['Twitter'];
    }

    $redesign =  $_POST['redesign'];
       if ($redesign=="yes"){$redesign = "Yes, this is new!";}
        else {$redesign="No, this is old!";}


        $related =  $_POST['related'];
       if ($related=="yes"){$related = "Yes, this is related!";}
        else {$related="No, this is not related!";}

$responds =  $_POST['responds'];
       if ($responds=="yes"){$responds = "Yes";}
        else {$responds="No";}

$locale =  $_POST['locale'];
       if ($locale=="yes"){$locale = "Yes";}
        else {$locale="No";}

$widgets =  $_POST['widgets'];
       if ($widgets=="yes"){$widgets = "Yes";}
        else {$widgets="No";}

$skins =  $_POST['skins'];
       if ($skins=="yes"){$skins = "Yes";}
        else {$skins="No";}

$contact =  $_POST['contact'];
       if ($contact=="yes"){$contact = "Yes";}
        else {$contact="No";}

$press =  $_POST['press'];
       if ($press=="yes"){$press = "Yes";}
        else {$press="No";}

$comm =  $_POST['comm'];
       if ($comm=="yes"){$comm = "Yes";}
        else {$comm="No";}

$page =  $_POST['page'];
       if ($page=="yes"){$page = "Yes";}
        else {$page="No";}

$psd =  $_POST['psd'];
       if ($psd=="yes"){$psd = "Yes";}
        else {$psd="No";}

$format =  $_POST['format'];
       if ($format=="yes"){$format = "Yes";}
        else {$format="No";}

$codes =  $_POST['codes'];
       if ($codes=="yes"){$codes = "Yes";}
        else {$codes="No";}
     $_SESSION['error_message'] = 'You will need to enter the correct captcha value. Please try again!';
        }

}

?>
3
Andrew

Pour résoudre ce problème, vous devrez utiliser plusieurs termes plutôt qu'un seul.

alors au lieu de:

$_POST['main_cat']

Utilisation:

array($_POST['main_cat'],$_POST['sub_cat'])

Vous ne devriez cependant pas avoir besoin de sauvegarder main_cat. Assigner sub_cat devrait impliquer que c'est aussi dans le terme parent de subcat

1
Tom J Nowell