Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. hi so i have done some modification to my index.php. on the frontpage the code i am using is the index.php from your website, where you select one post from each category to be displayed on the frontpage. what i am trying to do is pull the first image from a post and display it in the spoiler. i have a code that works but for some odd reason the pictures that are displayed are not the corresponding ones with the post. it's like it's almost offset by one post. if you would like i can give you a test account to see my theme since i am using theme test drive.

    and here is the code i have

    <!--- code to display one post per category only, put the category ID below in the order you wish and as many you want --->
    <?php $display_categories = array(53,56,57,55,54); foreach ($display_categories as $frontcats) { ?>
    <?php $catposts = get_posts("showposts=1&cat=$frontcats"); foreach($catposts as $post) : setup_postdata($post); ?>
    <div class="clearfloat">
    <h3 class=cat_title>"><?php $this_category = get_category($frontcats); ?><?php echo $this_category->cat_name; ?> »</h3>
    <div class="title">" rel="bookmark"><?php the_title(); ?></div>
    <div class="meta">[<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]</div>
    <div class="spoiler">
    <?php $values = get_post_custom_values("Image");if (isset($values[0])) { ?>
    " rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
    $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=150&h=150&zc=1&q=100"
    alt="<?php the_title(); ?>" class="left" width="150px" height="150px" />

    <?php }
    else {
    $id =$post->ID;
    $the_content = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
    $home = get_option('home');
    $pattern = '!<img.*?src="'.$home.'(.*?)"!';
    $image_src = $matches['1'][0];
    preg_match_all($pattern, $the_content, $matches);?>
    <?php if($image_src != '') { ?><img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $image_src; ?>&w=150&h=150&zc=1&q=100"
    alt="<?php the_title(); ?>" class="left" width="150px" height="150px" />
    <?php } ?>
    <?php } ?>
    <?php the_excerpt(); ?>
    </div>
    </div>
    <?php endforeach; // end of post ?>
    <?php } ?>
    <!--- end of code to display one post per category only --->
    <?php } // end of FIRST PAGE ?>

    thnx for your help and hope im clear on my question

    Posted 1 year ago #
  2. actually this code inside the else statement looks well, if there is showing even an image it's good.

    It should show the first image of the post you just display, to debug I sometimes just echo the values I set to see if they match. Sometimes it might be just the styling moving text and images to wrong places.

    You could try this:

    ....
    else {
    $id =$post->ID;
    echo $id;
    $the_content = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
    $home = get_option('home');
    $pattern = '!<img.*?src="'.$home.'(.*?)"!';
    $image_src = $matches['1'][0];
    echo $image_src;
    preg_match_all ...

    a reset of the query with wp_reset_query() could help too, try it here:

    <?php endforeach; wp_reset_query(); // end of post ?>
    <?php } ?>

    or here:

    <?php endforeach; // end of post ?>
    <?php } wp_reset_query(); ?>

    (I would have to try too..)

    Posted 1 year ago #
  3. hmm i reset the query but that didnt make a difference. when i echo the value of the id it was correct id with the post but when i echo the image_src it was not the right one for the post. it was a image for a different post in the loop...im still messing with it though because i have the same code 2 other times and it works good but something just about this one isnt displaying correctly...

    Posted 1 year ago #
  4. looking at the code again I think you get the matches after preg_match so you could try this by putting "$image_src = ..." below preg_match_all

    The line with the comment ($image_src = '';) might help, try without this line first.

    ...
    $the_content = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
    $home = get_option('home');
    $pattern = '!<img.*?src="'.$home.'(.*?)"!';
    $image_src = ''; // to clear any value if there is left from another query
    preg_match_all($pattern, $the_content, $matches);?>
    $image_src = $matches['1'][0];
    <?php if($image_src != '') ...

    is your pattern in the post really with the full path? I think timthumb prefers to get a path like

    http: / / yoursite/wp-content/themes/arthemia/scripts/timthumb.php?src=/wp-content/images/...

    without http after ...ripts/timthumb.php?src=
    So echo $image_src; before ..php if($image_src.. to check the value you give to timthumb
    Last not least, tell us if you get something working...

    Posted 1 year ago #
  5. hey! i've been away from the net for a while...moving to a different city! but thnx so much it worked after i moved the img src below the matches..thnk u thnk u thnk u!

    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.

Join us! or log in (lost password?):