• Resolved vinmassaro

    (@vinmassaro)


    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>
Viewing 15 replies - 1 through 15 (of 19 total)
  • Doesn’t your code work?

    Thread Starter vinmassaro

    (@vinmassaro)

    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.

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

    Now i got it. ?? 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.

    Thread Starter vinmassaro

    (@vinmassaro)

    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.

    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.

    Thread Starter vinmassaro

    (@vinmassaro)

    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 $

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

    Thread Starter vinmassaro

    (@vinmassaro)

    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:
    https://www.ny4ronpaul.com/dev/2col.php

    Thread Starter vinmassaro

    (@vinmassaro)

    Sach9r,

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

    I see, great ??

    Thread Starter vinmassaro

    (@vinmassaro)

    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.

    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()

    Thread Starter vinmassaro

    (@vinmassaro)

    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.

    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.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Apply DIV to every item in loop?’ is closed to new replies.