Grow your CSS skills. Land your dream job.

Get the URL from the image I inserted in the HTML Editor.

  • # August 20, 2012 at 9:00 pm

    I am using the Attachments Plugin (http://wordpress.org/extend/plugins/attachments) to use for a slideshow-like effect.

    Result: Process: 1 2 3

    Now what I’m trying to do is make it so the image I insert in the HTML editor (post_content) will link to 1 and the attachments from the plugin will apply to the rest. However, depending on the number of attachments I have, should reflect on the numbers outputted.

    Example (Let’s say I have 4 attachments)
    Process: 1 (post_content image) 2 3 4 5 (attachment images)

    Here is the code I have so far that reflects the attachments. What I need help with is how to display the URL of the post_content image to apply to 1.


    < ?php
    if( function_exists( 'attachments_get_attachments' ) )
    {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>


    < ?php endif; ?>
    < ?php } ?>
    # August 20, 2012 at 10:02 pm

    I believe I have solved this. Not exactly sure if it will work overall since I only tested on one page. Perhaps more advanced WP developers can chime in.

    Here is the following code:


    < ?php
    if( function_exists( 'attachments_get_attachments' ) )
    {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>
      Process:
      < ?php
      if ( has_post_thumbnail()) {
      $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
      echo '
    • ';
      echo '1
      ';
      }
      ?>
      < ?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    • ">< ?php echo $i+2; ?>

    • < ?php endfor; ?>


    < ?php endif; ?>
    < ?php } ?>
    # August 20, 2012 at 10:10 pm

    And it doesn’t work. Without noticing the code right away, it uses the post_thubmnail, not the actual full image which is what I need.

    # August 20, 2012 at 11:06 pm

    Not a wordpress guy, but try this:

    < ?php
    if( function_exists( 'attachments_get_attachments' ) )
    {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>
      Process:
      < ?php
      if ( has_post_thumbnail()) {
      $large_image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
      echo '
    • ';
      echo '1
      ';
      }
      ?>
      < ?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    • ">< ?php echo $i+2; ?>

    • < ?php endfor; ?>


    < ?php endif; ?>
    < ?php } ?>
    # August 20, 2012 at 11:43 pm

    @wolfcry911 What that does is give the permalink to the post not the image file (which is what I need). The following code below is what I believe to be the issue however I don’t know how to call the image I inserted inside the editor.

    $large_image_url = wp_get_attachment_url( get_post_thumbnail_id('full') );
    # August 21, 2012 at 2:10 pm

    In case anyone doesn’t understand. ALL I need is to get the URL of the image I inserted into the HTML editor in the backend. I believe in order to get that, I need to use wp_get_attachment_src however I’m not sure how to go about getting the ID dynamically.

    End result:

    # August 21, 2012 at 5:19 pm

    Using a function, I believe I have solved the issue.

    I placed this code in my functions.php file that allows me to grab the very first image in a post.

    // CREATES A FUNCTION TO GRAB FIRST IMAGE OF A POST
    function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('//i', $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
    }
    return $first_img;
    }

    From there I can echo the URL using the following

    < ?php echo catch_that_image(); ?>

    The end result would look like this

    < ?php
    $meta = $custom_metabox->the_meta('description', TRUE);
    if (!empty($meta)):
    echo '

    '.$meta.'

    '; ?>
    < ?php endif; ?>


    < ?php if( function_exists( 'attachments_get_attachments' ) ) {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>



    < ?php endif; ?> < ?php } ?>
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".