函数文档

get_links_list()

💡 云策文档标注

概述

get_links_list() 是一个已弃用的 WordPress 函数,用于按分类输出链接列表,生成嵌套的 HTML 无序列表。自 2.1.0 版本起,建议使用 wp_list_bookmarks() 替代。

关键要点

  • 函数已弃用:自 WordPress 2.1.0 起,应使用 wp_list_bookmarks() 替代。
  • 功能:基于 $wpdb->linkcategories 设置,按分类输出所有链接的嵌套无序列表。
  • 参数:$order 字符串,可选 'name' 或 'id',用于指定分类排序方式。
  • 内部实现:使用 get_categories() 获取链接分类,并通过 get_links() 输出每个分类下的链接。
  • Hook:提供 apply_filters('link_category', $cat_name) 用于过滤分类名称。

代码示例

function get_links_list($order = 'name') {
    _deprecated_function( __FUNCTION__, '2.1.0', 'wp_list_bookmarks()' );

    $order = strtolower($order);

    // Handle link category sorting.
    $direction = 'ASC';
    if ( str_starts_with( $order, '_' ) ) {
        $direction = 'DESC';
        $order = substr($order,1);
    }

    if ( !isset($direction) )
        $direction = '';

    $cats = get_categories(array('type' => 'link', 'orderby' => $order, 'order' => $direction, 'hierarchical' => 0));

    // Display each category.
    if ( $cats ) {
        foreach ( (array) $cats as $cat ) {
            // Handle each category.

            // Display the category name.
            echo '  term_id . '" class="linkcat">' . apply_filters('link_category', $cat->name ) . "\n\t\n";
            // Call get_links() with all the appropriate params.
            get_links($cat->term_id, '', "", "\n", true, 'name', false);

            // Close the last category.
            echo "\n\t\n\n";
        }
    }
}

注意事项

  • 此函数已弃用,新代码中应避免使用,改用 wp_list_bookmarks() 以确保兼容性和维护性。
  • 函数内部使用 _deprecated_function() 标记弃用,调用时会触发相关通知。
  • 排序参数 $order 支持前缀 '_' 表示降序(例如 '_name' 表示按名称降序)。

📄 原文内容

Output entire list of links by category.

Description

Output a list of all links, listed by category, using the settings in $wpdb->linkcategories and output it as a nested HTML unordered list.

See also

Parameters

$orderstringrequired
Sort link categories by 'name' or 'id'

Source

function get_links_list($order = 'name') {
	_deprecated_function( __FUNCTION__, '2.1.0', 'wp_list_bookmarks()' );

	$order = strtolower($order);

	// Handle link category sorting.
	$direction = 'ASC';
	if ( str_starts_with( $order, '_' ) ) {
		$direction = 'DESC';
		$order = substr($order,1);
	}

	if ( !isset($direction) )
		$direction = '';

	$cats = get_categories(array('type' => 'link', 'orderby' => $order, 'order' => $direction, 'hierarchical' => 0));

	// Display each category.
	if ( $cats ) {
		foreach ( (array) $cats as $cat ) {
			// Handle each category.

			// Display the category name.
			echo '  <li id="linkcat-' . $cat->term_id . '" class="linkcat"><h2>' . apply_filters('link_category', $cat->name ) . "</h2>nt<ul>n";
			// Call get_links() with all the appropriate params.
			get_links($cat->term_id, '<li>', "</li>", "n", true, 'name', false);

			// Close the last category.
			echo "nt</ul>n</li>n";
		}
	}
}

Hooks

apply_filters( ‘link_category’, string $cat_name )

Filters the category name.

Changelog

Version Description
2.1.0 Deprecated. Use wp_list_bookmarks()
1.0.1 Introduced.