WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. MD84
    Member
    Posted 4 years ago #

    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?

  2. MD84
    Member
    Posted 4 years ago #

    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 }; ?>
  3. MD84
    Member
    Posted 4 years ago #

    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>';
    
    }
    };
    ?>
  4. MD84
    Member
    Posted 4 years ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic