• Resolved tlw22

    (@tlw22)


    Hi there,

    In my sidebar I have a piece of code that searches for the first attached image in a post and then displays it next to the post title, however it doesn’t look that great when no images are attached. So my question is, is there any way to assign an image to the posts automatically? So the end result would be the “Latest Posts” section and a default image next to each post title (until the user attaches an image- where it would then get replaced). Hope this makes sense.

    Here’s the code for the latest posts:

    <?
    query_posts('showposts=3'); // recent 3 posts
      while(have_posts()) :
        the_post();
        ?><div class="my-post"><?php
        global $post;
    	if(preg_match_all('@wp-image-([0-9]+)@i', $post->post_content, $ids)){
          foreach($ids[1] as $id){
            echo wp_get_attachment_image($id, array(32/* width */,32 /* height */), false, array('class' => 'my-image'));
            // break; // if this loop breaks, only one image is shown
    	  }
    	}
        echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
        ?></div><?php
      endwhile;
    wp_reset_query();
    ?>

    Cheers

    Link: http://chrs.biz/theme1/

Viewing 14 replies - 1 through 14 (of 14 total)
  • <?php
    query_posts('showposts=3'); // recent 3 posts
    while(have_posts()) :
    the_post();?>
    <div class="my-post">
    <?php global $post;
    if(preg_match_all('@wp-image-([0-9]+)@i', $post->post_content, $ids)){
        foreach($ids[1] as $id) {
            if( wp_get_attachment_image($id, array(32/* width */,32 /* height */), false, array('class' => 'my-image')) != '' ) echo wp_get_attachment_image($id, array(32/* width */,32 /* height */), false, array('class' => 'my-image'));
            else echo '<img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" width="xx" height="yy" alt="" />'
            // break; // if this loop breaks, only one image is shown
        }
    }
    echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
    ?></div>
    <?php endwhile;
    wp_reset_query();
    ?>
    Thread Starter tlw22

    (@tlw22)

    Thanks, however it’s thrown up a syntax error. Any idea how to fix this?

    Missing semi-colon on the end of else echo '<img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" width="xx" height="yy" alt="" />';

    Thread Starter tlw22

    (@tlw22)

    Nope, it still shows the syntax error? Any other ideas? Thanks

    Thread Starter tlw22

    (@tlw22)

    I’ve now eliminated the error however the picture doesn’t show as default next to the post name :/

    Do you have an image file called default.jpg in your theme’s images folder?

    Thread Starter tlw22

    (@tlw22)

    Nope, so I instead changed the link to: <img src="http://chrs.biz/theme1/wp-content/themes/twentyten/images/wordpress.png" width="32px" height="32px" alt="" /> Not too sure why it’s not working, guess I really need to learn php!

    <img src="<?php bloginfo('template_directory'); ?>/images/wordpress.png" width="32" height="32" alt="" /> would be the correct syntax.

    Thread Starter tlw22

    (@tlw22)

    When entering the above code the syntax breaks and displays an error? It states: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/iappleg1/public_html/chrs.biz/theme1/wp-content/themes/twentyten/sidebar.php on line 26 Thanks

    Drop a copy of the file into the WordPress pastebin and post the pastebin url here. Perhaps someone will be able to spot the problem and suggest a solution.

    Thread Starter tlw22

    (@tlw22)

    Ok, thanks for all your help! πŸ™‚ http://wordpress.pastebin.com/vzjkhRRH

    You have commented out line 26 twice:

    // break; // if this loop breaks, only one image is shown

    Try:

    // break;
    // if this loop breaks, only one image is shown
    Thread Starter tlw22

    (@tlw22)

    Nope, no luck still

    Thread Starter tlw22

    (@tlw22)

    Just browsed the web and found a similar piece of code. In short, I’ve mixed the two pieces of code together and to anyone who would like to know, here is the solution:
    Place wherever:

    <?php
    query_posts('showposts=3'); // recent 3 posts
    while(have_posts()) :
    the_post();?>
    <div class="my-post">
    <img src="<?php get_post_thumbnail(); ?>" alt="<?php the_title(); ?>" class="front-list_thumbnail" width="32" height="32"/><?
    echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
    ?></div>
    <?php endwhile;
    wp_reset_query();
    ?>

    Place in functions.php:

    function get_post_thumbnail() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
    if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', false);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $thumb=wp_get_attachment_thumb_url($num);
    print $thumb;
    else:
    print "http://www.chrs.biz/theme1/wp-content/themes/twentyten/images/wordpress.png";  /* link for default image */
    endif;
    }

    Thanks for all your help esmi πŸ™‚ Hope this piece of code can help someone, enjoy!

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Auto-assign Image?’ is closed to new replies.