Speaking about backoffice here.

I usually add columns and filters to the backoffice for certain custom post types. No problem here. BUt what if I would simply like to change something on the content of the default columns? For example, how to change the color of the post titles?
And say, only some titles on a list, for example based on the terms the post belongs to?

I would just need an action like

manage_{$post->post_type}_posts_custom_column

But for regular/default columns. The above action only reach custom columns.

2 Answers
2

CSS is the answer. If you look at the HTML code of each row (<tr>), you will see that it has classes that include post ID, post status, post tags, categories, and so on. So, you can easily apply CSS rules based on that classes and based on post tags.

For example, this is a row in one of my site:

<tr id="post-24392" class="post-24392 type-post status-publish format-standard has-post-thumbnail hentry category-ciencia-y-tecnologia tag-distancia tag-longitud tag-metro tag-sistema-internacional-de-unidades alternate iedit author-other level-0">
            <th scope="row" class="check-column">
                            <label class="screen-reader-text" for="cb-select-24392">Elige ¿?</label>
            <input id="cb-select-24392" type="checkbox" name="post[]" value="24392">
            <div class="locked-indicator"></div>
                        </th>
        <td class="post-title page-title column-title">
        Here the title

If I want to change the color of the title if the post belongs to “metro” tag:

.tag-metro .post-title {
    color: red;
}

You can put that CSS in a file and enqueue it in admin.

If terms are from a custom taxonomy, you can hook post_class to add the classes based on the custom taxonomy:

add_filter( 'post_class', function( $classes, $class, $ID ) {

    $taxonomy = 'my-custom-taxonomy';

    $terms = get_the_terms( (int) $ID, $taxonomy );

    if( !empty( $terms ) ) {

        foreach( (array) $terms as $order => $term ) {

            if( !in_array( $term->slug, $classes ) ) {

                $classes[] = $term->slug;

            }

        }

    }

    return $classes;

}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *