WordPress的功能可以修改吗?

发布于 2024-11-09 04:53:51 字数 692 浏览 0 评论 0原文

我在 WordPress 中使用 wp_list_bookmarks() 函数来获取已在 WordPress 后端添加的链接,例如 blogroll 等。

我的问题是我需要以某种方式显示列表,以便我可以将它分成几列,每列大约有 5 个链接。

默认情况下,它只是一个大列表。有没有一种方法可以使用 PHP 来改变此函数发布链接的方式,以便我运行一个计数器,然后在 5 个链接后关闭当前列表并为下一列启动一个新列表?

我基本上需要这样的东西:

<ul class="column-1">
    <li>link1</li>
    <li>link2</li>
    <li>link3</li>
    <li>link4</li>
    <li>link5</li>
</ul>
<ul class="column-2">
    <li>link6</li>
    <li>link7</li>
    <li>link8</li>
    <li>link9</li>
    <li>link10</li>
</ul>
// etc...

提前谢谢你。

I'm using the wp_list_bookmarks() function in wordpress to get links that have been added in the Wordpress back-end, things like blogroll etc.

My problem is that I need the list to come out in a certain way so that I can put it into columns, with about 5 links in each column.

By default it's just one big list. Is there a way that I can use PHP to alter the way this function posts the links so that I run a counter, and then after 5 links close off the current list and start a new one for the next column?

I basically need something like this:

<ul class="column-1">
    <li>link1</li>
    <li>link2</li>
    <li>link3</li>
    <li>link4</li>
    <li>link5</li>
</ul>
<ul class="column-2">
    <li>link6</li>
    <li>link7</li>
    <li>link8</li>
    <li>link9</li>
    <li>link10</li>
</ul>
// etc...

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在我安 2024-11-16 04:53:51

您可以使用 Wordpress 的 get_bookmarks() 函数,然后根据您的喜好定制输出。请参阅此处的示例:

<?php

$bookmarks = get_bookmarks( array(
            'orderby'        => 'name',
            'order'          => 'ASC',
            'category_name'  => 'Related Sites'
                      ));

// Loop through each bookmark and print formatted output
$column = 1;
$counter = 1;

echo "<ul class='column-$column'>";
foreach ( $bookmarks as $bm ) { 
    if ($counter >= 5)
    {
        $column++;
        $counter = 1; // reset the counter

        echo "</ul><ul class='column-$column'>";

    }
    printf( '<li><a class="relatedlink" href="%s">%s</a></li>', $bm->link_url, __($bm->link_name) );
    $counter++;
}
echo "</ul>";

?>

参考:http://codex.wordpress.org/Template_Tags/get_bookmarks#Examples

You can use Wordpress's get_bookmarks() function and then tailor the output to your liking. See here for an example:

<?php

$bookmarks = get_bookmarks( array(
            'orderby'        => 'name',
            'order'          => 'ASC',
            'category_name'  => 'Related Sites'
                      ));

// Loop through each bookmark and print formatted output
$column = 1;
$counter = 1;

echo "<ul class='column-$column'>";
foreach ( $bookmarks as $bm ) { 
    if ($counter >= 5)
    {
        $column++;
        $counter = 1; // reset the counter

        echo "</ul><ul class='column-$column'>";

    }
    printf( '<li><a class="relatedlink" href="%s">%s</a></li>', $bm->link_url, __($bm->link_name) );
    $counter++;
}
echo "</ul>";

?>

Reference: http://codex.wordpress.org/Template_Tags/get_bookmarks#Examples

美羊羊 2024-11-16 04:53:51

根据wordpress的插件API

除了上述的钩子(操作和过滤器)之外,插件修改 WordPress 行为的另一种方法是覆盖 WordPress 函数。事实上,WordPress 打算让插件重新定义一小部分功能。

不幸的是,wp_list_bookmarks 不在他们的被视为“可插入”的函数列表中。

也许您可以在 API 中找到其他适合您需求的东西?

According to wordpress's Plugin API:

Besides the hooks (actions and filters) described above, another way for a plugin to modify WordPress's behavior is to override WordPress functions. In fact, there is a small set of functions WordPress intends for plugins to redefine.

Unfortunately wp_list_bookmarks isn't on their list of functions that are considered "pluggable".

Maybe you can find something else that will fit your needs in the API?

好菇凉咱不稀罕他 2024-11-16 04:53:51

您可以从插件(您需要编写)连接到 wp_list_bookmarks()。然后,该插件可以操作来自 wp_list_bookmarks() 函数的正常 HTML,并将操作后的 HTML 返回给它,以便正常回显或返回。

这个想法是创建(并激活!)一个简单的插件,例如:

<?php
/*
Plugin Name: Diggersworld Bookmarks
Plugin URI: https://stackoverflow.com/questions/6089883/can-wordpress-functions-be-modified
Description: Example plugin to customise output from wp_list_bookmarks
Version: 0.0
*/

function diggersworld_list_bookmarks($html)
{
    // Do your transformation here
    return '<p>Mmm pie.</p>';
}

add_action('wp_list_bookmarks', 'diggersworld_list_bookmarks');

?>    

将其另存为插件文件夹中的文件,然后从 Wordpress 管理面板中激活它。您将看到原来放置书签的位置出现了 Mmm pie. 文本。修改该示例插件以使用您喜欢的任何方法返回所需的 HTML。

由于这与 wp_list_bookmarks() 函数内的挂钩相关联,因此您的模板应正常调用 wp_list_bookmarks()

当然,插件函数的主体可以使用类似于 thesocialgeek 的答案中的代码

You can hook into wp_list_bookmarks() from a plugin (that you need to write). That plugin can then manipulate the normal HTML that comes from the wp_list_bookmarks() function and return that manipulated HTML back to it, to be echoed or returned as normal.

The idea is to create (and activate!) a simple plugin like:

<?php
/*
Plugin Name: Diggersworld Bookmarks
Plugin URI: https://stackoverflow.com/questions/6089883/can-wordpress-functions-be-modified
Description: Example plugin to customise output from wp_list_bookmarks
Version: 0.0
*/

function diggersworld_list_bookmarks($html)
{
    // Do your transformation here
    return '<p>Mmm pie.</p>';
}

add_action('wp_list_bookmarks', 'diggersworld_list_bookmarks');

?>    

Save this as a file in your plugins folder, and activate it from within the Wordpress admin panel. You will see that where your bookmarks used to be placed, the Mmm pie. text is there instead. Modify that sample plugin to return your desired HTML using whatever method you like.

Since this is tying into a hook inside the wp_list_bookmarks() function, your templates should call wp_list_bookmarks() as normal.

Of course, the body of the plugin function could use code similar to that in thesocialgeek's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文