Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回WordPress中的所有页面名称,不超过?_Php_Wordpress - Fatal编程技术网

Php 返回WordPress中的所有页面名称,不超过?

Php 返回WordPress中的所有页面名称,不超过?,php,wordpress,Php,Wordpress,我需要检索WordPress中注册的每个页面名称的数组。我有两个问题,我可以做一个get_pages()之类的,但它确实吸引了关于每个页面的每一件怪事,包括它们的内容。当我只需要每个页面的页面名称时,完全不必要的开销 另一个是,如果可能的话,我想用一个内置的方法,因为这是一个内部插件,我们希望保持它与主线兼容。(最坏的情况是直接访问数据库并获取它们)我知道可以在get_pages()调用中包含/排除,但我还没有弄清楚是否可以排除检索除一个之外的所有内容,而不是相反的内容 它需要是动态的,因为它不

我需要检索WordPress中注册的每个页面名称的数组。我有两个问题,我可以做一个get_pages()之类的,但它确实吸引了关于每个页面的每一件怪事,包括它们的内容。当我只需要每个页面的页面名称时,完全不必要的开销

另一个是,如果可能的话,我想用一个内置的方法,因为这是一个内部插件,我们希望保持它与主线兼容。(最坏的情况是直接访问数据库并获取它们)我知道可以在get_pages()调用中包含/排除,但我还没有弄清楚是否可以排除检索除一个之外的所有内容,而不是相反的内容

它需要是动态的,因为它不能有任何硬编码字符串,也就是说,知道任何关于页面本身或页面名称的信息。也没有额外的垃圾,像它在一个无序的名单或东西。直线阵列,无需电平。(列出的子页面与主页面相同)

有什么想法吗,伙计们?我搜索了又搜索……但你们可能知道,这些类型的东西的文档是迟钝的

谢谢

最后我想要的示例或类似示例:

Array
(
    [0] => stdClass Object
        (
            [page_name] => 'page1'
        )
    [1] => stdClass Object
        (
            [page_name] => 'page2'
        )
    [2] => stdClass Object
        (
            [page_name] => 'page3'
        )
    [3] => stdClass Object
        (
            [page_name] => 'page4'
        )
)

我选中了
get_pages()
,您无法限制从数据库请求的内容。页面从中获取,如您所见,有一个硬编码的
SELECT*FROM
查询。

要限制返回的字段,您可以设置一个过滤器。在themes functions.php文件或插件中,尝试

add_filter( 'get_pages', 'get_pages_filter' );

function get_pages_filter( $res ){
    $res = array_map( 'get_pages_title', $res ); 
    return $res;
}

function get_pages_title( $item ){
    return (object) array( 'page_name' => $item->post_name );
}

$pages = get_pages();
var_dump( $pages );

如果有人有干净的解决方案,请插话。现在,我将使用WordPress的内置DB连接并手动获取它们

对于好奇的人…我只是很快地把它拼凑起来…可能不是最好的

/**
 * get_wpdb_values()
 *
 * DESC:
 *
 * Allows you to make a WP Database connection
 * and return an Array => Object of what ever
 * values you need from a table.
 *
 * Was made for the purpose of returning a page
 * list, but since you pass it the field you
 * want returned, along with with optional filters
 * it can easily be used for other purposes.
 *
 * USAGE:
 *
 * array get_wpdb_values ( string $table [, string $field [, array $filters ]] )
 *
 * PARAMETERS:
 *
 * ----------|-----------------------------------------------------------------
 * $table    | Required table you want to return values from.
 *           | DO NOT INCLUDE THE WP PREFIX! (e.g. use 'posts' not 'wp_posts'
 * ----------|-----------------------------------------------------------------
 * $field    | Optional field name you want returned. (Default returns * all)
 * ----------|-----------------------------------------------------------------
 * $filters  | Optional filtering passed as field => value array.
 * ----------|-----------------------------------------------------------------
 */

function get_wpdb_values( $table = null,
                          $field = "*",
                          $filters = null )
{
    // Get access to the
    // WordPress Database
    // class in this scope

    global $wpdb;

    // If we weren't passed any
    // arguments, get out quick

    if(is_null($table) || empty($table))
        return false;

    // Add optional filters if
    // they were passed in

    if(!is_null($filters))
    {
        // Counter is so we can tell
        // if there is more then one
        // filter so we can add the
        // AND separator in the
        // SQL query

        $WHERE  = "WHERE ";
        $counter = 0;

        foreach ($filters as $key => &$value)
        {
            $counter++;

            // If we're on the second or more
            // pair, we add the AND to chain
            // conditional WHERE's

            $AND = ($counter >= 2) ? " AND" : null;

            // Append to existing WHERE 
            // statement

            $WHERE .= "$AND $key = '$value' ";
        }
    }
    else
    {
        // No filters passed

        $WHERE = null;
    }

    // Get WordPress formatted
    // table name

    $wp_table = $wpdb->$table;

    // Putting it all together

    $query = "SELECT $field FROM $wp_table $WHERE ";

    // Make actual DB call
    // through the built in
    // MySQL interface for
    // WordPress to at least
    // attempt to remain
    // compatible with mainline

    return $wpdb->get_results($query);
}

除非有人有更干净的解决方案,否则我只想打我自己的wpdb电话。谢谢你的帮助。如果我错了,请纠正我,但这似乎会影响加载插件后get_pages()的所有使用?如果是真的,我们宁愿不这样做,以防其他插件出于任何原因需要使用get_页面。对。在这种情况下,您需要在调用get_pages()之前直接添加过滤器。之后,您可能需要调用remove_filter('get_pages','get_pages_filter');也