• Resolved jasonjasonjason

    (@jasonjasonjason)


    good evening,

    i am working on a way to display a small image as an icon for each post. this would normally be handled using a post’s “featured image”, but i am using the featured image as the background for the post, and i want the icon to be a different picture.

    my best idea is to simply upload a second picture, with the same name as the featured image but with an additional “_icon” appended to the name. for example “japan.jpg” and “japan_icon.jpg” would be the featured image and the icon.

    is there a way to fetch the name of the featured image, without including the file extension? or a way to delete the last three characters of the returned feature image URL?

    here’s my loop for setting the featured image as the background :

    <?php
       $args1=array(
       'showposts'=>1,
       'post_type' => 'page'
       );
    
    $my_query = new WP_Query($args1);
      if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
      <?php $imgID = get_post_thumbnail_id($post->ID);
            $featuredImage = wp_get_attachment_image_src($imgID, 'full');
    	$imgURL = $featuredImage[0] ;?>
    
    <div class="first"
         style="background-image: url(<?php echo $imgURL ?>);">  
    
    </div>
    
    <?php endwhile;} ?>

    it would be easy to append “_icon.jpg” to the returned value of $imgURL, and give me my matched icon.

    any suggestions would be appreciated ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • You could just use the PHP function str_replace():

    $imgIconURL = str_replace('.jpg', '_icon.jpg', $imgURL);

    Thread Starter jasonjasonjason

    (@jasonjasonjason)

    that worked like a charm!

    you are a splendid and wonderful creature, linux4me2, may your days be bright and full of donuts ??

    LOL Thanks, I like donuts.

    Thread Starter jasonjasonjason

    (@jasonjasonjason)

    the code is breaking in a very strange way…

    it works perfectly for the first two results, and then does something strange for the third (and final) result.

    when i view the source for the resulting page, it has appeneded “_thumb” twice onto the last timage. not “_thumb.jpg_thumb.jpg”, but “_thumb_thumb.jpg”

    my first assumption was i had incorrectly named the last picture. but checking the upload folder in WP, it shows all three test files are named identically :/

    <?php      $args2=array(
          'showposts'=>3,
         'post_type' => 'page'   );
    
    $my_query = new WP_Query($args2);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
      <?php $imgID = get_post_thumbnail_id($post->ID);
            $featuredImage = wp_get_attachment_image_src($imgID, 'full' );
            $imgURL = $featuredImage[0];
            $imgIconURL = str_replace('.jpg', '_thumb.jpg', $imgURL);  ?>
    
    <div class="flex-item_2">
      <div class="tape">
       <img src="<?php echo $imgIconURL ?>"> <!-- Title Section content -->
       <span class="writing"> <?php echo substr(get_the_title(), 0,20); ?> </span>
      </div>
    </div> <!-- div "flex-item" -->
    
    <?php endwhile;} ?>

    so, i basically end up with “sample1_thumb.jpg” “sample2_thumb.jpg” and “sample3_thumb_thumb.jpg”. even though my files are all named “sampleX_thumb.jpg”.

    i don’t see why it would change on the third iteration of the loop??

    EDIT :
    i deleted all traces of the third entry, and added it again. this time it worked fine. must have been some glitch in the matrix ??

    on the plus side, i just learned about the neccessity of including the functions.php and how to include theme support for post-thumbnails. sheesh.

    I’m glad you got it figured out, because I couldn’t see anything wrong with your code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘how to get the partial filename of a post's featured image? or remove extension?’ is closed to new replies.