Display Item Filter

The display item filter is called just before an item is inserted into an index page.  Using this filter allows you to affect the what shows up on the web page when this item is displayed.  Note, you cannot use this filter to influence the sorting of the index. That is what the item filter is for.

Note: this filter will only be called if the Group items with the same heading… option is not selected.

To add a filter for all indexes in your blog use the following code:

add_filter('azindex_display_item', 'my_display_item_filter');

Then add the display item filter function with the name you specified:

function my_display_item_filter($item) {
    /* My item modifying code here */
    return $item;
}

The $item parameter is an associative array containing all the information about the item being loaded into the index.  It has the following keys names:

Key Name Description
id Post id - the id of the post linked to by this item
head The item’s heading string, as it will be displayed in the index
subhead The item’s subheading string, as it will be displayed in the index
desc The item’s description string, as it will be displayed in the index
sort-head The item’s sorted heading string – used only when sorting the index
sort-subhead The item’s sorted subheading string – used only when sorting the index
sort-desc The item’s sorted description string – used only when sorting the index
key Category/tag id of the item – only if category/tag is selected as the heading
output Empty when the filter function is called, but if you add your own HTML here, then it will be used for displaying the item.

The important values for this filter are:

  • head, subhead, and desc, which you can modify to change the content of the item display
  • id, which you can use in WordPress functions to query the information you need about the object
  • output, which you can use if you want to completely replace the HTML used for the item.  See below for more information

Note: modifying the other values in the array does not affect anything.

You can change the output in two different ways:

  1. by modifying the head, subhead, and desc values individually.  Each value will be wrapped in a <span> element before being output.  You can insert any HTML you like into the values, including <img> tags for thumbnails.  The permalink for the item will be added to the item by the plugin after the filter returns.  If you don’t want the default ‘-‘ character between the head and subhead just type a space character into the Heading separator field in the settings page.
  2. by putting HTML to the output value.  If you add anything at all to output then it will be used instead of the usual output generated by the plugin.  You can use this field to create index items with exactly the look and feel you need, but remember, you will be responsible for adding the link to the post to the HTML, otherwise there will be nowhere for readers to click to get to your posts.  The item will be wrapped in a list item tag <li> when it is added to the index page,

If you use this filter to alter the way the items are displayed, you may well need to modify the default CSS stylesheet used by your index.  In the settings page, simply select the following option:

  • Use customized stylesheets for the index

and you can make the changes you need. The filter always uses the first set of CSS styles on the settings page, the one labeled Stylesheet for ungrouped index items.

Note: Remember, if you are using the output value to return your own HTML, the .head, .subhead, and .desc CSS classes will not be used unless you use them in your HTML.

More Display Item Filter Parameters

If you only want to use the item filter with one specific index, you need to know the id of the index your filter function is being called for.  You can do this by specifying that you want your filter to receive all the available parameters, which is 2:

add_filter('azindex_display_item', 'my_item_filter', 10, 2);

Note: the third parameter in the add_filter() call, 10, is the default filter priority, and must be present so you can specify the number of parameters you want to be passed to your filter.

Now you can modify the filter function to receive the index id, so you can add a test in your code to see if you need to filter this item.

function my_display_item_filter($item, $idindex) {
    if ($idindex = INDEX_ID) {
        /* My item modifying code here */
    }
    return $item;
}

Note: in future versions for AZIndex, $idindex may be a string, as well as a number.

Example: Movie Stars Thumbnail Index

As an example of what you can do with this filter, this is how I created the thumbnail index of movie stars in the gallery section:

  1. Find a place to put a small amount of PHP code—I prefer to put it in another plugin (one I made myself) but you can also add it to somewhere like your functions.php file in your current theme’s directory.
  2. Add the following code to your PHP file:
    /* This call hooks the filter function into the plugin. Since
     * I only want to modify the content of one of my indexes, I
     * specify that I need both available parameters passed
     * to my filter function.
     */
    add_filter('azindex_display_item', 'my_add_thumb', 10, 2);
    
    /**
     * This is the filter function.  It simply puts the post's  thumbnail into
     * the head field and moves the original contents of the head field
     * (the title of the post) into the subhead field.
     *
     * @param $item array contains information about the item to be displayed
     * @param $idindex the id of the index being displayed
     */
    function my_add_thumb($item, $idindex) {
    
        // Only use this filter to process one specific index.
        if ($idindex == 18) {
    
            // I stored the thumbnail for each post in a custom value called "Thumb"
            // If you are using a thumbnail plugin, you will need to replace this line
            // with the correct function to obtain the URL of the thumbnail image.
            // (Note: you're on your own with that!)
            $thumb = get_post_custom_values('Thumb', $item['id']);
    
            // Move the title of the post from the heading to the subheading.
            // I wrap the text inside a "div" element so that I can control exactly
            // where the text appears next to the thumbnail.  You may not have
            // to do this, depending on what you need.
            $item['subhead'] = '<div style="height:50px;padding-top:30px;">'.$item['head'].'</div>';
    
            // Wrap the thumbnail image inside an "img" tag and put it in the heading.
            $item['head'] = "<img src='{$thumb[0]}'/>";
        }
        // Return the modified item back to the plugin.
        return $item;
    }

    Please be aware that this code will not work as-is on your blog. You will need to make changes to use the correct index id, to fetch the correct thumbnail URL, and to adjust the positioning of the image and text, to get the index looking the way you want.

  3. In the index settings:
    • Make sure the Group items with the same heading… option is not selected
    • Select the Use customized stylesheets for the index option
    • Make the following change to the first line of the Stylesheet for ungrouped index items:
      .azindex .head {float:left;padding-right:10px}

      As mentioned above, the stylesheet changes required depend on the output you are generating for the item.

  4. Save the settings and the file with the filter code, and go have a look at your new index.

Additional Notes

There are some other things to be aware of when you are using a display item filter:

  • If you are only interested in altering the appearance of the index items (and not the way the index is sorted), then use this filter over the azindex_item filter as it will be faster, since it will be called much less often.
  • The filter will not be called if you have the Group headings… option set, so the grouping capabilities of the plugin will not be available when you use this filter.
  • The strings in head, subhead, and desc use the UTF-8 encoding, where some characters (like those unique to certain languages) are more than one byte long.  If you are going to manipulate text that contains these multi-byte characters, you should use the PHP multi-byte string library functions to do so.
  • Unless you have a good reason, it’s best not to use the contents of sort-head, sort-subhead, or sort-desc in this filter.
  • Other keys may be added to the $item array in future releases, so the number of keys and their order may change over time.

No comments yet to

  • I was suggested this blog through my cousin. I am not positive whether this submit is written by means of him as nobody else recognize such special about my problem. You’re amazing! Thank you!

  • I was just searching for this information for a while. After six hours of continuous Googleing, at last I got it in your web site. I wonder what’s the lack of Google strategy that don’t rank this kind of informative websites in top of the list. Generally the top websites are full of garbage.

  • Haha, I’m only buying it because it won’t get boring super-fast.

  • I have been using the AZIndex for a while to do an alphabetical post listing and it has been great! Now I want to do a chronological index listing with the same look. Is there a way to do a sort by Published date so that it looks the same way?

    Here is my current alpha page index: http://strongdisciple.com/alphabetical-listing

    Thanks again for this great plugin!
    Dan

  • Youre so cool! I dont suppose Ive read anything like this before. So good to seek out any person with some unique thoughts on this subject. realy thank you for beginning this up. this website is something that is needed on the internet, someone with a bit originality. helpful job for bringing something new to the web!

  • I’ve learn several just right stuff here. Definitely value bookmarking for revisiting. I wonder how a lot effort you place to create this sort of great informative website.

  • naturally like your web site but you have to test the spelling on quite a few of your posts. Several of them are rife with spelling issues and I to find it very bothersome to tell the truth on the other hand I’ll definitely come back again.

  • excellent points altogether, you just received a emblem new reader. What could you recommend in regards to your post that you simply made some days in the past? Any certain?

  • Youre so cool! I dont suppose Ive read anything like this before. So nice to seek out somebody with some authentic ideas on this subject. realy thanks for beginning this up. this website is one thing that is needed on the web, someone with slightly originality. helpful job for bringing something new to the web!

  • Hi Mike…

    Big fan of the plugin. We use it everywhere.

    Not sure where to make suggestions so I thought I would try here.

    We have a directory and it would be really useful for us to be able to use the category as a sub head.

    This way we could have a page that show all categories grouped by a custom field.

    Would love to know if this will make the list.

    Jeff

  • There is clearly a bunch to realize about this. I think you made some good points in features also.

  • This website online is mostly a walk-via for all of the information you wished about this and didn’t know who to ask. Glimpse here, and also you’ll positively discover it.

  • Wonderful paintings! This is the type of info that are supposed to be shared around the web. Disgrace on the seek engines for no longer positioning this post higher! Come on over and seek advice from my site . Thank you =)

  • I found your weblog site on google and verify a couple of of your early posts. Continue to keep up the superb operate. I just additional up your RSS feed to my MSN Information Reader. Seeking forward to reading extra from you afterward!…

  • Well I definitely liked reading it. This subject provided by you is very helpful for accurate planning.

  • using AZIndex 0.8.1 on wordpress 2.7 that was installed a few years ago by original site developer. The website is for non-profit organization, run by volunteers.

    I have no knowledge/experience with this plugin and has been working up till today, when noticed site had issue.

    AZIndex Plug Error 101 – required database table wp_bfafaz_indexes – donot exist
    deactivate then reactivate AZIndex to correct this problem

    I deactivated then reactivated plugin- got same Error

    Suggestions on how this issue can be resolved would be much appreciated.

  • I think other web site proprietors should take this web site as an model, very clean and magnificent user friendly style and design, as well as the content. You’re an expert in this topic!

  • Michael Grant said:

    This plugin does in fact seem to work on wordpress 3.x.

    It would be fantastic if this plugin could be updated to be able to use custom types and taxonomies. I can see by playing with the code that it wouldn’t be too difficult.

    Is there any active development being done on this? Can I make some code contributions?

  • I precisely needed to thank you very much yet again. I do not know the things I would have made to happen without those solutions contributed by you directly on that concern. It became an absolute challenging circumstance in my position, nevertheless being able to view the specialized way you treated the issue made me to leap over happiness. Extremely happy for the assistance and even expect you are aware of an amazing job you are getting into training other individuals with the aid of your web page. Probably you’ve never encountered all of us.

  • I appreciate, result in I discovered just what I used to be having a look for. You have ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye