function.php的问题

时间:2017-09-09 11:02:42

标签: php wordpress web backend

&&

**附加示例: **

function getParentID() {
    $url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
    $pageID = url_to_postid($url);
    $pageID = trim($pageID);
    return $pageID;
}

function OrderFieldsQuery($args) {

    //->> WORKS WITH HARD-CODED ID'S
    $args['post_parent'] = 52;

    $args['post_parent'] = getParentID();
    // Doesn't work in that way, even function returns same value;

    return $args;
}

add_filter('acf/fields/relationship/query', 'OrderFieldsQuery', 10, 1); // add key to filter

输出我得到这个.........:

数组([post_parent] => 52 [post_parent1] => 52 [等于] => 1)

所以它们是平等的但只有在硬编码时才有效。

2 个答案:

答案 0 :(得分:2)

从代码

获取页面网址
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));

现在使用下面的代码来获取页面ID

  $postid = url_to_postid( $current_url );

现在你将获得id

答案 1 :(得分:1)

试试这个

function OrderFieldsQuery($args) {

    //->> WORKS WITH HARD-CODED ID'S
    $args['post_parent'] = 52;

    $url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
    $pageID = url_to_postid($url);
    $pageID = trim($pageID);
    $args['post_parent'] = $pageID;
    // Doesn't work in that way, even function returns same value;

    return $args;
}

add_filter('acf/fields/relationship/query', 'OrderFieldsQuery', 10, 1); // add key to filter