WordPress.org

Support

Support » How-To and Troubleshooting » How to alter attachment URLs (of images) inside a certain post?

How to alter attachment URLs (of images) inside a certain post?

  • Inside a post (which functions as a submenu) I have multiple images. Now I want to alter the attachment URLs of these images.

    How to?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I now have this, but it doesn’t work. Can somebody help me?

    <?php if(is_paged()){ ?>
    
    <?php
    $args = array('order'=>'ASC', 'post_type'=>'attachment', 'post_parent'=>$post->ID, 'post_mime_type'=>'image', 'post_status'=>null, 'numberposts'=>-1,);
    $attachments = get_posts($args);
    if($attachments){
    foreach($attachments as $attachment){
    echo wp_get_attachment_link($attachment->ID . '&paged=' . $paged, 'thumbnail', true, false);
    }
    };
    the_content();
    ?>
    
    <?php } else { ?>
    <?php the_content(); ?>
    <?php }; ?>

    I solved it! Instead of using the wp_get_attachment_link() function I used two new variables ($atturl and $attlink) which I combined later on with some simple HTML.

    Problem now is that the post_parent argument doesn’t work correctly. When ‘null’ it does show my attachments (multiple thumbnails), but when I use $post->ID (or the ‘include’ argument with as value the numeric post ID), it shows blank. What is the problem?

    Can anybody help?

    <?php
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => 25,
    'post_parent' => $post->ID,
    'order' => 'ASC'
    );
    
    $attachments = get_posts($args);
    if($attachments){
    foreach($attachments as $attachment){
    $atturl = wp_get_attachment_thumb_url($attachment->ID);
    $attlink = get_attachment_link($attachment->ID) . '&paged=' . $paged;
    
    echo '<a href="' . $attlink . '"><img src="' . $atturl . '"></a>';
    
    }
    };
    ?>

    Cannot get it to work, bleh!
    Below my complete code. Question above remains the same.

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query = new WP_Query();
    $wp_query -> query('category_name=photo&posts_per_page=1&order=ASC&paged=' . $paged);
    ?>
    
    <?php while($wp_query -> have_posts()) : $wp_query -> the_post(); ?>
    
    <?php if(is_paged()){ ?>
    
    <?php
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => 25,
    'post_parent' => $post->ID,
    'order' => 'ASC'
    );
    
    $attachments = get_posts($args);
    if($attachments){
    foreach($attachments as $attachment){
    $atturl = wp_get_attachment_thumb_url($attachment->ID);
    $attlink = get_attachment_link($attachment->ID) . '&paged=' . $paged;
    echo '<a href="' . $attlink . '"><img src="' . $atturl . '"></a>';
    }
    };
    ?>
    
    <?php } else { ?>
    <?php the_content(); ?>
    <?php }; ?>
    
    <?php endwhile; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to alter attachment URLs (of images) inside a certain post?’ is closed to new replies.