开发者

Can Wordpress functions be modified?

I'm using the wp_list_bookmarks() function in wordpress to get links that have been added in the Wordpress back-end, things like b开发者_JAVA百科logroll 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.


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


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜