首頁 >CMS教程 >&#&按 >定制的WordPress管理過濾器

定制的WordPress管理過濾器

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌原創
2025-02-18 11:11:091039瀏覽

>本文說明瞭如何使用WordPress過濾器自定義郵政管理屏幕。 我們將介紹WordPress的內置過濾器,並向您展示如何創建自定義過濾器以增強功能。

關鍵概念

  • 使用
  • 掛鉤添加新的過濾器。這些允許通過各種標准進行過濾柱。 restrict_manage_posts pre_get_posts>可以通過將代碼添加到主題的
  • >文件(或插件)來創建郵政格式和作者的自定義過濾器。
  • > functions.php
  • 過濾器修改數據庫查詢,控制顯示哪些帖子。
  • 自定義過濾器改善用戶體驗,添加功能並維護乾淨的代碼。 > pre_get_posts
  • >過濾帖子:基礎知識
  • WordPress在郵政管理屏幕上提供默認過濾器(如日期過濾,如下所示)。 主題和插件可以添加更多。 這些過濾器根據指定的標準縮小了帖子列表。 >

    創建自定義過濾器

    Customized WordPress Administration Filters > WordPress提供內置過濾器時,您通常需要自定義的過濾器才能獲得更好的用戶體驗。 兩個鉤子是關鍵:

    :將新的過濾器控件添加到管理員屏幕的頂部。

      :在查詢運行之前修改查詢,過濾顯示的帖子。 >
    • restrict_manage_posts>示例:作者和郵政格式過濾
    • >讓我們為郵政作者和格式創建過濾器。 想像一個網站,帖子手動分配了格式和作者。默認的管理屏幕可能是壓倒性的。 我們將添加下拉菜單以易於過濾。 pre_get_posts>
    • 添加下拉菜單(

    >將以下代碼添加到您的主題的

    >文件(或插件):>

    >由作者過濾:

    restrict_manage_posts

    >按郵政格式過濾:

    functions.php

    這將兩個下拉列表添加到“郵政列表”屏幕。

    >

    <code class="language-php">function add_author_filter() {
        global $post_type;
        if ($post_type == 'post') {
            $user_args = array(
                'show_option_all' => 'All Authors',
                'orderby' => 'display_name',
                'order' => 'ASC',
                'name' => 'author_filter',
                'who' => 'authors',
                'include_selected' => true
            );
            if (isset($_GET['author_filter'])) {
                $user_args['selected'] = (int) sanitize_text_field($_GET['author_filter']);
            }
            wp_dropdown_users($user_args);
        }
    }
    add_action('restrict_manage_posts', 'add_author_filter');</code>

    >過濾帖子列表(
    <code class="language-php">function add_post_format_filter() {
        global $post_type;
        if ($post_type == 'post') {
            $post_formats_args = array(
                'show_option_all' => 'All Formats',
                'orderby' => 'NAME',
                'order' => 'ASC',
                'name' => 'post_format_filter',
                'taxonomy' => 'post_format'
            );
            if (isset($_GET['post_format_filter'])) {
                $post_formats_args['selected'] = sanitize_text_field($_GET['post_format_filter']);
            }
            wp_dropdown_categories($post_formats_args);
        }
    }
    add_action('restrict_manage_posts', 'add_post_format_filter');</code>

    現在,讓我們使下拉列表功能:

    Customized WordPress Administration Filters >作者過濾:

    pre_get_posts>通過郵政格式過濾:

    這些函數將查詢修改為僅包含匹配所選作者或郵政格式的帖子。

    <code class="language-php">function filter_posts_by_author($query) {
        global $post_type, $pagenow;
        if ($pagenow == 'edit.php' && $post_type == 'post' && isset($_GET['author_filter'])) {
            $author_id = sanitize_text_field($_GET['author_filter']);
            if ($author_id != 0) {
                $query->set('author', $author_id);
            }
        }
    }
    add_action('pre_get_posts', 'filter_posts_by_author');</code>

    結論

    這通過自定義過濾器增強了您的WordPress管理員。 您可以通過其他帖子屬性(請參閱WordPress查詢類文檔)進行過濾。 請記住要始終對用戶輸入進行消毒以防止安全漏洞。

    >

    以上是定制的WordPress管理過濾器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn