PHP Gurus, foreach question, WP related

7 replies
Here is what I am attempting to do,

I need to grab post information ( title, excerpt and post count ) and echo that information outside of the loop.

So far, I can do this with the post count and title. I've bolded the relevant code.

<?php
$titles = array();
$tmp = $wp_query;
$wp_query = new WP_Query('posts_per_page=' .$my_admin_option['theme_index_featured_postcount']. '');
if(have_posts()) : while(have_posts()) :
the_post();
$titles[] = get_the_title($post->ID);
<img src="<?php echo $image[0]; ?>" class="attachment-post-thumbnail-600x396" title="#caption<?php echo count($titles)-1; ?>" alt="<?php the_title_attribute(); ?>" />
<?php
endwhile; endif;
$wp_query = $tmp;
?>
</div>

<?php foreach( $titles as $key => $title ) { ?>
<div id="caption<?php echo $key; ?>">
<h2><?php echo $title; ?></h2>

</div>
<?php } ?>
The image title and the following ID outside of the loop have to match. This is what connects the image with the correct post title.

Now, how do I expand the array to include additional post information? I'm trying to include the post excerpt.

I've been messing around with

$titles["title"] = get_the_title
$titles["excerpt"] = get_the_excerpt

but I can't figure it out.
#foreach #gurus #php #question
  • Profile picture of the author WRTech
    Hello there! You appear to be on the right track, and there are a couple of ways to finish up from here.

    First: I'm not a WordPress dev. These are just general suggestions, so I'll assume you have a matching get_the_excerpt function or something like it to the get_the_title function in your example.

    Now then, to go about it the way you've attempted, here's what I'd suggest.

    On the line that reads "$titles[] = get_the_title($post->ID);", change it to:

    $titles[] = array(get_the_title($post->ID), get_the_excerpt($post->ID));


    Then, in your foreach loop, make it look something like:

    <?php foreach( $titles as $key => $title ) { ?>
    <div id="caption<?php echo $key; ?>">
    <h2><?php echo $title[0]; ?></h2>
    <p><?php echo $title[1]; ?></p>
    </div>
    <?php } ?>


    Note the new array notation and the added <p> tag. This should get you what you want.

    The other way would be to create a second array called $excerpts and just make sure you add the excerpts in the same order (at the same time) as you're adding the titles. Then you can use the same $key to reference the $titles array and the $excerpts array.

    Let me know if you have any questions!

    - John
    {{ DiscussionBoard.errors[3567911].message }}
    • Profile picture of the author christopher jon
      John, worked like a charm. Exactly what I needed it to do.

      Looking at your code I see exactly what I was doing wrong. Apparently my brain isn't functioning today.

      Thanks!
      {{ DiscussionBoard.errors[3568083].message }}
      • Profile picture of the author WRTech
        Glad to be of service!

        - John
        {{ DiscussionBoard.errors[3568100].message }}
  • Profile picture of the author Happy_Balance
    Looks great, I will need to review this soon, thanks
    Signature

    Every Day Is Fun! :)

    {{ DiscussionBoard.errors[3567928].message }}
  • Profile picture of the author SteveJohnson
    I don't exactly understand what you're trying to do - your 'foreach' IS a loop - why not just use the new query object you generated and use the built-in WP functions to display the info?

    Aside from that, you should also note that the assignment of $wp_query to $tmp will not work in PHP5 because of the way objects are treated. You have to 'clone' the object:
    $tmp = clone $wp_query;

    Then when you're done, reverse it:
    $wp_query = clone $tmp;

    All in all, much easier to just use your new query object and loop through it.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3568234].message }}
  • Profile picture of the author christopher jon
    I don't exactly understand what you're trying to do - your 'foreach' IS a loop - why not just use the new query object you generated and use the built-in WP functions to display the info?
    Hacking the nivo flash slider to display post titles and excerpts which it's not designed to do.

    The way it works isn't WP loop friendly. Sometimes you have to play by another developers rules.

    But, mission accomplished. The slider now calls up posts and pages marked as featured, displays the title and the excerpt.

    to
    I removed this completely. It was sloppy code left over from me trying different things that didn't work. I'm from the Jackson Pollock school of coding.

    I really need to get down to the bookstore and pick up a php5 book. I'm running out of excuses.
    {{ DiscussionBoard.errors[3568473].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by christopher jon View Post

      The way it works isn't WP loop friendly. Sometimes you have to play by another developers rules.
      I think I see - you needed two separate lists then? The first with the images only, the next with the indexed information.

      Glad you got it to work the way you needed...and not to be argumentative, but you could have just reset the loop and echoed the needed info...
      PHP Code:
      <?php
          
      endwhile; endif; ## end of existing loop
          ## rewind the loop
          
      rewind_posts();
          while ( 
      have_posts() ): the_post(); ?>
              <div id="caption<?php echo $ post->ID?>">
                  <h2><?php echo get_the_title(); ?></h2>
                  <p><?php echo get_the_excerpt(); ?></p>
              </div>
      <?php
          
      endwhile; ?>
      There's always 26 ways to skin a cat
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[3568911].message }}

Trending Topics