Displaying Tags in Text Format
-
Hi David, hope your having a nice time away.
I would like to display the attachment_tag, in text format (linkable), for each image on my ‘attachment-image.php’ page. I found a plugin (https://www.ads-software.com/plugins/media-tags/) that does that, but that would be double work.
https://www.ads-software.com/extend/plugins/media-library-assistant/
-
Mike,
The trip’s going great, thanks. With regard to your question, here is a slightly modified version of the code MLA uses to generate the text links for taxonomy columns in the Media/Assistant submenu table:
$tax_object = get_taxonomy( 'attachment_tag' ); $terms = wp_get_object_terms( $item->ID, 'attachment_tag' ); if ( !is_wp_error( $terms ) ) { if ( empty( $terms ) ) return 'none'; $list = array(); foreach ( $terms as $term ) { $term_name = esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'category', 'display' ) ); $list[ ] = sprintf( '<a href="%1$s" title="Filter by %2$s">%3$s</a>', esc_url( add_query_arg( array( 'page' => MLA::ADMIN_PAGE_SLUG, 'mla-tax' => 'attachment_tag', 'mla-term' => $term->slug, 'heading_suffix' => urlencode( $tax_object->label . ': ' . $term->name ) ), 'upload.php' ) ), $term_name, $term_name ); } // foreach $term return join( ', ', $list ); } // if !is_wp_error else { return 'not supported'; }
The first two lines call WordPress API functions to get information about the taxonomy and the list of terms for the specific attachment. You would replace
$item->ID
with something likeget_the_ID()
.The
foreach
loop builds an array of text links, one for each term associated with the current attachment. In my case, the links look like this:https://mladev/wp-admin/upload.php?page=mla-menu&mla-tax=attachment_tag&mla-term=keword1&heading_suffix=Att.+Tags%3A+keword1
The
add_query_arg
function builds the URL and then adds the other items (e.g., page) to it. You don’t say specifically what you want to links to get to, but I imagine you will need modify this bit in some significant way. If you give me more details I can help with that. Theesc_url
function converts punctuation, spaces, etc. to URL-encoded values.After the loop this code returns a comma-delimited string to the caller. Your code would probably use
echo
in place ofreturn
.That’s the basic outline of a solution. If you have any trouble adapting it to your specific requirements let me know more and I can give a better response. you might also find some inspiration in this recent support topic along similar lines:
The link to your example didn’t work.
Here’s what i would like: Alpenglow on Half Dome. I’m using the ‘Media Tags‘ plugin, but I would like to see if I could use the attachment_tag tag instead. Each tag link will show all images with that particular tag.
I saw the recent thread, but unfortunately I don’t know enough code to modify my application.
Mike,
I’m not exactly sure what “link to your example” refers to. The “in my case … ” example just shows what the code above it generates for each tag. It takes you back to the Media/Assistant subscreen with a table view filtered by the tag value. Clearly, you will want something different; arguments like “page” and “heading_sufffix” don’t apply.
I had a look at your Alpenglow page (terrific image!) and see the “Media-Tags” and “Tags” links towards the bottom. It looks like the Media-Tags links take you to a page with a “gallery” of images filtered by the tag (as you said). I’m not sure how that page is generated, but I can imagine doing something like it with an
[mla_gallery]
shortcode.To replicate the link behind the “Media-Tags” items, you would code:
$list[ ] = sprintf( '<a href="%1$s/%2$s" title="Gallery for %3$s">%4$s</a>', site_url('/media-tags'), $term->slug, $term_name, $term_name );
I’d have to see the PHP source behind
content-attachment.php
to know what arguments you’d need to power it. I can do more after I get home next week. Play with this and let me know how it goes.The
site_url('/media-tags')
part is from the plugin that I’m not going to use, so I’ll have to remove that.I’ll play around with it, but I’ll post the ”content-attachment.php’ page here:
<?php /** * Custom 'content-attachment.php' for the 'Media-Tags' Plugin * I just wrapped the 'the_excerpt();' with a #media-tags-excerpts div so * I can hide the "extra thumbnail" from the media tag plugin. */ ?> <article id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>"> <?php if ( is_singular( get_post_type() ) ) { ?> <header class="entry-header"> <?php echo apply_atomic_shortcode( 'entry_title', '[entry-title]' ); ?> </header><!-- .entry-header --> <div class="entry-content"> <?php hybrid_attachment(); // Function for handling non-image attachments. ?> <?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'chun' ) ); ?> <?php wp_link_pages( array( 'before' => '<p class="page-links">' . '<span class="before">' . __( 'Pages:', 'chun' ) . '</span>', 'after' => '</p>' ) ); ?> </div><!-- .entry-content --> <footer class="entry-footer"> <?php echo apply_atomic_shortcode( 'entry_meta', '<div class="entry-meta">' . __( 'Published on [entry-published] [entry-edit-link before="| "]', 'chun' ) . '</div>' ); ?> </footer><!-- .entry-footer --> <?php } else { ?> <header class="entry-header"> <?php echo apply_atomic_shortcode( 'entry_title', '[entry-title]' ); ?> </header><!-- .entry-header --> <div class="entry-summary"> <?php if ( current_theme_supports( 'get-the-image' ) ) get_the_image( array( 'size' => 'post-thumbnail' ) ); ?> <div id="media-tags-excerpt"><?php the_excerpt(); ?></div> <?php wp_link_pages( array( 'before' => '<p class="page-links">' . '<span class="before">' . __( 'Pages:', 'chun' ) . '</span>', 'after' => '</p>' ) ); ?> </div><!-- .entry-summary --> <?php } ?> </article><!-- .hentry -->
I’m getting closer, but I have yet to display the actual tag entries. I created a copy of the
content-attachment.php
and called itcontent-attachment_tag.php
. It displays the title of the tag, but no content. I changed this on myattachment-image.php
page.
$list[ ] = sprintf( '<a href="%1$s/%2$s" title="Gallery for %3$s">%4$s</a>', ('attachment_tag'), $term->slug, $term_name, $term_name );
Not sure where to go from here. ??
You’re close. The links you’re building contain the taxonomy name and the term you want to filter by. WordPress parses the link, extracts the taxonomy and term, then uses it to query the database and pass the results to your
content-attachment_tag.php
page. Unfortunately, the query WordPress sets up is looking for posts and pages, not attachments, so it returns nothing; the empty page.There are two ways to proceed. First, you can re-query the database, changing the
post_type
parameter, and then display the results using the existing logic incontent-attachment_tag.php
. I’m not sure what that would look like.Second, you can create a new page with an
[mla_gallery]
shortcode filtered by the tag value. That would give you more control over the format of the display. You might find some ideas in this earlier support topic:building-an-archive-gallery-with-mla
I think we’ve also exchanged some e-mails about previous/next links on a similar page.
Either way would work, but the details are more than I can figure out while on the road. I’ll be off the grid for the next few days and then back home later in the week. Thanks for your patience while I’m out of touch.
I saw the other thread. I’ll play around with it, but I doubt I’ll get any further than this. Have a safe trip.
Thank you for waiting until I had a chance to dig into this issue more deeply.
Your
attachment-image.php
page looks fine; the links you’re building in$list[ ]
contain the taxonomy and term you want to filter by. WordPress parses the URL, recognizes the taxonomy and term and uses the Template Hierarchy to display the “Archive page” for the taxonomy/term combination. You are seeing an empty Archive page because WordPress queries for posts and pages, not attachments, to get the archive contents.Modifying your
content-attachment_category.php
file will customize the display of the archive, but the database query happens before this template part is called. What you need is a new file,taxonomy-attachment_category.php
to modify the database query. Many themes have anarchive.php
file you can model this on, but your theme (Chun) does not. In you case, I think you can base yourtaxonomy-attachment_category.php
file on theindex.php
file in the parent Chun theme.As I said in an earlier post on this topic, there are two ways to proceed: 1) re-query the database and use your
content-attachment_category.php
template, or 2) create a new page with an[mla_gallery]
for the taxonomy/term combination.Re-query and use
content-attachment_category.php
Add the following code after the
<?php get_header(); // Loads the header.php template. ?>
and before the<div id="content">
lines in theindex.php
(orarchive.php
) file:<?php global $wp_query; $args = array_merge( $wp_query->query_vars, array( 'post_type' => 'attachment', 'post_status' => 'inherit' ) ); query_posts( $args ); ?>
Save the new file as
taxonomy-attachment_category.php
and run a test. You should see all the images associated with the term, each one formatted separately according to yourcontent-attachment_category.php
template.Create a new
[mla_gallery]
pageIf you don’t want to format and display each image separately, you can replace the WordPress “Loop” code entirely, substituting an
[mla_gallery]
. Create thetaxonomy-attachment_category.php
as described above, including the newquery_posts
code. Then, replace everything from (and including) the<?php while ( have_posts() ) : the_post(); ?>
line to (and including) the<?php endwhile; ?>
line with this code:<?php echo do_shortcode( sprintf( '[mla_gallery %1$s="%2$s" ]', $wp_query->query_vars['taxonomy'], $wp_query->query_vars['term'] ) ); ?>
That should give you a page with a gallery containing the images associated with the selected term.
I think one of the above alternatives will get you what you want. Play around with them and let me know if you need more specific help. You can change
attachment_category
toattachment_tag
to display “Att. Tag” results, by the way.Good timing David, I was just thinking about this! I tried both, and they both work. I’ll have to do some additional styling on the “Create a New Page” one if I decided to use that. Thanks for the help!
I noticed the pagination doesn’t work using either method.
Thanks for the update; I’m glad the alternatives I outlined worked for you!
Pagination is a separate topic/issue and I am addressing it in a separate support topic:
I am going to mark this topic Resolved and continue working on pagination under the above-referenced topic.
The “Paginated Revisited” support topic now contains a solution for this case. Thanks for your patience while I worked out the specifics.
- The topic ‘Displaying Tags in Text Format’ is closed to new replies.