Forums

[resolved] Apply DIV to every item in loop? (20 posts)

  1. vinmassaro
    Member
    Posted 4 years ago #

    Hello,

    I am a newbie with WordPress loops so hopefully someone can help. I am trying to pull news items from WordPress from a specific category and get EACH news item to be wrapped in a div. Right now, it pulls all the news items and places them inside one large div. Thanks in advance! Here is my code:

    <?php if ( query_posts('cat=1') ) { ?>
       <div class="newsitem">
       <?php } ?>
       <?php query_posts('cat=1'); ?>
       <p><?php the_time('F jS, Y'); ?></p>
          <?php while (have_posts()) : the_post(); ?>
          <h4><a>"><?php the_title(); ?></a></h4>
          <?php the_content(); ?>
    
    <?php endwhile;?></div>
  2. sasch9r
    Member
    Posted 4 years ago #

    Doesn't your code work?

  3. vinmassaro
    Member
    Posted 4 years ago #

    Right now, it is taking all of the posts in that category and placing them in one big div. I want to place each POST in a div, so that I can set a hover color for the class and each news item will change color, not just the big container div that it is creating now.

  4. mrwolf
    Member
    Posted 4 years ago #

    i've really got no idea but my best guess would be a div tag after line 6

  5. sasch9r
    Member
    Posted 4 years ago #

    Now i got it. :D Just Play your </div> before closing the While-Loop.

    <?php if ( query_posts('cat=1') ) { ?>
    <div class="newsitem">
      <?php } ?>
      <?php query_posts('cat=1'); ?>
      <?php the_time('F jS, Y'); ?>
    
      <?php while (have_posts()) : the_post(); ?>
      <h4><?php the_title(); ?></h4>
      <?php the_content(); ?>
    </div>
    <?php endwhile;?>

    I removed the "> behind <h4> too.

  6. vinmassaro
    Member
    Posted 4 years ago #

    That worked, kinda. Now, it is putting ONLY the first news item in a div, but the news items after that do not have their own div.

  7. sasch9r
    Member
    Posted 4 years ago #

    Ok, found the problem. Have a look at this code.

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post">
    	<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	<div class="date"><?php the_time('j. M') ?> — <?php the_time('G:i') ?></div>
    	<?php the_content('‹mehr›'); ?>
    </div>
    <?php endwhile; ?>

    My code is a little bit different to yours. Maybe you can change yours.

  8. vinmassaro
    Member
    Posted 4 years ago #

    Sach94r,
    Thanks -- I am having trouble getting that to load without an error, though.

    I used the query function since I want to have more than 1 news feed on the same page, from different categories. If I delete every other instance of my code and use yours, I still get this error: Parse error: parse error, unexpected $

  9. sasch9r
    Member
    Posted 4 years ago #

    Umm, could you show me your blog? Maybe it will be easyer to understand for me.

  10. vinmassaro
    Member
    Posted 4 years ago #

    Sure, what I am doing is calling the blog items from a non-blog page. Right now, you can see I am calling the code in the right column, and for simplicity sake, I placed a red border on that class. You can see that only the first news item from that category is getting wrapped in the div. I want each of those posts to be in their own div so I can apply a hover to the class and get them to change background color when moused over. I also just noticed the date is only being applied to the first news item.

    URL:
    http://www.ny4ronpaul.com/dev/2col.php

  11. vinmassaro
    Member
    Posted 4 years ago #

    Sach9r,

    With your help and playing with the code, I think I have figured it out!

  12. sasch9r
    Member
    Posted 4 years ago #

    I see, great :)

  13. vinmassaro
    Member
    Posted 4 years ago #

    Another thing, :)

    How do I get it to print the date only once per day? Right now, it prints the date for every post. Ideally, it would print the date once then list each post for that day underneath.

  14. sasch9r
    Member
    Posted 4 years ago #

    Try this:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_date('d M y') ?>
    <div class="post">
    	<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	<?php the_content('‹mehr›'); ?>
    </div>
    <?php endwhile; ?>

    Just use the_date() instead of the_time()

  15. vinmassaro
    Member
    Posted 4 years ago #

    using the_date instead of the_time worked perfectly. Is there a list of all of the options to shape how the output looks? I also want to limit the number of characters that are output per post.

  16. doodlebee
    Member
    Posted 4 years ago #

    I also want to limit the number of characters that are output per post.

    You might want to be careful with that. If you limit the characters, what if you place a link in a certain spot, and then the middle of the link is cut off? It'll ruin your site's formatting.

    You're probably better off using the_excerpt, or using the <!--more--> tag.

  17. vinmassaro
    Member
    Posted 4 years ago #

    So would you recommend that an excerpt is filled in for every news item and I include the excerpt? How does the excerpt get output?

  18. doodlebee
    Member
    Posted 4 years ago #

    just replace "the_content" with "the_excerpt" in your index.php file.

  19. vinmassaro
    Member
    Posted 4 years ago #

    Thanks guys, using "the_excerpt" works well. How do I limit the number of posts that are loaded to a specific number?

  20. sasch9r
    Member
    Posted 4 years ago #

    If you use my script, you're able to change the value in the admin panel

    Options -> Reading -> Blog Pages

    Edit: Ok, can't find another way. Sorry.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags