Tuesday, January 31, 2023
HomeWordPress Developmenttemplates - on attachment.php, tips on how to show earlier and subsequent...

templates – on attachment.php, tips on how to show earlier and subsequent attachment hyperlinks that comply with the identical order as a customized WP Question


I’ve a media web page that makes use of a customized WP_Query to return all thumbnails of media library attachments (no matter publish father or mother) with a customized taxonomy of media_visibility that has the time period of printed. That is working as anticipated on the media web page. It is utilizing my template file page-media.php. Related code pattern:

  // what number of media thumbnails to indicate at first.
  $thumbnails_to_show = 42;

  //Shield in opposition to arbitrary paged values
  $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

        // question media
        $media_query_args = array(
            'post_type'   => 'attachment',
            'post_status' => 'any',
            'order' => 'DESC',
            'orderby' => 'date',
            'paged' => $paged,
            'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'media_visibility',
                        'subject' => 'slug',
                        'phrases' => 'printed',
                    ),
            ),
            'posts_per_page' => $thumbnails_to_show,
        );
        $media_query = new WP_Query ($media_query_args);

        $thumbnails = array();

        if ( $media_query->have_posts() ) : whereas ( $media_query->have_posts() ) : $media_query->the_post();
                // retailer thumbnails in array
                $thumbnails[] = wp_get_attachment_link( get_the_ID(), 'thumbnail', true );
            endwhile;

        endif; // finish of media loop

When one clicks on a hyperlink on a thumbnail picture, it hundreds a web page exhibiting the attachment and former / subsequent hyperlinks utilizing my attachment.php file. Related code pattern:

    whereas ( have_posts() ) :
        the_post();

        get_template_part( 'template-parts/content material', get_post_type() );

        previous_post_link();
        next_post_link();

        if ( comments_open() || get_comments_number() ) :
            comments_template();
        endif;

    endwhile; // Finish of the loop.

Right here, there appears to be a problem with the order of posts / the totally different loops. On some (perhaps all) attachment pages, previous_post_link() is displaying a hyperlink to the identical attachment proven reasonably than the earlier one, and next_post_link() will not be displaying something.

current behavior of attachment page

What I need on the attachment web page is to have earlier and subsequent textual content hyperlinks that web page by means of the attachment pages in the identical order they seem on the media web page and solely together with these attachments proven on the media web page.

I attempted another approaches like the_post_navigation() and get_previous_image_link() / get_next_image_link() however didn’t get what I used to be attempting to realize right here. I additionally tried changing the loop on attachment.php with the identical customized WP_Query that I used on page-media.php however this ended up all the time displaying the primary attachment returned within the question, reasonably than the attachment specified within the URL.

View the media web page on staging web site

(With a view to view it, log in as
username: stackexchange
password: visitor)

This custom-made theme is predicated on underscores.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments