การเพิ่มตัวกรอง Taxonomy ให้กับรายการผู้ดูแลระบบสำหรับประเภทโพสต์ที่กำหนดเองหรือไม่


129

ฉันได้สร้างประเภทที่กำหนดเองโพสต์ที่เรียกว่าและเพิ่มอนุกรมวิธานที่กำหนดเองที่เรียกว่า'listing' 'businesses'ฉันต้องการเพิ่มรายการแบบหล่นลงของธุรกิจลงในรายการผู้ดูแลระบบสำหรับรายชื่อ

นี่คือลักษณะการทำงานนี้ในรายการผู้ดูแลระบบสำหรับโพสต์(ฉันต้องการเหมือนกันสำหรับประเภทโพสต์ที่กำหนดเอง):

หมวดหมู่แบบเลื่อนลงในกระทู้

นี่คือรหัสปัจจุบันของฉัน( และนี่คือรหัสเดียวกันในส่วนสำคัญ ):

<?php
/*
Plugin Name: Listing Content Item
Plugin URI:
Description: 
Author: 
Version: 1.0
Author URI: 
*/

class Listing {
  var $meta_fields = array("list-address1","list-address2","list-country","list-province","list-city","list-postcode","list-firstname","list-lastname","list-website","list-mobile","list-phone","list-fax","list-email", "list-profile", "list-distributionrange", "list-distributionarea");

  public function loadStyleScripts() {
    $eventsURL = trailingslashit( WP_PLUGIN_URL ) . trailingslashit( plugin_basename( dirname( __FILE__ ) ) ) . 'css/';
    wp_enqueue_style('listing-style', $eventsURL.'listing.css');
  }

  function Listing() {
    // Register custom post types
    register_post_type('listing', array(
      'labels' => array(
        'name' => __('Listings'), 'singular_name' => __( 'Listing' ),
        'add_new' => __( 'Add Listing' ),
        'add_new_item' => __( 'Add New Listing' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Listing' ),
        'new_item' => __( 'New Listing' ),
        'view' => __( 'View Listing' ),
        'view_item' => __( 'View Listing' ),
        'search_items' => __( 'Search Listings' ),
        'not_found' => __( 'No listings found' ),
        'not_found_in_trash' => __( 'No listings found in Trash' ),
        'parent' => __( 'Parent Listing' ),
      ),
      'singular_label' => __('Listing'),
      'public' => true,
      'show_ui' => true, // UI in admin panel
      '_builtin' => false, // It's a custom post type, not built in
      '_edit_link' => 'post.php?post=%d',
      'capability_type' => 'post',
      'hierarchical' => false,
      'rewrite' => array("slug" => "listings"), // Permalinks
      'query_var' => "listings", // This goes to the WP_Query schema
      'supports' => array('title','editor')
    ));

    add_filter("manage_edit-listing_columns", array(&$this, "edit_columns"));
    add_action("manage_posts_custom_column", array(&$this, "custom_columns"));

    // Register custom taxonomy

    #Businesses
    register_taxonomy("businesses", array("listing"), array(
      "hierarchical" => true, 
      "label" => "Listing Categories", 
      "singular_label" => "Listing Categorie", 
      "rewrite" => true,
    ));

    # Region
    register_taxonomy("regions", array("listing"), array(
      'labels' => array(
        'search_items' =>  __( 'Search Regions' ),
        'popular_items' => __( 'Popular Regions' ),
        'all_items' => __( 'All Regions' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Region' ), 
        'update_item' => __( 'Update Region' ),
        'add_new_item' => __( 'Add New Region' ),
        'new_item_name' => __( 'New Region Name' ),
        'separate_items_with_commas' => __( 'Separate regions with commas' ),
        'add_or_remove_items' => __( 'Add or remove regions' ),
        'choose_from_most_used' => __( 'Choose from the most used regions' ),
      ),
      "hierarchical" => false, 
      "label" => "Listing Regions", 
      "singular_label" => "Listing Region", 
      "rewrite" => true,
    ));

    # Member Organizations
    register_taxonomy("organizations", array("listing"), array(
      'labels' => array(
        'search_items' =>  __( 'Search Member Organizations' ),
        'popular_items' => __( 'Popular Member Organizations' ),
        'all_items' => __( 'All Member Organizations' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Member Organization' ), 
        'update_item' => __( 'Update Member Organization' ),
        'add_new_item' => __( 'Add New Member Organization' ),
        'new_item_name' => __( 'New Member Organization Name' ),
        'separate_items_with_commas' => __( 'Separate member organizations with commas' ),
        'add_or_remove_items' => __( 'Add or remove member organizations' ),
        'choose_from_most_used' => __( 'Choose from the most used member organizations' ),
      ),
      "hierarchical" => false, 
      "label" => "Member Organizations", 
      "singular_label" => "Member Organization", 
      "rewrite" => true,
    ));

    # Retail Products
    register_taxonomy("retails", array("listing"), array(
      'labels' => array(
        'search_items' =>  __( 'Search Retail Products' ),
        'popular_items' => __( 'Popular Retail Products' ),
        'all_items' => __( 'All Retail Products' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Retail Product' ), 
        'update_item' => __( 'Update Retail Product' ),
        'add_new_item' => __( 'Add New Retail Product' ),
        'new_item_name' => __( 'New Retail Product Name' ),
        'separate_items_with_commas' => __( 'Separate retail products with commas' ),
        'add_or_remove_items' => __( 'Add or remove retail products' ),
        'choose_from_most_used' => __( 'Choose from the most used retail products' ),
      ),
      "hierarchical" => false, 
      "label" => "Retail Products", 
      "singular_label" => "Retail Product", 
      "rewrite" => true,
    ));

    # Farming Practices
    register_taxonomy("practices", array("listing"), array(
      'labels' => array(
        'search_items' =>  __( 'Search Farming Practices' ),
        'popular_items' => __( 'Popular Farming Practices' ),
        'all_items' => __( 'All Farming Practices' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Farming Practice' ), 
        'update_item' => __( 'Update Farming Practice' ),
        'add_new_item' => __( 'Add New Farming Practice' ),
        'new_item_name' => __( 'New Farming Practice Name' ),
        'separate_items_with_commas' => __( 'Separate farming practices with commas' ),
        'add_or_remove_items' => __( 'Add or remove farming practices' ),
        'choose_from_most_used' => __( 'Choose from the most used farming practices' ),
      ),
      "hierarchical" => false, 
      "label" => "Farming Practices", 
      "singular_label" => "Farming Practice", 
      "rewrite" => true,
     ));

    # Products 
    register_taxonomy("products", array("listing"), array(
      'labels' => array(
        'search_items' =>  __( 'Search Products' ),
        'popular_items' => __( 'Popular Products' ),
        'all_items' => __( 'All Products' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Product' ), 
        'update_item' => __( 'Update Product' ),
        'add_new_item' => __( 'Add New Product' ),
        'new_item_name' => __( 'New Product Name' ),
        'separate_items_with_commas' => __( 'Separate products with commas' ),
        'add_or_remove_items' => __( 'Add or remove products' ),
        'choose_from_most_used' => __( 'Choose from the most used products' ),
      ),
      "hierarchical" => false, 
      "label" => "Products", 
      "singular_label" => "Product", 
      "rewrite" => true,
    ));


    // Admin interface init
    add_action("admin_init", array(&$this, "admin_init"));
    add_action("template_redirect", array(&$this, 'template_redirect'));

    // Insert post hook
    add_action("wp_insert_post", array(&$this, "wp_insert_post"), 10, 2);
  }

  function edit_columns($columns) {
    $columns = array(
      "cb" => "<input type=\"checkbox\" />",
      "title" => "Business Name",
      "description" => "Description",
      "list-personal" => "Personal Information",
      "list-location" => "Location",
      "list-categorie" => "Categorie",
    );

    return $columns;
  }

  function custom_columns($column) {
    global $post;
    switch ($column) {
      case "description":
        the_excerpt();
        break;
      case "list-personal":
        $custom = get_post_custom();
        if(isset($custom["list-firstname"][0])) echo $custom["list-firstname"][0]."<br />";
        if(isset($custom["list-lastname"][0])) echo $custom["list-lastname"][0]."<br />";
        if(isset($custom["list-email"][0])) echo $custom["list-email"][0]."<br />";
        if(isset($custom["list-website"][0])) echo $custom["list-website"][0]."<br />";
        if(isset($custom["list-phone"][0])) echo $custom["list-phone"][0]."<br />";
        if(isset($custom["list-mobile"][0])) echo $custom["list-mobile"][0]."<br />";
        if(isset($custom["list-fax"][0])) echo $custom["list-fax"][0];
        break;
      case "list-location":
        $custom = get_post_custom();
        if(isset($custom["list-address1"][0])) echo $custom["list-address1"][0]."<br />";
        if(isset($custom["list-address2"][0])) echo $custom["list-address2"][0]."<br />";
        if(isset($custom["list-city"][0])) echo $custom["list-city"][0]."<br />";
        if(isset($custom["list-province"][0])) echo $custom["list-province"][0]."<br />";
        if(isset($custom["list-postcode"][0])) echo $custom["list-postcode"][0]."<br />";
        if(isset($custom["list-country"][0])) echo $custom["list-country"][0]."<br />";
        if(isset($custom["list-profile"][0])) echo $custom["list-profile"][0]."<br />";
        if(isset($custom["list-distributionrange"][0])) echo $custom["list-distributionrange"][0]."<br />";
        if(isset($custom["list-distributionarea"][0])) echo $custom["list-distributionarea"][0];
        break;
      case "list-categorie":
        $speakers = get_the_terms(0, "businesses");
        $speakers_html = array();
        if(is_array($speakers)) {
          foreach ($speakers as $speaker)
          array_push($speakers_html, '<a href="' . get_term_link($speaker->slug, 'businesses') . '">' . $speaker->name . '</a>');
          echo implode($speakers_html, ", ");
        }
        break;
    }
  }

  // Template selection
  function template_redirect() {
    global $wp;
    if (isset($wp->query_vars["post_type"]) && ($wp->query_vars["post_type"] == "listing")) {
      include(STYLESHEETPATH . "/listing.php");
      die();
    }
  }

  // When a post is inserted or updated
  function wp_insert_post($post_id, $post = null) {
    if ($post->post_type == "listing") {
      // Loop through the POST data
      foreach ($this->meta_fields as $key) {
        $value = @$_POST[$key];
        if (empty($value)) {
          delete_post_meta($post_id, $key);
          continue;
        }

        // If value is a string it should be unique
        if (!is_array($value)) {
          // Update meta
          if (!update_post_meta($post_id, $key, $value)) {
            // Or add the meta data
            add_post_meta($post_id, $key, $value);
          }
        }
        else
        {
          // If passed along is an array, we should remove all previous data
          delete_post_meta($post_id, $key);

          // Loop through the array adding new values to the post meta as different entries with the same name
          foreach ($value as $entry)
            add_post_meta($post_id, $key, $entry);
        }
      }
    }
  }

  function admin_init() {
    // Custom meta boxes for the edit listing screen
    add_meta_box("list-pers-meta", "Personal Information", array(&$this, "meta_personal"), "listing", "normal", "low");
    add_meta_box("list-meta", "Location", array(&$this, "meta_location"), "listing", "normal", "low");
  }

  function meta_personal() {
    global $post;
    $custom = get_post_custom($post->ID);
    if(isset($custom["list-firstname"][0])) $first_name = $custom["list-firstname"][0];else $first_name = '';
    if(isset($custom["list-lastname"][0])) $last_name = $custom["list-lastname"][0];else $last_name = '';
    if(isset($custom["list-website"][0])) $website = $custom["list-website"][0];else $website = '';
    if(isset($custom["list-phone"][0])) $phone = $custom["list-phone"][0];else $phone = '';
    if(isset($custom["list-mobile"][0])) $mobile = $custom["list-mobile"][0];else $mobile = '';
    if(isset($custom["list-fax"][0])) $fax = $custom["list-fax"][0];else $fax = '';
    if(isset($custom["list-email"][0])) $email = $custom["list-email"][0];else $email = '';
?>
<div class="personal">
<table border="0" id="personal">
<tr><td class="personal_field"><label>Firstname:</label></td><td class="personal_input"><input name="list-firstname" value="<?php echo $first_name; ?>" /></td></tr>
<tr><td class="personal_field"><label>Lastname:</label></td><td class="personal_input"><input name="list-lastname" value="<?php echo $last_name; ?>" /></td></tr>
<tr><td class="personal_field"><label>Email:</label></td><td class="personal_input"><input name="list-email" value="<?php echo $email; ?>" size="40"/></td></tr>
<tr><td class="personal_field"><label>Website:</label></td><td class="personal_input"><input name="list-website" value="<?php echo $website; ?>" size="40"/></td></tr>
<tr><td class="personal_field"><label>Phone:</label></td><td class="personal_input"><input name="list-phone" value="<?php echo $phone; ?>" /></td></tr>
<tr><td class="personal_field"><label>Mobile:</label></td><td class="personal_input"><input name="list-mobile" value="<?php echo $mobile; ?>" /></td></tr>
<tr><td class="personal_field"><label>Fax:</label></td><td class="personal_input"><input name="list-fax" value="<?php echo $fax; ?>" /></td></tr>
</table>
</div>
     <?php
  }

  // Admin post meta contents
  function meta_location() {
    global $post;
    $custom = get_post_custom($post->ID);
    if(isset($custom["list-address1"])) $address1 = $custom["list-address1"][0];else $address1 = '';
    if(isset($custom["list-address2"])) $address2 = $custom["list-address2"][0];else $address2 = '';
    if(isset($custom["list-country"])) $country = $custom["list-country"][0];else $country = '';
    if(isset($custom["list-province"])) $province = $custom["list-province"][0];else $province = '';
    if(isset($custom["list-city"])) $city = $custom["list-city"][0];else $city = '';
    if(isset($custom["list-postcode"])) $post_code = $custom["list-postcode"][0];else $post_code = '';
    if(isset($custom["list-profile"])) $profile = $custom["list-profile"][0];else $profile = '';
    if(isset($custom["list-distributionrange"])) $distribution_range = $custom["list-distributionrange"][0];else $distribution_range = '';
    if(isset($custom["list-distributionarea"])) $distribution_area = $custom["list-distributionarea"][0];else $ddistribution_area = '';
  ?>
<div class="location">
<table border="0" id="location">
<tr><td class="location_field"><label>Address 1:</label></td><td class="location_input"><input name="list-address1" value="<?php echo $address1; ?>" size="60" /></td></tr>
<tr><td class="location_field"><label>Address 2:</label></td><td class="location_input"><input name="list-address2" value="<?php echo $address2; ?>" size="60" /></td></tr>
<tr><td class="location_field"><label>City:</label></td><td class="location_input"><input name="list-city" value="<?php echo $city; ?>" /></td></tr>
<tr><td class="location_field"><label>Province:</label></td><td class="location_input"><input name="list-province" value="Ontario" readonly /></td></tr>
<tr><td class="location_field"><label>Postal Code:</label></td><td class="location_input"><input name="list-postcode" value="<?php echo $post_code; ?>" /></td></tr>
<tr><td class="location_field"><label>Country:</label></td><td class="location_input"><input name="list-country" value="Canada" readonly /></td></tr>
<tr><td class="location_field"><label>Profile:</label></td><td class="location_input"><input name="list-profile" value="<?php echo $profile; ?>" size="60" /></td></tr>
<tr><td class="location_field"><label>Distribution Range:</label></td><td class="location_input"><input name="list-distributionrange" value="<?php echo $distribution_range; ?>" size="60" /></td></tr>
<tr><td class="location_field"><label>Distribution Area:</label></td><td class="location_input"><input name="list-distributionarea" value="<?php echo $distribution_area; ?>" size="60" /></td></tr>
</table>
</div>
   <?php
  }
}

// Initiate the plugin
add_action("init", "ListingInit");
function ListingInit() { 
  global $listing;
  $listing = new Listing();
  $add_css = $listing->loadStyleScripts();
}

ฉันจะเพิ่มรายชื่อธุรกิจแบบหล่นลงในรายชื่อผู้ดูแลระบบสำหรับรายชื่อได้อย่างไร


8
ขอบคุณสำหรับภาพหน้าจอมันจริงๆจะช่วยให้มีผู้
MikeSchinkel

มีตัวกรอง Taxonomy ผู้ดูแลระบบปลั๊กอินที่สามารถทำงานที่แน่นอนได้
Anh Tran

คำตอบ:


140

UPDATE:ฉันได้รวมคำตอบที่สมบูรณ์ใหม่แล้ว แต่ถึงอย่างนั้นฉันก็ยังคงทิ้งคำตอบดั้งเดิมไว้ที่ด้านล่างซึ่งการอ้างอิงความคิดเห็นสองสามข้อแรก


สวัสดี@ tarasm :

แม้ว่าฉันจะบอกว่ามันไม่ควรยาก แต่ก็มีส่วนเกี่ยวข้องเล็กน้อย แต่ก่อนที่เราจะขุดลงในรหัส ...

ภาพหน้าจอ:

... ลองตรวจสอบภาพหน้าจอสำหรับผลิตภัณฑ์สำเร็จรูป:

หน้ารายการรายชื่อที่ไม่มีการกรอง:

หน้ารายการรายชื่อที่ไม่มีการกรอง
(ที่มา: mikeschinkel.com )

หน้ารายการรายชื่อที่มีการกรอง:

หน้ารายการรายชื่อพร้อมการกรอง
(ที่มา: mikeschinkel.com )

รหัส

ดังนั้นที่นี่เราไป ... ( หมายเหตุ:ฉันใช้รูปแบบเอกพจน์สำหรับชื่ออนุกรมวิธานของbusinessฉันหวังว่าจะตรงกับคุณจากประสบการณ์มากมายกับทั้ง WordPress และการพัฒนาฐานข้อมูลในอดีตฉันเชื่อว่ามันเป็นวิธีที่ดีที่สุดที่จะทำมันด้วยวิธีนี้ .)

ขั้นตอนที่ # 1: restrict_manage_postsเบ็ดการกระทำ

สิ่งแรกที่คุณต้องทำคือขอเกี่ยวกับrestrict_manage_postsการกระทำที่ไม่มีพารามิเตอร์และถูกเรียกจาก/wp-admin/edit.php(ใน v3.0.1 ที่เรียกว่าเป็นสาย 378. ) ซึ่งจะช่วยให้คุณสร้างตัวเลือกแบบหล่นลงที่ตำแหน่งที่เหมาะสมด้านบนรายการ รายการโพสต์

<?php
add_action('restrict_manage_posts','restrict_listings_by_business');
function restrict_listings_by_business() {
    global $typenow;
    global $wp_query;
    if ($typenow=='listing') {
        $taxonomy = 'business';
        $business_taxonomy = get_taxonomy($taxonomy);
        wp_dropdown_categories(array(
            'show_option_all' =>  __("Show All {$business_taxonomy->label}"),
            'taxonomy'        =>  $taxonomy,
            'name'            =>  'business',
            'orderby'         =>  'name',
            'selected'        =>  $wp_query->query['term'],
            'hierarchical'    =>  true,
            'depth'           =>  3,
            'show_count'      =>  true, // Show # listings in parens
            'hide_empty'      =>  true, // Don't show businesses w/o listings
        ));
    }
}

เราเริ่มต้นด้วยการตรวจสอบ$typenowตัวแปรเพื่อให้แน่ใจว่าเราอยู่ในความเป็นจริงบนของpost_type listingหากคุณไม่ทำเช่นนี้คุณจะได้รับรายการแบบหล่นลงนี้สำหรับโพสต์ทุกประเภทซึ่งในบางกรณีเป็นสิ่งที่คุณต้องการ แต่ไม่ใช่ในกรณีนี้

get_taxonomy()ต่อไปเราโหลดข้อมูลเกี่ยวกับอนุกรมวิธานธุรกิจโดยใช้ เราจำเป็นต้องใช้เพื่อดึงข้อมูลฉลากสำหรับอนุกรมวิธาน (เช่น " ธุรกิจ " เราอาจมีรหัสยาก แต่ก็ไม่ดีมากถ้าคุณต้องการทำให้เป็นสากลในภายหลัง) จากนั้นเราเรียกwp_dropdown_categories()อาร์กิวเมนต์ที่เหมาะสมทั้งหมดใน$argsอาร์เรย์เพื่อสร้าง หล่นลง

<?php
return wp_dropdown_categories(array(
    'show_option_all' =>  __("Show All {$business_taxonomy->label}"),
    'taxonomy'        =>  $taxonomy,
    'name'            =>  'business',
    'orderby'         =>  'name',
    'selected'        =>  $wp_query->query['term'],
    'hierarchical'    =>  true,
    'depth'           =>  3,
    'show_count'      =>  true, // Show # listings in parens
    'hide_empty'      =>  true, // Don't show businesses w/o listings
));

แต่อะไรคือข้อโต้แย้งที่เหมาะสม? มาดูกันทีละคน:

  • show_optional_all- ตรงไปตรงมาเป็นสิ่งที่แสดงในรายการแบบหล่นลงในตอนแรกและเมื่อไม่มีการใช้ตัวกรอง ในกรณีของเรามันจะเป็น"แสดงธุรกิจทั้งหมด " แต่เราอาจเรียกมันว่า"รายชื่อธุรกิจทั้งหมด"หรืออะไรก็ตามที่คุณต้องการ

  • taxonomy- อาร์กิวเมนต์นี้บอกฟังก์ชันว่า taxonomy ดึงคำศัพท์มาจากฟังก์ชันใดcategoriesในชื่อ ใน v2.8 และก่อนหน้านี้ WordPress ไม่ได้มี taxonomies ที่กำหนดเอง แต่เมื่อพวกเขาถูกเพิ่มทีมตัดสินใจว่ามันจะง่ายกว่าในการเพิ่มอาร์กิวเมนต์ taxonomy ให้กับฟังก์ชั่นนี้แทนที่จะสร้างฟังก์ชันอื่นที่มีชื่ออื่น

  • name- อาร์กิวเมนต์นี้ช่วยให้คุณสามารถระบุค่าที่ WordPress พร้อมใช้สำหรับnameแอตทริบิวต์ขององค์ประกอบ <select> ที่สร้างขึ้นสำหรับการเลื่อนลง ในกรณีที่ไม่ชัดเจนนี่ก็เป็นค่าที่จะใช้ใน URL เมื่อทำการกรอง

  • orderby- อาร์กิวเมนต์นี้บอก WordPress ถึงวิธีการเรียงลำดับผลลัพธ์ตามตัวอักษร ในกรณีของเราเราระบุให้สั่งซื้อnameเงื่อนไขใน taxonomy เช่นชื่อธุรกิจในกรณีนี้

  • selected- อาร์กิวเมนต์นี้จำเป็นเพื่อให้ดร็อปดาวน์สามารถแสดงตัวกรองปัจจุบันในดร็อปดาวน์ มันควรจะเป็นterm_idจากระยะเวลาอนุกรมวิธานที่เลือก ในกรณีของเรามันอาจจะเป็นterm_idจาก"ธุรกิจ # 2" เราจะได้รับค่านี้ที่ไหน จากตัวแปรทั่วโลกของ WordPress $wp_query; มันมีคุณสมบัติqueryที่มีอาเรย์ของพารามิเตอร์ URL ทั้งหมดและค่าของมัน (ยกเว้นบางปลั๊กอินที่ปรับเปลี่ยนได้แล้วแน่นอน) กำหนดว่า WordPress ประมวลผลสิ่งต่าง ๆ อย่างไรจะมีtermพารามิเตอร์ URL ส่งผ่านไปยัง URL เมื่อผู้ใช้คลิกตัวกรอง หากผู้ใช้เลือกคำที่ถูกต้อง (เช่นหนึ่งในธุรกิจที่อยู่ในรายการ)

  • hierarchical- โดยการตั้งค่านี้ให้trueคุณบอกฟังก์ชั่นให้เคารพธรรมชาติของลำดับชั้นของอนุกรมวิธานและแสดงไว้ในมุมมองแบบต้นไม้ถ้าคำว่า (ธุรกิจ) ในความเป็นจริงมีลูก สำหรับภาพหน้าจอเพื่อดูว่าลักษณะนี้เป็นอย่างไรให้ดูด้านล่าง

  • depth- อาร์กิวเมนต์นี้ทำงานร่วมกับhierarchicalอาร์กิวเมนต์เพื่อกำหนดจำนวนระดับที่ควรใช้ฟังก์ชันในการแสดงลูก

  • show_count- หากtrueอาร์กิวเมนต์นี้จะแสดงจำนวนการโพสต์ภายในวงเล็บทางด้านซ้ายของชื่อคำภายในรายการแบบหล่นลง ในกรณีนี้มันจะแสดงจำนวนรายชื่อที่เกี่ยวข้องกับธุรกิจ สำหรับภาพหน้าจอเพื่อดูว่าลักษณะนี้เป็นอย่างไรให้ดูด้านล่าง

  • hide_empty- ในที่สุดหากมีคำศัพท์ในอนุกรมวิธานที่ไม่เกี่ยวข้องกับการโพสต์ (เช่นธุรกิจที่ไม่เกี่ยวข้องกับรายชื่อ) ดังนั้นการตั้งค่านี้trueจะไม่รวมอยู่ในรายการแบบหล่นลง

Taxonomy Drop Down ควรเป็นลำดับชั้นและจำนวน
(ที่มา: mikeschinkel.com )

ขั้นตอนที่ # 2: parse_queryเบ็ดกรอง

ต่อไปเราจะเรียกความสนใจของเราไปที่parse_queryhook filter ซึ่งมีหนึ่งพารามิเตอร์ ( $query) และถูกเรียกจาก/wp-includes/query.php(ใน v3.0.1 การโทรนั้นอยู่ที่บรรทัด 1549) มันถูกเรียกเมื่อ WordPress เสร็จสิ้นการตรวจสอบ URL และการตั้งค่าที่เหมาะสมทั้งหมดในปัจจุบัน ใช้งาน$wp_queryรวมถึงสิ่งที่ชอบ$wp_query->is_homeและ$wp_query->is_authorอื่น ๆ

หลังจากparse_queryเบ็ดกรองวิ่ง WordPress จะเรียกและโหลดรายชื่อของโพสต์ขึ้นอยู่กับสิ่งที่ระบุไว้ในการใช้งานอยู่ในปัจจุบันget_posts() $wp_queryดังนั้นจึงparse_queryเป็นสถานที่ที่ดีในการทำให้ WordPress เปลี่ยนใจที่จะโหลดโพสต์

ในกรณีที่คุณใช้งานเราต้องการให้ WordPress กรองตามธุรกิจที่เลือก กล่าวคือแสดงเฉพาะรายชื่อผู้ที่เกี่ยวข้องกับธุรกิจที่เลือก (ฉันจะบอกว่า"... เฉพาะรายชื่อที่ได้รับการ" จัดหมวดหมู่ "โดยธุรกิจที่เลือก"แต่นั่นไม่ถูกต้องทางเทคนิคcategoryเป็นอนุกรมวิธานของตัวเอง ด้วยbusinessยกเว้นที่categoryสร้างไว้ใน WordPress และbusinessเป็นแบบกำหนดเอง แต่สำหรับผู้ที่คุ้นเคยกับการจัดหมวดหมู่โพสต์สิ่งนี้อาจช่วยให้คุณเข้าใจในขณะที่มันทำงานเกือบเหมือนกัน แต่ฉันพูดนอกเรื่อง ... )

เมื่อถึงรหัส สิ่งแรกที่เราทำคือการคว้าอ้างอิงถึงใช้งานในปัจจุบัน$wp_query's query_varsเพื่อที่จะสะดวกมากขึ้นในการทำงานด้วยเช่นเดียวกับวิธีทำภายใน WordPress' ของตัวเองparse_query()ฟังก์ชั่น ซึ่งแตกต่างจาก$wp_query->queryที่ใช้ในการมิเรอร์พารามิเตอร์ที่ส่งผ่าน URL $wp_query->query_varsอาเรย์ที่ใช้ในการควบคุมการค้นหาที่ WordPress ทำงานและคาดว่าจะมีการปรับเปลี่ยน ดังนั้นหากคุณต้องการแก้ไขหนึ่งนั่นเป็นอย่างน้อย (อย่างน้อยฉันคิดว่านั่นคือความแตกต่างระหว่างสอง; ถ้าใครรู้อย่างอื่นโปรดแจ้งให้เราทราบเพื่อให้ฉันสามารถอัปเดตนี้!)

<?php
add_filter('parse_query','convert_business_id_to_taxonomy_term_in_query');
function convert_business_id_to_taxonomy_term_in_query($query) {
    global $pagenow;
    $qv = &$query->query_vars;
    if ($pagenow=='edit.php' &&
            isset($qv['taxonomy']) && $qv['taxonomy']=='business' &&
            isset($qv['term']) && is_numeric($qv['term'])) {
        $term = get_term_by('id',$qv['term'],'business');
        $qv['term'] = $term->slug;
    }
}

ต่อไปเราจะทดสอบ$pagenowเพื่อให้แน่ใจว่าเรากำลังโหลด WordPress จากเส้นทาง URL /wp-admin/edit.phpอย่างแน่นอน เราทำเช่นนี้เพื่อหลีกเลี่ยงการทำให้เสียการค้นหาในหน้าอื่นโดยไม่ตั้งใจ นอกจากนี้เรายังตรวจสอบเพื่อให้แน่ใจว่าเรามีทั้งbusinessเป็นtaxonomyองค์ประกอบและtermองค์ประกอบเกินไป (หมายเหตุtaxonomyและtermเป็นคู่; พวกมันถูกใช้ร่วมกันเพื่ออนุญาตให้ทำการสืบค้นคำศัพท์อนุกรมวิธาน; ต้องมีทั้งคู่หรือ WordPress ไม่รู้ว่าอนุกรมวิธานใดที่จะตรวจสอบ)

คุณอาจสงสัยว่าbusinessเปิดขึ้นในtaxonomyองค์ประกอบของquery_varsอาร์เรย์ สิ่งที่เราเขียนลงในparse_queryเบ็ดของเราทำให้เกิดเวทมนต์ภายในของ WordPress ที่รออยู่เมื่อคุณลงทะเบียนbusinesstaxonomy โดยการตั้งค่าquery_varให้เป็นจริง ( register_taxonomy()คัดลอกชื่อ taxonomy เป็นของมันquery_varคุณสามารถเปลี่ยนแน่นอน แต่ถ้าคุณไม่มีความขัดแย้ง วิธีที่ดีที่สุดคือติดกัน):

<?php
add_action('init','register_business_taxonomy');
    function register_business_taxonomy() {
        register_taxonomy('business',array('listing'),array(
        'label' => 'Businesses',
        'public'=>true,
        'hierarchical'=>true,
        'show_ui'=>true,
        'query_var'=>true
    ));
}

ตอนนี้ WordPress '$ wp_query ถูกเขียนขึ้นเพื่อใช้กระสุนสำหรับการสืบค้นอนุกรมวิธานที่กรองแล้วไม่ใช่รหัสคำศัพท์อนุกรมวิธาน สำหรับกรณีการใช้งานสิ่งที่เราต้องทำให้แบบสอบถามการกรองของเราเป็นจริง:

taxonomy: ธุรกิจ

term: business-1 (เช่นslug)

ไม่ใช่สิ่งเหล่านี้:

taxonomy: ธุรกิจ

term: 27 (เช่นterm_id)

ที่น่าสนใจและน่าเสียดายที่หล่นลงที่เกิดจากการwp_dropdown_categories()ตั้งค่า<option>ของvalueแอตทริบิวต์ของคำ (ธุรกิจ / ') term_id, ไม่ได้slugคำว่า ดังนั้นเราต้องแปลง$wp_query->query_vars['term']จากตัวเลขterm_idเป็นสตริงslugดังต่อไปนี้ในตัวอย่างข้อมูลจากด้านบน (หมายเหตุนี่ไม่ใช่วิธีที่มีประสิทธิภาพมากที่สุดในการสืบค้นฐานข้อมูล แต่จนกระทั่ง WordPress เพิ่มการสนับสนุนสำหรับ term_ids เป็นแบบสอบถามที่ดีที่สุดที่เราทำได้!) :

<?php
$term = get_term_by('id',$qv['term'],'business');
$qv['term'] = $term->slug;

และนั่นมัน! ด้วยฟังก์ชันทั้งสองนี้คุณจะได้รับการกรองตามที่คุณต้องการ

แต่รอมีอีกมาก! :-)

ฉันไปข้างหน้าและเพิ่มคอลัมน์"ธุรกิจ"ในรายการของคุณเพราะฉันรู้ว่ามันจะเป็นคำถามต่อไปของคุณ โดยไม่ต้องมีคอลัมน์สำหรับสิ่งที่คุณกรองมันอาจสร้างความสับสนให้กับผู้ใช้ (ฉันต้องดิ้นรนกับมันด้วยตัวเองและฉันก็เป็น coder!) แน่นอนคุณสามารถเห็นคอลัมน์"ธุรกิจ"ในภาพหน้าจอก่อนหน้าด้านบน

ขั้นตอนที่ # 3: manage_posts_columnsเบ็ดกรอง

ในการเพิ่มคอลัมน์ในรายการโพสต์จะเรียกใช้ hooks เพิ่มอีกสอง (2) อัน คนแรกคือmanage_posts_columnsหรือรุ่นเฉพาะของโพสต์manage_listing_posts_columnsที่ฉันเรียกว่าแทน ยอมรับหนึ่งพารามิเตอร์ ( posts_columns) และถูกเรียกจาก/wp-admin/includes/template.php(ใน v3.0.1 ว่าการโทรนั้นอยู่ที่บรรทัด 623):

<?php
add_action('manage_listing_posts_columns', 'add_businesses_column_to_listing_list');
function add_businesses_column_to_listing_list( $posts_columns ) {
    if (!isset($posts_columns['author'])) {
        $new_posts_columns = $posts_columns;
    } else {
        $new_posts_columns = array();
        $index = 0;
        foreach($posts_columns as $key => $posts_column) {
            if ($key=='author')
                $new_posts_columns['businesses'] = null;
            $new_posts_columns[$key] = $posts_column;
        }
    }
    $new_posts_columns['businesses'] = 'Businesses';
    return $new_posts_columns;
}

manage_posts_columnsฟังก์ชัน hook ของคุณได้รับอาร์เรย์ของคอลัมน์ที่ค่าเป็นส่วนหัวของคอลัมน์ที่แสดงและที่สำคัญคือตัวระบุคอลัมน์ภายใน ตัวระบุคอลัมน์มาตรฐานสามารถรวมสิ่งเหล่านี้และอื่น ๆ : 'cb', 'title' 'author',, ``' วันที่ '' ฯลฯ

'cb', คือcheckboxคอลัมน์และทั้งคู่'title'และ'date'อ้างถึงpost_titleและpost_dateจากwp_postsตารางตามลำดับ 'author'แน่นอนว่าเป็นpost_authorฟิลด์หลังจากชื่อผู้เขียนจะถูกดึงออกมาจากwp_usersตาราง

สกรีนช็อตของคอลัมน์โพสต์ 'cb' เป็นช่องทำเครื่องหมาย
(ที่มา: mikeschinkel.com )

สำหรับmanage_posts_columnsเบ็ดเราเพียงต้องการที่จะแทรกคอลัมน์ของเรา businessesลงใน$posts_columnsอาร์เรย์ก่อน'author'สมมติว่าปลั๊กอินอื่น ๆ ยังไม่ได้ลบออกauthorจากรายการ!

$new_posts_columns['businesses'] = 'Businesses';

( โปรดทราบว่าฉันเขียนadd_businesses_column_to_listing_list()มันเกิดขึ้นกับฉันว่า PHP ต้องมีวิธีที่ง่ายกว่าในการแทรกค่าลงในอาเรย์แบบสัมพันธ์ในลำดับที่ถูกต้อง!? หรืออย่างน้อยก็ต้องมีฟังก์ชั่นในคอร์ WordPress ด้วย ให้ฉันลงเพื่อที่ฉันจะไปกับสิ่งที่ทำงานถ้าใครมีทางเลือกที่แนะนำใด ๆ ฉันจะหูทั้งหมดและชื่นชมล่วงหน้า!)

ซึ่งในที่สุดก็นำเราไปที่ ...

ขั้นตอนที่ # 4: manage_posts_custom_columnเบ็ดการกระทำ

สิ่งที่สองของสอง (2) ที่เราต้องทำเพื่อให้ธุรกิจของเราแสดงในคอลัมน์คือการส่งออกชื่อของแต่ละธุรกิจที่เกี่ยวข้องโดยใช้ manage_posts_custom_columnaction hook hook นี้ยอมรับพารามิเตอร์สอง (2) ตัว ( column_idและpost_id) และถูกเรียกจาก/wp-admin/includes/template.php(ใน v3.0.1 ที่การเรียกใช้อยู่ที่บรรทัดที่ 1459):

<?php
add_action('manage_posts_custom_column', 'show_businesses_column_for_listing_list',10,2);
function show_businesses_column_for_listing_list( $column_id,$post_id ) {
    global $typenow;
    if ($typenow=='listing') {
        $taxonomy = 'business';
        switch ($column_name) {
        case 'businesses':
            $businesses = get_the_terms($post_id,$taxonomy);
            if (is_array($businesses)) {
                foreach($businesses as $key => $business) {
                    $edit_link = get_term_link($business,$taxonomy);
                    $businesses[$key] = '<a href="'.$edit_link.'">' . $business->name . '</a>';
                }
                //echo implode("<br/>",$businesses);
                echo implode(' | ',$businesses);
            }
            break;
        }
    }
}

เบ็ดนี้ถูกเรียกสำหรับแต่ละคอลัมน์สำหรับแต่ละแถว (/ ธุรกิจ) ครั้งแรกที่เราตรวจสอบว่าเรากำลังทำงานแน่นอนมีเพียงlistingโพสต์ที่กำหนดเองชนิดและจากนั้นเราจะใช้คำสั่งในการทดสอบกับswitch column_idฉันเลือกswitchเพราะตะขอนี้มักจะใช้ในการสร้างผลลัพธ์สำหรับคอลัมน์ที่แตกต่างกันโดยเฉพาะอย่างยิ่งถ้าเราใช้ฟังก์ชั่นเดียวสำหรับโพสต์ที่แตกต่างกันหลายประเภทซึ่งอาจมีลักษณะเช่นนี้:

<?php
add_action('manage_posts_custom_column', 'my_manage_posts_custom_column',10,2);
function my_manage_posts_custom_column( $column_id,$post_id ) {
    global $typenow;
    switch ("{$typenow}:{$column_id}") {
    case 'listing:business':
        echo '...whatever...';
        break;
    case 'listing:property':
        echo '...whatever...';
        break;
    case 'agent:listing':
        echo '...whatever...';
        break;
    }
}

การตรวจสอบกรณีการใช้งานของเราเพียงเล็กน้อยคุณจะเห็นget_the_terms()ฟังก์ชั่นที่จะส่งคืนรายการเงื่อนไขสำหรับอนุกรมวิธานนี้ (เช่นธุรกิจสำหรับรายชื่อนี้) ที่นี่จะได้รับความคิดเห็นสำหรับหน้าส่วนหน้าของคำว่าปกติจะแสดงรายการกระทู้ที่เกี่ยวข้อง ด้วยคำศัพท์ แต่แน่นอนอาจแตกต่างกันไปขึ้นอยู่กับธีมและ / หรือปลั๊กอินที่ติดตั้ง

เราใช้ลิงก์เชื่อมโยงหลายมิติเพื่อเชื่อมโยงคำศัพท์เพียงเพราะฉันชอบเชื่อมโยงหลายมิติ จากนั้นเราจะรวมคำศัพท์ที่เชื่อมโยงหลายมิติทั้งหมด (/ ธุรกิจ) เข้าด้วยกันโดยคั่นด้วย|อักขระไพพ์ (' ') และส่งออกไปยังบัฟเฟอร์ PHP ซึ่งส่งไปยังเบราว์เซอร์ / ไคลเอ็นต์ HTTP ของผู้ใช้:

<?php
$businesses = get_the_terms($post_id,$taxonomy);
if (is_array($businesses)) {
    foreach($businesses as $key => $business) {
        $edit_link = get_term_link($business,$taxonomy);
        $businesses[$key] = '<a href="'.$edit_link.'">' . $business->name . '</a>';
    }
    //echo implode("<br/>",$businesses);
    echo implode(' | ',$businesses);
}

ตอนนี้เรากำลังทำในที่สุด

สรุป

ดังนั้นโดยสรุปคุณต้องใช้ hooks สี่ (4) ต่อไปนี้เพื่อรับทั้งตัวกรองและคอลัมน์ที่เกี่ยวข้องในหน้ารายการโพสต์ที่กำหนดเอง (โอ้มันจะทำงานกับโพสต์และหน้า) ด้วย:

  • ขั้นตอนที่ # 1: restrict_manage_postsเบ็ดการกระทำ
  • ขั้นตอนที่ # 2: parse_queryเบ็ดกรอง
  • ขั้นตอนที่ # 3: manage_posts_columnsเบ็ดกรอง
  • ขั้นตอนที่ # 4: manage_posts_custom_columnเบ็ดการกระทำ

ดาวน์โหลดรหัสได้ที่ไหน

แต่ถ้าฉันบังคับให้คุณอ่านทั้งหมดข้างต้นฉันจะไม่เป็นคนที่ดีถ้าฉันทำให้คุณขุดรหัสเพื่อที่จะลอง! แต่ตรงกันข้ามกับที่บางคนพูดว่าฉันดี ดังนั้นที่นี่คุณไป:

หมายเหตุถึง @tarasm : ผมรวมตะขอสำหรับregister_post_type()และregister_taxonomy()เพื่อให้คนอื่น ๆ อาจจะลองนี้ออกมาได้โดยไม่ต้องสร้างให้พวกเขา คุณอาจต้องการลบการเรียกฟังก์ชันทั้งสองก่อนที่จะทดสอบ

ตอนจบ


คำตอบเดิม:

สวัสดี@ tarasm :

คุณกำลังมองหาแบบเลื่อนลงที่ด้านบนเช่นหน้าจอนี้หรือคุณกำลังมองหาแบบเลื่อนลงหนึ่งรายการต่อการบันทึกโพสต์และหากเป็นเช่นนั้นคุณคาดหวังให้การทำงานแบบหลังเป็นอย่างไร

วิธีการสร้างเรียงตามฟังก์ชั่นสำหรับประเภทโพสต์ที่กำหนดเองใน WordPress Admin
(ที่มา: mikeschinkel.com )

ถ้าเป็นอย่างแรกให้ดูที่คำตอบสำหรับคำถามนี้วิธีการจัดเรียงพื้นที่ผู้ดูแลระบบของประเภทโพสต์ที่กำหนดเอง Wordpress โดยเขตข้อมูลที่กำหนดเอง? หากนั่นคือสิ่งที่คุณต้องการฉันสามารถให้ข้อมูลเฉพาะเพิ่มเติมที่เกี่ยวข้องกับอนุกรมวิธาน


ฉันกำลังมองหา 1 ดรอปดาวน์ที่ด้านบนที่จะแสดงตัวกรองหมวดหมู่ ฉันหลงทางถ้ามีวิธีการมาตรฐานในการทำเช่นนี้โดยไม่ต้องเขียนโค้ดที่กำหนดเอง
Taras Mankovski

ตอนแรกหน้าแดงฉันไม่คิดว่าคุณสามารถทำรหัสที่กำหนดเองโดยไม่ต้อง แต่แล้วฉันไม่คิดว่ารหัสที่กำหนดเองจะมีความสำคัญ ฉันมีลูกค้าที่โทรมาเพื่อเตรียมพร้อมดังนั้นมันจะต้องมาภายหลังในวันนี้ว่า
MikeSchinkel

2
ที่จริงแล้วทั้งสารละลาย (โซมาติกและ MikeSchinkel) ไม่ทำงานเมื่อคุณพยายามกรอง taxonomy ที่ต่างกัน 2 ตัวในตัวกรองเดียวกัน: - / กรอง taxonomy ล่าสุดเสมอเมื่อพยายามกรอง 2+ ในเวลาเดียวกัน
Ünsal Korkmaz

1
@ Ünsal WordPress รุ่นปัจจุบัน (3.0) ไม่รองรับการสืบค้น Taxonomy หลายคำ แต่จากสิ่งที่ฉันได้ยินมาว่าจะเปลี่ยนไปด้วยเวอร์ชัน 3.1 ในการทำให้ตัวอย่างนี้ใช้งานได้กับหลาย taxonomies คุณจะต้องเพิ่มตัวเชื่อมและ wheres ลงในแบบสอบถามผ่านทาง hooks_join และ posts_where ตัวกรอง
Manny Fleurmond

1
ใน WP 3.1+ ขั้นตอนที่หนึ่งและสองนั้นดีกว่าใน @ drew-gourley คำตอบ (อันที่จริงขั้นตอนที่ 2 ของคุณไม่ทำงานสำหรับฉันฉันคิดว่ามีการเปลี่ยนแปลงในตัวกรองนี้ใน WordPress ใหม่)
Tomasz Struczyński

44

เพียงแค่ต้องการแบ่งปันการใช้งานทางเลือก ฉันไม่ได้มีแบบฝึกหัดที่น่าทึ่งของไมค์เมื่อฉันคิดออกดังนั้นวิธีแก้ปัญหาของฉันจึงต่างออกไปเล็กน้อย โดยเฉพาะฉันจะทำให้ขั้นตอนที่ # 1ของไมค์ง่ายขึ้นและกำจัด ขั้นตอนที่ # 2 - ขั้นตอนอื่นยังคงมีผลบังคับใช้

ในการกวดวิชาของไมค์ที่ใช้wp_dropdown_categories()ช่วยเราคู่มือบางรายการอาคาร แต่ต้องมีการปรับเปลี่ยนบางอย่างซับซ้อนแบบสอบถามเงื่อนไข ( ขั้นตอนที่ 2 ) เพื่อจัดการการใช้งานของรหัสแทนกระสุน ไม่ต้องพูดถึงความยากลำบากในการแก้ไขโค้ดนั้นเพื่อจัดการกับสถานการณ์อื่น ๆ เช่นตัวกรองอนุกรมวิธานหลายตัว ..

อีกวิธีคือการไม่ใช้ข้อบกพร่องwp_dropdown_categories()ทั้งหมด แต่เพื่อสร้างรายการตัวเลือกแบบเลื่อนลงของเราเองตั้งแต่เริ่มต้น มันไม่ซับซ้อนเลยใช้โค้ดน้อยกว่า 30 บรรทัดและไม่ต้องต่อสายparse_queryเลย:

add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {

    // only display these taxonomy filters on desired custom post_type listings
    global $typenow;
    if ($typenow == 'photos' || $typenow == 'videos') {

        // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
        $filters = array('plants', 'animals', 'insects');

        foreach ($filters as $tax_slug) {
            // retrieve the taxonomy object
            $tax_obj = get_taxonomy($tax_slug);
            $tax_name = $tax_obj->labels->name;
            // retrieve array of term objects per taxonomy
            $terms = get_terms($tax_slug);

            // output html for taxonomy dropdown filter
            echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
            echo "<option value=''>Show All $tax_name</option>";
            foreach ($terms as $term) {
                // output each select option line, check against the last $_GET to show the current option selected
                echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
            }
            echo "</select>";
        }
    }
}

เพียงแค่เสียบ taxonomies ที่ต้องการลงใน$filtersอาร์เรย์คุณสามารถส่งออกตัวกรองอนุกรมวิธานหลาย ๆ ตัวได้อย่างรวดเร็ว พวกเขาจะปรากฏเหมือนกับในภาพหน้าจอของ Mike จากนั้นคุณสามารถผ่านไปกับขั้นตอน # 3และ# 4


4
@somatic - อัปเดตที่ดี! ใช่การใช้งานwp_dropdown_categories()ต้องใช้การแก้ไขปัญหาจำนวนมาก ฉันพยายามที่จะยึดติดกับฟังก์ชั่นหลักเมื่อเป็นไปได้ แต่เมื่อคุณชี้ให้เห็นว่าบางครั้งมันก็ใช้งานได้มากกว่านี้ เพิ่งไปพิสูจน์ด้วย WordPress มักจะมีวิธีที่ดีกว่าหนึ่งวิธีในการแก้ปัญหา เยี่ยมมาก!
MikeSchinkel

เพิ่งหยุดทำงานกับฉันใน WordPress 3.1 พยายามหาว่าอะไรที่เปลี่ยนแปลงไปอย่างแน่นอน ดูเหมือนว่ามันจะยังคงใช้งานได้: อนุกรมวิธานและตัวบุ้งระยะแสดงเป็นค่า GET ใน url แต่ผลลัพธ์ทั้งหมดนั้นคือผลลัพธ์ 0 รายการ
Manny Fleurmond

ฉันพยายามทำให้มันใช้งานได้ แต่วิธีเดียวที่ฉันสามารถทำได้คือใช้ตะขอ parse_query ตรวจสอบการสืบค้นของ taxonomy และตั้ง taxonomy และ term vars แบบสอบถามตามนั้น ใช้ WP 3.1 taxonomy & term ควรปรากฏใน URL เมื่อส่งตัวกรองหรือไม่
sanchothefat

2
ทำงานเหมือนมีเสน่ห์สำหรับฉัน! ทางออกที่หรูหรามาก ฉันเป็นหนี้คุณ :) เบียร์
คาลเมา

@somatic วิธีนี้ใช้งานได้ดี แต่มีวิธีให้ $ term-> นับได้เพียงนับข้อกำหนดสำหรับประเภทโพสต์นั้นหรือไม่ ตัวอย่างเช่นหากฉันมี taxonomy ที่กำหนดเองสำหรับทั้งภาพถ่ายและวิดีโอมันจะแสดงให้ฉันดูเมื่อโพสต์วิดีโอที่กำหนดเองพิมพ์จำนวนโพสต์ทั้งหมดสำหรับคำนั้นจากทั้งประเภทโพสต์ที่กำหนดเองแทนที่จะเป็นเพียงจำนวนโพสต์วิดีโอทั้งหมดที่ใช้ วาระ
Greenhoe

13

นี่คือรุ่นของสิ่งนี้ที่สร้างและใช้ตัวกรองจาก taxonomies ทั้งหมดที่ใช้กับประเภทโพสต์ที่กำหนดเองทั้งหมดที่ใช้โดยอัตโนมัติ (ช่างเป็นคำพูด) ยังไงก็ตามฉันก็ปรับมันด้วยดังนั้นมันจึงใช้งานได้กับ wp_dropdown_categories () และ wordpress 3.1 โครงการที่ฉันกำลังทำอยู่เรียกว่า ToDo คุณสามารถเปลี่ยนชื่อฟังก์ชั่นเป็นบางสิ่งที่เหมาะสมสำหรับคุณ แต่สิ่งนี้จะใช้ได้ผลกับทุกสิ่งโดยอัตโนมัติ

function todo_restrict_manage_posts() {
    global $typenow;
    $args=array( 'public' => true, '_builtin' => false ); 
    $post_types = get_post_types($args);
    if ( in_array($typenow, $post_types) ) {
    $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $tax_obj = get_taxonomy($tax_slug);
            wp_dropdown_categories(array(
                'show_option_all' => __('Show All '.$tax_obj->label ),
                'taxonomy' => $tax_slug,
                'name' => $tax_obj->name,
                'orderby' => 'term_order',
                'selected' => $_GET[$tax_obj->query_var],
                'hierarchical' => $tax_obj->hierarchical,
                'show_count' => false,
                'hide_empty' => true
            ));
        }
    }
}
function todo_convert_restrict($query) {
    global $pagenow;
    global $typenow;
    if ($pagenow=='edit.php') {
        $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $var = &$query->query_vars[$tax_slug];
            if ( isset($var) ) {
                $term = get_term_by('id',$var,$tax_slug);
                $var = $term->slug;
            }
        }
    }
    return $query;
}
add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
add_filter('parse_query','todo_convert_restrict');

โปรดทราบว่าฉันใช้ปลั๊กอินที่เพิ่ม 'term_order' เป็นวิธีการสั่งซื้อคำสั่งคุณจะต้องเปลี่ยนหรือลบอาร์กิวเมนต์นั้นเพื่อย้อนกลับไปที่ค่าเริ่มต้น


เซ็กซี่มากอย่างแน่นอน ฉันได้รับแจ้งข้อผิดพลาดดังนั้นฉันจึงเปลี่ยนถ้า (isset ($ var)) เป็นถ้า (isset ($ var) && $ var> 0) เพื่อหลีกเลี่ยงการพยายามค้นหาคำที่เกี่ยวข้องกับค่าดูทั้งหมด 0 โอ้และฉันต้องส่งคืนเคียวรี $ ในฟังก์ชัน
todo_convert_restrict

11

ตอบช้า

แก้ไข

ฉันได้เขียนFilteramaปลั๊กอินที่จะเพิ่มฟังก์ชันการทำงานนี้เป็นวิธีที่ง่ายที่สุด

อัปเดตสำหรับ WordPress 3.5+

ตอนนี้สิ่งที่ง่ายมากที่นี่เป็นอย่างมากการแก้ปัญหาง่ายๆเป็นปลั๊กอินหรือ MU-ปลั๊กอิน

มันใช้ทรัพยากรน้อยที่สุดเท่าที่ทำได้โหลดบนหน้าจอที่ต้องการและเพิ่มคอลัมน์ + ตัวกรองสำหรับทุกอนุกรมวิธานที่กำหนดเอง

add_action( 'plugins_loaded', array( 'WCM_Admin_PT_List_Tax_Filter', 'init' ) );
class WCM_Admin_PT_List_Tax_Filter
{
    private static $instance;

    public $post_type;

    public $taxonomies;

    static function init()
    {
        null === self::$instance AND self::$instance = new self;
        return self::$instance;
    }

    public function __construct()
    {
        add_action( 'load-edit.php', array( $this, 'setup' ) );
    }

    public function setup()
    {
        add_action( current_filter(), array( $this, 'setup_vars' ), 20 );

        add_action( 'restrict_manage_posts', array( $this, 'get_select' ) );

        add_filter( "manage_taxonomies_for_{$this->post_type}_columns", array( $this, 'add_columns' ) );
    }

    public function setup_vars()
    {
        $this->post_type  = get_current_screen()->post_type;
        $this->taxonomies = array_diff(
            get_object_taxonomies( $this->post_type ),
            get_taxonomies( array( 'show_admin_column' => 'false' ) )
        );
    }

    public function add_columns( $taxonomies )
    {
        return array_merge( taxonomies, $this->taxonomies );
    }


    public function get_select()
    {
        $walker = new WCMF_walker;
        foreach ( $this->taxonomies as $tax )
        {
            wp_dropdown_categories( array(
                'taxonomy'        => $tax,
                'hide_if_empty'   => true,
                'show_option_all' => sprintf(
                    get_taxonomy( $tax )->labels->all_items
                ),
                'hide_empty'      => true,
                'hierarchical'    => is_taxonomy_hierarchical( $tax ),
                'show_count'      => true,
                'orderby'         => 'name',
                'selected'        => '0' !== get_query_var( $tax )
                    ? get_query_var( $tax )
                    : false,
                'name'            => $tax,
                'id'              => $tax,
                'walker'          => $walker,
            ) );
        }

    }

}

และจากนั้นคุณเพียงแค่ต้องการชั้นเรียนวอล์คเกอร์ที่กำหนดเอง

class WCMF_walker extends Walker_CategoryDropdown
{
    public $tree_type = 'category';
    public $db_fields = array(
        'parent' => 'parent',
        'id'     => 'term_id',
    );
    public $tax_name;

    public function start_el( &$output, $term, $depth, $args, $id = 0 )
    {
        $pad = str_repeat( '&nbsp;', $depth * 3 );
        $cat_name = apply_filters( 'list_cats', $term->name, $term );
        $output .= sprintf(
            '<option class="level-%s" value="%s" %s>%s%s</option>',
            $depth,
            $term->slug,
            selected(
                $args['selected'],
                $term->slug,
                false
            ),
            $pad.$cat_name,
            $args['show_count']
                ? "&nbsp;&nbsp;({$term->count})"
                : ''
        );
    }
}

ทำให้สิ่งนี้เป็นจริง แต่เมธอด get_select () จะหายไป
Dave Romsey

@ goto10 คุณพูดถูก Updated Btw: ง่ายกว่าแค่หยิบปลั๊กอินที่ลิงก์มา มันจะพร้อมใช้งานในที่เก็บปลั๊กอินในหนึ่งหรือสองสัปดาห์ (ยืนยันแล้ว)
ไกเซอร์

ฉันต้องใช้$this->setup_vars();ตอนแรกpublic function setup()เพื่อที่จะ"manage_taxonomies_for_{$this->post_type}_columns"ทำงาน
Christian

แต่อาจเป็นเพราะฉันใช้ใน Theme function.php ด้วยadd_action( 'init', array( 'WCM_Admin_PT_List_Tax_Filter', 'init' ) );
Christian

@Christian นี่ไม่ใช่เนื้อหาของธีม สิ่งนี้อยู่ในปลั๊กอินและในขณะที่โค้ดด้านบนย่อมาจากโหลดก่อนที่จะโหลดธีม
ไกเซอร์

7

ฉันแค่ต้องการที่จะทำให้ทราบอย่างรวดเร็ว ใน WP รุ่นที่ใหม่กว่าการลงรายการบัญชีในผู้ดูแลระบบจะจัดการโดยคลาส WP_Posts_List_Table รหัส Apply_filters ตอนนี้เป็นดังนี้:

if ( 'page' == $post_type )
        $posts_columns = apply_filters( 'manage_pages_columns', $posts_columns );
    else
        $posts_columns = apply_filters( 'manage_posts_columns', $posts_columns, $post_type );
    $posts_columns = apply_filters( "manage_{$post_type}_posts_columns", $posts_columns );

ดังนั้นในการเพิ่มคอลัมน์ใหม่เบ็ด add_filter ควรเป็นเช่นนี้:

add_filter( 'manage_posts_columns', 'my_add_columns', 10, 2);

ต่อไปนี้เป็นตัวอย่าง:

function my_add_columns($posts_columns, $post_type)
{
  if ('myposttype' == $post_type) {
    $posts_columns = array(
      "cb"            => "<input type=\"checkbox\" />",
      "title"         => "Title",
      "anothercolumn" => "Bacon",
      "date"          => __( 'Date' )
    );
    return $posts_columns;
  }
} 

ตอนนี้สำหรับแถวโพสต์ นี่คือรหัสที่จัดการข้อมูลคอลัมน์ในรายการ:

default:
            ?>
            <td <?php echo $attributes ?>><?php
                if ( is_post_type_hierarchical( $post->post_type ) )
                    do_action( 'manage_pages_custom_column', $column_name, $post->ID );
                else
                    do_action( 'manage_posts_custom_column', $column_name, $post->ID );
                do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID );
            ?></td>
            <?php

เพื่อดึงข้อมูลโพสต์ของเราเราต้องเพิ่มตะขอการกระทำเช่นนี้:

add_action( "manage_(here_goes_your_post_type)_posts_custom_column", "my_posttype_add_column", 10, 2);

ตัวอย่าง (ตัวอย่างนี้ใช้ taxonomies แต่คุณสามารถสอบถามสิ่งอื่น ๆ ):

function my_posttype_add_column($column_name, $post_id)
{
  switch ($column_name) {
    case 'anothercolumn':
      $flavours = get_the_terms($post_id, 'flavour');
      if (is_array($flavours)) {
        foreach($flavours as $key => $flavour) {
          $edit_link = get_term_link($flavour, 'flavour');
          $flavours[$key] = '<a href="'.$edit_link.'">' . $flavour->name . '</a>';
        }
        echo implode(' | ',$flavours);
      }
      break;

    default:
      break;
  }
}

7

ทำงานได้ใน WP 3.2!

custom_post_type: หนังสือ custom_taxonomy: ประเภท

มีเพียงการแก้ไขเท่านั้นที่บอกว่า: // change here

function restrict_books_by_genre() {
    global $typenow;
    $post_type = 'books'; // change HERE
    $taxonomy = 'genre'; // change HERE
    if ($typenow == $post_type) {
        $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
        $info_taxonomy = get_taxonomy($taxonomy);
        wp_dropdown_categories(array(
            'show_option_all' => __("Show All {$info_taxonomy->label}"),
            'taxonomy' => $taxonomy,
            'name' => $taxonomy,
            'orderby' => 'name',
            'selected' => $selected,
            'show_count' => true,
            'hide_empty' => true,
        ));
    };
}

add_action('restrict_manage_posts', 'restrict_books_by_genre');


function convert_id_to_term_in_query($query) {
    global $pagenow;
    $post_type = 'books'; // change HERE
    $taxonomy = 'genre'; // change HERE
    $q_vars = &$query->query_vars;
    if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0) {
        $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
        $q_vars[$taxonomy] = $term->slug;
    }
}

add_filter('parse_query', 'convert_id_to_term_in_query');

นั่นเป็นทางออกที่ดีและง่ายสำหรับ WP 3.2+
petermolnar

มันใช้งานได้ แต่__("Show All {$info_taxonomy->label}")เป็นวิธีที่ผิดที่จะใช้สตริงที่แปลได้
Mark Kaplun

2

ต่อไปนี้เป็นวิธีดำเนินการโดยใช้การกระทำ LIMIT_manage_posts ดูเหมือนว่าจะทำงานได้ดีสำหรับฉันและเพิ่มความสามารถในการกรองตามอนุกรมวิธานสำหรับโพสต์ทุกประเภทและมี taxonomies ที่เกี่ยวข้อง

// registers each of the taxonomy filter drop downs
function sunrise_fbt_add_taxonomy_filters() {
    global $typenow;            // the current post type
    $taxonomies = get_taxonomies('','objects');
    foreach($taxonomies as $taxName => $tax) {
    if(in_array($typenow,$tax->object_type) && $taxName != 'category' && $taxName != 'tags') {
            $terms = get_terms($taxName);
            if(count($terms) > 0) {
              //Check if hierarchical - if so build hierarchical drop-down
              if($tax->hierarchical) {
                $args = array(
                      'show_option_all'    => 'All '.$tax->labels->name,
                      'show_option_none'   => 'Select '.$tax->labels->name,
                      'show_count'         => 1,
                      'hide_empty'         => 0, 
                      'echo'               => 1,
                      'hierarchical'       => 1,
                      'depth'              => 3, 
                      'name'               => $tax->rewrite['slug'],
                      'id'                 => $tax->rewrite['slug'],                      
                      'class'              => 'postform',
                      'depth'              => 0,
                      'tab_index'          => 0,
                      'taxonomy'           => $taxName,
                      'hide_if_empty'      => false);
            $args['walker'] = new Walker_FilterByTaxonomy;
                wp_dropdown_categories($args);
              } else {
                    echo "<select name='".$tax->rewrite['slug']."' id='".$tax->rewrite['slug']."' class='postform'>";
                    echo "<option value=''>Show All ".$tax->labels->name."</option>";
                    foreach ($terms as $term) { 
              echo '<option value="' . $term->slug . '"', $_GET[$taxName] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; 
            }
                    echo "</select>";
                }
            }
    }
    }
}
add_action( 'restrict_manage_posts', 'sunrise_fbt_add_taxonomy_filters', 100 );

/**
 * Create HTML dropdown list of Categories.
 *
 * @package WordPress
 * @since 2.1.0
 * @uses Walker
 */
class Walker_FilterByTaxonomy extends Walker {
    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
    function start_el(&$output, $category, $depth, $args) {
      $args['selected'] = get_query_var( $args['taxonomy'] );
        $pad = str_repeat('&nbsp;', $depth * 3);

        $cat_name = apply_filters('list_cats', $category->name, $category);
        $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
        if ( $category->slug == $args['selected'] )
            $output .= ' selected="selected"';
        $output .= '>';
        $output .= $pad.$cat_name;
        if ( $args['show_count'] )
            $output .= '&nbsp;&nbsp;('. $category->count .')';
        if ( $args['show_last_update'] ) {
            $format = 'Y-m-d';
            $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
        }
        $output .= "</option>\n";
        }
} 

One note - ฉันพยายาม จำกัด ความลึกเนื่องจาก taxonomies แบบลำดับชั้นของฉันมีขนาดใหญ่มาก แต่ก็ใช้งานไม่ได้ - อาจเป็นข้อผิดพลาดในฟังก์ชั่น wp_dropdown_categories


2

เรื่องนี้ไม่เป็นที่รู้จักกันดีฉันเดา แต่ในฐานะของ wordpress 3.5 คุณสามารถผ่าน'show_admin_column' => trueไปregister_taxonomyได้ สิ่งนี้ทำ 2 สิ่ง:

  1. เพิ่มคอลัมน์ taxonomy เข้ากับมุมมองรายการชนิดโพสต์ของผู้ดูแลระบบ
  2. โดยการคลิกที่ชื่อของคำในคอลัมน์อนุกรมวิธานนั้นอันที่จริงแล้วมันจะกรองรายการไปยังคำนั้น

ดังนั้นไม่เหมือนกันกับการเลือก แต่เกือบฟังก์ชั่นเดียวกันความกว้างเพียงหนึ่งแถวของรหัส

https://make.wordpress.org/core/2012/12/11/wordpress-3-5-admin-columns-for-custom-taxonomies/

นอกจากนี้อย่างที่คุณสามารถอ่านได้มีตัวกรองใหม่ที่เหมาะสำหรับการเพิ่มคอลัมน์อนุกรมวิธาน (ถ้าคุณต้องการจริงๆ)


1

คำตอบของ @ somatic ตามลำดับชั้นตามที่ @kevin ร้องขอ:

<?php
add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {

    // only display these taxonomy filters on desired custom post_type listings
    global $typenow;
    if ($typenow == 'photos' || $typenow == 'videos') {

        // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
        $filters = array('plants', 'animals', 'insects');

        foreach ($filters as $tax_slug) {
            // retrieve the taxonomy object
            $tax_obj = get_taxonomy($tax_slug);
            $tax_name = $tax_obj->labels->name;

            // output html for taxonomy dropdown filter
            echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
            echo "<option value=''>Show All $tax_name</option>";
            generate_taxonomy_options($tax_slug,0,0);
            echo "</select>";
        }
    }
}

function generate_taxonomy_options($tax_slug, $parent = '', $level = 0) {
    $args = array('show_empty' => 1);
    if(!is_null($parent)) {
        $args = array('parent' => $parent);
    } 
    $terms = get_terms($tax_slug,$args);
    $tab='';
    for($i=0;$i<$level;$i++){
        $tab.='--';
    }
    foreach ($terms as $term) {
        // output each select option line, check against the last $_GET to show the current option selected
        echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>';
        generate_taxonomy_options($tax_slug, $term->term_id, $level+1);
    }

}
?>

โดยทั่วไปฉันจะลบโค้ดที่สร้างตัวเลือกและใส่ไว้ในฟังก์ชั่นของตัวเอง ฟังก์ชัน 'generate_taxonomy_options' นอกเหนือจากการใช้ tax_slug ยังใช้พารามิเตอร์พาเรนต์และระดับ ฟังก์ชันจะถือว่าตัวเลือกการสร้างสำหรับพาเรนต์ 0 ซึ่งจะเลือกคำศัพท์ระดับรูททั้งหมด ในลูปฟังก์ชันจะเรียกตัวเองซ้ำโดยใช้คำปัจจุบันเป็นผู้ปกครองและเพิ่มระดับทีละหนึ่ง มันจะเพิ่มเห็บไปทางด้านข้างโดยอัตโนมัติยิ่งคุณลงต้นไม้และ voila!


1

การปรับปรุงของคำตอบ @Drew กูร์เลย์สำหรับ WP 3.3.1 (และการใช้มาตรการโค้ดจากhttp://wordpress.org/support/topic/wp_dropdown_categories-generating-url-id-number-instead-of-slug?replies=6#post- 2529115 ):

add_action('restrict_manage_posts', 'xyz_restrict_manage_posts');
function xyz_restrict_manage_posts() {
    global $typenow;

    $args = array('public'=>true, '_builtin'=>false); 
    $post_types = get_post_types($args);

    if(in_array($typenow, $post_types)) {
        $filters = get_object_taxonomies($typenow);

        foreach ($filters as $tax_slug) {
            $tax_obj = get_taxonomy($tax_slug);
            $term = get_term_by('slug', $_GET[$tax_obj->query_var], $tax_slug);

            wp_dropdown_categories(array(
                'show_option_all' => __('Show All '.$tax_obj->label ),
                'taxonomy' => $tax_slug,
                'name' => $tax_obj->name,
                'orderby' => 'term_order',
                'selected' => $term->term_id,
                'hierarchical' => $tax_obj->hierarchical,
                'show_count' => false,
                // 'hide_empty' => true,
                'hide_empty' => false,
                'walker' => new DropdownSlugWalker()
            ));
        }
    }
}


//Dropdown filter class.  Used with wp_dropdown_categories() to cause the resulting dropdown to use term slugs instead of ids.
class DropdownSlugWalker extends Walker_CategoryDropdown {

    function start_el(&$output, $category, $depth, $args) {
        $pad = str_repeat('&nbsp;', $depth * 3);

        $cat_name = apply_filters('list_cats', $category->name, $category);
        $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";

        if($category->term_id == $args['selected'])
            $output .= ' selected="selected"';

        $output .= '>';
        $output .= $pad.$cat_name;
        $output .= "</option>\n";
    }
}

0

ขออภัยในความเป็นจริงว่าในฐานะผู้ใช้ใหม่ฉันไม่สามารถโพสต์ความคิดเห็น แต่ฉันสามารถโพสต์คำตอบได้ ...

ในฐานะของ WordPress 3.1 (RC 1) คำตอบของไมค์ (ซึ่งให้บริการฉันดีสำหรับสองสามเดือนที่ผ่านมา) ไม่ได้ผลสำหรับฉันอีกต่อไป การ จำกัด โดยอนุกรมวิธานเด็ก ๆ ให้ผลที่ว่างเปล่า ฉันลองอัปเดตของโซมาติและมันใช้งานได้ดีมาก ยิ่งไปกว่านั้นมันทำงานกับเคียวรีอนุกรมวิธานหลายรายการที่ทำงานกับรีลีสนี้


ด้วยเหตุผลบางอย่างรุ่นโซมาติกไม่ทำงานสำหรับ 3.1
Manny Fleurmond

0

ลองใช้รหัสทั้งสองจากไมค์และโซมาติกและสงสัยว่าจะได้รับสิ่งใดจากแต่ละเทคนิค:

ด้วยรหัสของไมค์มันจะแสดงรายการแบบหล่นลงพร้อมตัวเลือกลำดับชั้นซึ่งช่วยได้มาก แต่เพื่อที่จะแสดงสองดรอปดาวน์ผมต้องทำซ้ำ if ($typenow=='produtos') {...}คำสั่งในฟังก์ชั่นrestrict_listings_by_business()และif ($pagenow=='edit.php' && ... }ใน convert_business_id_to_taxonomy_term_in_query($query)ฟังก์ชั่นซึ่งตอนนี้ให้รหัสจำนวนมาก

ด้วยรหัสโซมาติกของฉันฉันต้องระบุ taxonomies ฉันต้องการดูเป็นแบบเลื่อนลงและปังทำงาน; $filters = array('taxo1', 'taxo2');

คำถาม: ฉันสามารถเข้าใกล้โซมาติกและมีตัวเลือกลำดับชั้นได้หรือไม่

ขอบคุณมากสำหรับการกวดวิชานี้ช่วยมาก!


Se คำตอบของฉันสำหรับการแก้ปัญหาลำดับชั้น
Manny Fleurmond

0

บทช่วยสอนของ Mike ในเรื่องนี้ยอดเยี่ยมมาก! ฉันอาจจะไม่ต้องใส่ใจกับการเพิ่มฟังก์ชั่นนี้ในปลั๊กอินหมวดหมู่สื่อของฉันถ้าฉันต้องคิดออกเอง

ที่กล่าวว่าฉันคิดว่าการใช้parse_queryและจากนั้นรับการสืบค้นสำหรับคำที่ไม่จำเป็น มันสะอาดสำหรับการสร้างคลาสวอล์คเกอร์ที่คุณกำหนดเอง อาจเป็นไปไม่ได้เมื่อเขาเขียนบทความของเขา - มันอายุ 3 ปีในขณะที่ฉันเขียนบทความนี้

ชำระเงินตัวอย่างที่ดีนี้ใน GitHub ใช้งานได้อย่างมีเสน่ห์เปลี่ยน ID ในค่าดร็อปดาวน์เป็นตัวบุ้งดังนั้นมันจึงใช้งานได้จริงโดยไม่ต้องแก้ไขเคียวรี

https://gist.github.com/stephenh1988/2902509

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.