Ask a Question Page Category View Hierarchy
Hello.
I was able to solve the process of separating the category selection into main and sub on the question page as follows, but this arrangement caused an error message on the AnsPress settings screen and I overcame it. You cannot select pages with this error, but it does not disrupt previous selections. These are the things I noticed.
What kind of problems does this regulation cause in the general framework? I wonder about this. If it’s just a minor warning, using this gets me there.
It would be great if someone could review it in detail and report any issues that may arise with AnsPress.
<?php /** * AnsPress Select tipi alan nesnesi. * * @package AnsPress * @subpackage Fields * @since 4.1.0 * @author Rahul Aryan<[email protected]> * @copyright Copyright (c) 2017, Rahul Aryan * @license http://opensource.org/licenses/gpl-3.0.php GNU Genel Kamu Lisansı */ namespace AnsPress\Form\Field; // Doğrudan erişildiğinde çıkış yap. if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Select tipi alan nesnesi. * * @since 4.1.0 */ class Select extends \AnsPress\Form\Field { /** * Alan tipi. * * @var string */ public $type = 'select'; /** * Alanı hazırla. * * @return void */ protected function prepare() { $this->args = wp_parse_args( $this->args, array( 'label' => __( 'AnsPress Seçim Alanı', 'anspress-question-answer' ), 'options' => array(), 'terms_args' => array( 'taxonomy' => 'question_category', 'hide_empty' => false, 'orderby' => 'id', 'parent' => 0, // Yalnızca üst düzey terimleri al (ana kategoriler) ), ) ); // Ebeveyn hazırlığını çağır. parent::prepare(); // 'options' değerine göre sanitize işlevini belirle. if ( in_array( $this->get( 'options' ), array( 'posts', 'terms' ), true ) ) { $this->sanitize_cb = array_merge( array( 'absint' ), $this->sanitize_cb ); } else { $this->sanitize_cb = array_merge( array( 'text_field' ), $this->sanitize_cb ); } } /** * Bir seçim alanının seçeneklerini döndür. * * @return array */ private function get_options() { $options = $this->get( 'options' ); if ( is_string( $options ) && 'terms' === $options ) { $terms_args = $this->get( 'terms_args', array() ); // Yalnızca üst düzey terimleri al (ana kategoriler) $terms_args['parent'] = 0; $main_categories = get_terms( $terms_args ); $category_options = array(); foreach ( $main_categories as $main_category ) { $child_terms = get_terms( array( 'taxonomy' => 'question_category', 'parent' => $main_category->term_id, 'hide_empty' => false, ) ); foreach ( $child_terms as $child_term ) { if ( ! isset( $category_options[ $main_category->name ] ) ) { $category_options[ $main_category->name ] = array(); } $category_options[ $main_category->name ][ $child_term->term_id ] = $child_term->name; } } return $category_options; } if ( is_string( $options ) && 'posts' === $options ) { return wp_list_pluck( get_posts( $this->get( 'posts_args', array() ) ), 'post_title', 'ID' ); } return array(); // Beklenmeyen durumlar için boş dizi döndür } /** * Alan HTML işaretleme. * * @return void */ public function field_markup() { parent::field_markup(); $this->add_html( '<select' . $this->common_attr() . $this->custom_attr() . '>' ); $this->add_html( '<option value="">' . __( 'Bir seçenek seçin', 'anspress-question-answer' ) . '</option>' ); $options = $this->get_options(); // $options bir dizi veya nesne mi diye kontrol edin if ( is_array($options) || is_object($options) ) { foreach ($options as $main_category => $child_terms) { if (!is_array($child_terms) && !is_object($child_terms)) { continue; // Dizi veya nesne değilse döngüyü atla } $this->add_html('<optgroup label="' . esc_attr($main_category) . '">'); foreach ($child_terms as $term_id => $term_name) { $selected = selected($this->value(), $term_id, false); $this->add_html('<option value="' . esc_attr($term_id) . '" ' . $selected . '>' . esc_html($term_name) . '</option>'); } $this->add_html('</optgroup>'); } } else { // Beklenmeyen durumda ise hata ayıklama mesajı ekleyin error_log('Options is not array or object: ' . var_export($options, true)); $this->add_html('<optgroup label="' . __('Mevcut seçenek yok', 'anspress-question-answer') . '"></optgroup>'); } $this->add_html('</select>'); /** Bu eylem lib/form/class-input.php içinde belgelenmiştir */ do_action_ref_array('ap_after_field_markup', array(&$this)); } }
Haydar Osman İLİK Asked question