簡體   English   中英

從 get_pages 函數獲取標簽、自定義分類法和附件數據

[英]Get tag, custom taxonomy and attachment data from get_pages function

我目前有一些代碼可以使用 get_pages 函數檢索當前登錄的作者提交的所有頁面。 然后我通過 Ajax 將結果傳遞給 Javascript。 一切都按預期工作,我正在檢索我應該找到的頁面。

我的問題是,該對象在此頁面上沒有標簽、其他自定義分類法和附件的任何數據。

有沒有辦法將此數據附加到返回的對象?

函數.php:

function get_ajax_pages()
{
    // Query Arguments
    $args = array(
        'authors' => $author,
        'post_type' => 'page',
        'post_status' => array('publish', 'pending'),
        'hierarchical' => 0
    );

    // The Query
    $ajaxpages = get_pages($args);

    echo json_encode($ajaxpages);

    exit;
}

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_pages', 'get_ajax_pages');
add_action('wp_ajax_nopriv_get_ajax_pages', 'get_ajax_pages');

Javascript:

var adminAjaxUrl = "<?php echo admin_url('admin-ajax.php'); ?>";

// the list of pages by the current author
    var pageList = [];

    // get an array of the current author's pages
    $.ajax({
        type: 'POST',
        url: adminAjaxUrl,
        dataType: "json",
        data: { action: 'get_ajax_pages' },
        success: function (response) {
            pageList = response;
        }
    });

此時,每個條目將返回如下內容:

ID: 100
comment_count: "0"
comment_status: "open"
filter: "raw"
guid: "https://example.com/?page_id=100"
menu_order: 0
ping_status: "closed"
pinged: ""
post_author: "1"
post_content: "This is the post content"
post_content_filtered: ""
post_date: "2021-06-18 10:00:00"
post_date_gmt: "0000-00-00 00:00:00"
post_excerpt: "This is the excerpt"
post_mime_type: ""
post_modified: "2021-06-18 10:00:00"
post_modified_gmt: "0000-00-00 00:00:00"
post_name: ""
post_parent: 19
post_password: ""
post_status: "pending"
post_title: "Example page"
post_type: "page"
to_ping: ""

我希望它還包括以下方面的數據:

tags: "example, test"
myCustomTaxonomy: "extra information, more data"
attachments: "https://example.com/wp-content/uploads/2021/06/myImage.png"

這可能嗎?

如果它有用,這是我想出的解決方案。 它當然可能效率不高,但它似乎至少適合我的目的。

我最終做的是用在第一個 AJAX 調用中找到的每個頁面填充一個選擇列表。 在此列表中選擇一個頁面,然后調用第二個函數,該函數獲取該頁面的各種額外元數據。

函數.php:

function get_ajax_pages()
{
    // Query Arguments
    $args = array(
        'authors' => $author,
        'post_type' => 'page',
        'post_status' => array('publish', 'pending'),
        'hierarchical' => 0
    );

    // The Query
    $ajaxPages = get_pages($args);

    echo json_encode($ajaxPages);

    exit;
}

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_pages', 'get_ajax_pages');
add_action('wp_ajax_nopriv_get_ajax_pages', 'get_ajax_pages');

function get_ajax_meta()
{
    $editId = $_POST['editId'];

    $meta = [];

    $editTags = get_the_tags($editId);
    $editCustomTaxonomy1 = get_the_terms($editId, 'CustomTaxonomy1');
    $editCustomTaxonomy2 = get_the_terms($editId, 'CustomTaxonomy2');
    $media = get_attached_media('', $editId);

    array_push($meta, (object)[
        'tags' => $editTags,
        'customTaxonomy1' => $customTaxonomy1,
        'customTaxonomy1' => $customTaxonomy2,
        'media' => $media
    ]);

    echo json_encode($meta);

    exit;
}

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_meta', 'get_ajax_meta');
add_action('wp_ajax_nopriv_get_ajax_meta', 'get_ajax_meta');

Javascript:

var adminAjaxUrl = "<?php echo admin_url('admin-ajax.php'); ?>";

// the list of pages by the current author
var pageList = [];

// get an array of the current author's pages
$.ajax({
    type: 'POST',
    url: adminAjaxUrl,
    dataType: "json",
    data: { action: 'get_ajax_pages' },
    success: function (response) {
        pageList = response;

        // populate the dropdown with the pages
        if (pageList.length > 0) {
            for (var i = 0; i < pageList.length; i++) {
                $("select[name=edit-page]").append("<option value='" + pageList[i].ID + "'>" + pageList[i].post_title + "</option>");
            }
        }
    }
});

$("body").on("change", "select[name=edit-page]", function () {
    editId = $(this).val();

    // get the meta information of the selected page (tags, custom taxonomies, attached files)
    $.ajax({
        type: 'POST',
        url: adminAjaxUrl,
        dataType: "json",
        data: { "editId": editId, action: "get_ajax_meta" },
        success: function (response) {
            pageMeta = response;

            // get the tags
            console.log(pageMeta[0].tags[i].name)

            // get custom taxonomy 1 data
            console.log(pageMeta[0].customTaxonomy1[i].name)

            // get custom taxonomy 2 data
            console.log(pageMeta[0].customTaxonomy2[i].name)

            // get attached file data
            if (Object.keys(pageMeta[0].media).length > 0) {
                for (var i = 0; i < Object.keys(pageMeta[0].media).length; i++) {
                    console.log(pageMeta[0].media[Object.keys(pageMeta[i].media)[0]]);
                }
            }
        }
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM