Archived topic
custom taxonomy for images
23 replies · Started by Ernst Wilhelm on October 14, 2022
Hi. I have quite a lot of custom taxonomies on images and heading to work with the attachment.php (https://generatepress.com/forums/topic/template-for-attachments-image/). I want to display corresponding images as with the get_the_term_list as for posts. An image/attachment category is named 'attachment_category' a link looks like '.../attachment_category/attachment-slug/' or similar.
Is there an idea to get this done?
Many Thanks for your input.
Hi there,
is it possible to see what you have so far ? Might help me better understand the requirement and the method.
Here's a link to my website. Have a look at the bottom of the screen https://ernst-wilhelm-grueter.info/vogelfotografie/amsel-drossel-fink-star/attachment/amsel-2/#main
functions.php pretty close to the template mentioned here https://generatepress.com/forums/topic/template-for-attachments-image/
And here's the question regarding the featured image for the attachment.php https://generatepress.com/forums/topic/attachment-php-featured-image/#post-2373443
[update] The idea is to show the attached image as a featured image in any blog like view where the attachment could appear but not for the attachment itself to avoid doublication.
Hook Element
<footer class="entry-meta">
<span class="entry-meta-icon icon-format-<?php echo esc_attr( 'image' ); ?>"></span>
<?php
global $post;
$metadata = wp_get_attachment_metadata();
printf( __( 'This photo appears in <b><a href="%4$s" title="View %5$s" rel="gallery">%6$s</a></b> at <a href="%1$s" title="Link to full-size image">%2$s × %3$s</a><br>', 'portfolioplus' ),
esc_url( wp_get_attachment_url() ),
$metadata['width'],
$metadata['height'],
esc_url( get_permalink( $post->post_parent ) ),
esc_attr( strip_tags( get_the_title( $post->post_parent ) ) ),
get_the_title( $post->post_parent )
);
edit_post_link( __( 'Edit', 'portfolioplus' ), '<span class="edit-link">', '</span>' );
echo get_the_term_list( $post->ID, 'category', '<br>-1: ', ', ' );
echo get_the_term_list( $post->ID, 'attachment_tag', '<br>0: ', ', ' );
echo get_the_term_list( $post->ID, 'adfs_habitat_eng', '<br>1: ', ', ' );
echo get_the_term_list( $post->ID, 'adfs_habitat_weit', '<br>2: ', ', ' );
echo get_the_term_list( $post->ID, 'adfs_region', '<br>3: ', ', ' );
echo get_the_term_list( $post->ID, 'adfs_staat', '<br>4: ', ', ' );
echo get_the_term_list( $post->ID, 'adfs_systematik', '<br>5: ', ', ' );
$suchkriterium = get_the_title( $post->post_parent );
echo "<p style='font-size:80%'><H3>Bilder zu " . $suchkriterium . "</H3>";
echo ", ext. Links
<a href=https://www.brodowski-fotografie.de/beobachtungen/" . strtolower($suchkriterium) . ".html>brodowski-fotografie</a>,
<a href=https://www.nabu.de/tiere-und-pflanzen/voegel/portraets/" . strtolower($suchkriterium) . ">Nabu</a>,
<a href=https://www.lbv.de/ratgeber/naturwissen/artenportraits/detail/" . strtolower($suchkriterium) . ">LBV</a>,
<a href=https://de.wikipedia.org/wiki/" . strtolower($suchkriterium) . ">Wikipedia</a>,
<a href=https://xeno-canto.org/explore?query=" . strtolower($suchkriterium) . "+cnt%3A%22%3Dgermany%22&dir=0&order=en>Vogelstimmen auf xeno-cant</a>";
echo "</p>";
?>
</footer>
If i visit this URL, which is the attachment category archive:
https://ernst-wilhelm-grueter.info/attachment_category/drosseln/
I see that the tax-attachment_category archive template is being loaded.
But if i view the HTML i see that its loading a dynamic-content-template - so do you have a Block element Content Template set for that archive ?
Yep. I've removed the archive assignment. The system now gives message nothing found.
If you go to your custom taxonomy, i assume it shows a list of attqchement in each term. What happens if you View one of those terms ?
Sorry it is the same. Shall I change the theme, deactivate all the plugins?
Been doing some reading up on this.
Try adding this PHP snippet:
add_action( 'pre_get_posts', 'get_all_posts_attachment' , 10, 1);
function get_all_posts_attachment( $query ) {
if( is_post_type_archive('attachment') && 'attachment' == $query->get('post_type') )
{
$query->set( 'post_status', 'inherit' );
}
return $query;
}
Then check one of the archives
David.
Many thanks for your effort. Unfortunately the if statement is not working. No change in showing nothing found.
A solution based on pure WordPress is very much appreciated, but for my functional requirements I have a self-developed workaround based on attachment.php as outlined inn Alex and Tom's conversation, an own page, url param, and media library assistant plugin. So don't worry if you want to resolve the ticket.
If you may have a look https://ernst-wilhelm-grueter.info/vogel/amsel-drossel-fink-star/attachment/amsel-4/#main
After adding the snippet here:
https://generatepress.com/forums/topic/custom-taxonomy-for-images/#post-2376042
Can you go to Settings > Permalinks and click save changes this will regenerate the permalinks and may fix the issue.
Nope.
I put the php snippet into functions.php not in any archive template. Correct?
Correct
It seems that the following code is going to work attachment_tag/nahrung/. I found it on the support pages from the media library assistant plugin nothing-found-on-tag-category-pages
I need to add it for each custom tax as far as I can see, but this is not an issue.
add_action('parse_query', 'ew_attachments_in_archives');
function ew_attachments_in_archives() {
global $wp_query;
// Note that is_tax() returns false on category archives and tag archives
// Use is_category() and is_tag() respectively when checking for category and tag archives
if (is_tax('attachment_category') OR is_tag() OR is_category()) {
$wp_query->query_vars['post_type'] = array( 'attachment', 'page', 'post' );
$wp_query->query_vars['post_status'] = array( 'publish', 'inherit' );
return $wp_query;
}
}
What I would like to get now is to display the image on this archive page. Can yu give me some guidance in this?
In every case many thanks for helping me. Ernst Wilhelm
Aah ok, i see, i think my code failed as it didn't target the correct taxonomy.
To get the image attachment, you can try hooking this in after ( or before ) the content title for that archive:
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
change thumbnail to the image attachment size you want displayed