简体   繁体   中英

How can I add custom filter to a wordpress blog?

Im using simple fields in a wordpress site, so users can "tag" diferents post in admin.

Now, in the frontend view, I want to show a dropdown with the different options (distinct tags) that are avaible, so user can filter the post.

I know I need to modify some php code, and I found the exactly file where I need to touch, but I don't know even where to start.

My question is if I can add some custom php code, calling a mysql query and then add that recordset to fill the dropdown, or there is an more easy way to do this?

Any idea?

Probably you are looking for add_filter() function in WordPress.

Since your question is too broad I just provide you with some examples that could help to get you started. First, add a function similar to the one below in your functions.php file,

function add_custom_tags($tags) {

    /** you can do a query to get these tags from database */
    $extra_tags = array(
        'tag1',
        'tag2',
        'tag3'
    );

    $tags = array_merge($extra_tags, $tags);
    return $tags;
}
add_filter('dropdown_tags', 'add_custom_tags');

where, dropdown_tags is the name of the filter and add_custom_tags is the function to be run when the filter is applied.

Then, you can use this filter in your dropdown function like this,

if(has_filter('dropdown_tags')) {
    $tags = apply_filters('dropdown_tags', $tags);
}

where $tags are current fields in your dropdown.

You can find good examples for add_filter function here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM