• 0

Wordpress homepage news


Question

Currently I have my homepage which displays the 5 most recent articles, then I have another page showing the same articles but more of them.

What I want to do is style the homepage articles independately from each other so

latest article is div 1, then 2 and so forth.

This isd the code I currently have to show the 5 latest posts


<?php
$args = array( 'numberposts' => 5 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endforeach; ?>
[/CODE]

I've heard a super-loop should work, problem is I don't really know PHP so as much help would be greatful :)

Thanks

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

You basically want to add <div id="div1/2/3">...</div> around the posts right? I haven't tested it but something like this should work:

&lt;?php

$args = array( 'numberposts' =&gt; 5 );
$lastposts = get_posts( $args );
$i = 1;
foreach($lastposts as $post)
{
  echo '&lt;div id="div' . $i . '"&gt;';
  setup_postdata($post);
  echo '&lt;h2&gt;&lt;a href="' . the_permalink(); . '"&gt;' . the_title(); . '&lt;/a&gt;&lt;/h2&gt;&lt;/div&gt;'
  $i++;
}

?&gt;

Link to comment
Share on other sites

  • 0

You basically want to add <div id="div1/2/3">...</div> around the posts right? I haven't tested it but something like this should work:

&lt;?php

$args = array( 'numberposts' =&gt; 5 );
$lastposts = get_posts( $args );
$i = 1;
foreach($lastposts as $post)
{
  echo '&lt;div id="div' . $i . '"&gt;';
  setup_postdata($post);
  echo '&lt;h2&gt;&lt;a href="' . the_permalink(); . '"&gt;' . the_title(); . '&lt;/a&gt;&lt;/h2&gt;&lt;/div&gt;'
  $i++;
}

?&gt;

Something like that would be ideal, however when I paste the code I get

"Parse error: syntax error, unexpected '.' in /home/moi/public_html/beta/wp-content/themes/toolbox/main.php on line 19

"

Link to comment
Share on other sites

  • 0

couldnt you just wrap them in a div and use nth-child:


<?php$args = array( 'numberposts' => 5 );$lastposts = get_posts( $args );foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="posts"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div><?php endforeach; ?>
[/CODE]

[CODE]
.posts:nth-child(1){
styling for first post}
.posts:nth-child(2){
styling for second post}
etc
[/CODE]

I think thats how I would do it.

Link to comment
Share on other sites

  • 0

Something like that would be ideal, however when I paste the code I get

"Parse error: syntax error, unexpected '.' in /home/moi/public_html/beta/wp-content/themes/toolbox/main.php on line 19

"

Sorry, typo...

  echo '&lt;h2&gt;&lt;a href="' . the_permalink(); . '"&gt;' . the_title(); . '&lt;/a&gt;&lt;/h2&gt;&lt;/div&gt;'

Should read:

  echo '&lt;h2&gt;&lt;a href="' . the_permalink() . '"&gt;' . the_title() . '&lt;/a&gt;&lt;/h2&gt;&lt;/div&gt;';

Link to comment
Share on other sites

  • 0

Problem with that nth-child is that it isn't really compatible with anything.

On that page it appears like it won't work for msot of IE's, some firefox's etc :(

Looking at the code it's this line which is the problem one


echo '<h2><a href="' . the_permalink(); . '">' . the_title(); . '</a></h2></div>'
[/CODE]

Hi Alex,

I've put in the amended code but the same problems pops up hmmm.

It says the problem is the . somewhere?

I feel your close though :)

Link to comment
Share on other sites

  • 0

Yes - That's working (sort of)

This is the HTML output now.


<div id="home_blogroll">
<div id="div1">http://link/cookie/cookie-test/Cookie test<h2><a href=""></a></h2></div>
<div id="div2">http://link/hawk/1/Demo Forza Motorsport 4<h2><a href=""></a></h2></div>
<div id="div3">http://link/hawk/2/2<h2><a href=""></a></h2></div>
<div id="div4">http://link/hawk/3/3<h2><a href=""></a></h2></div>
<div id="div5">http://link/hawk/4/4<h2><a href=""></a></h2></div>
</div>
[/CODE]

So for some reason it's not getting the href nor the article title.

Link to comment
Share on other sites

  • 0

Try this:

Nope messed it up.

Probably not the best way of doing it but i think this might work:


<?php
$args = array( 'numberposts' => 5 );
$lastposts = get_posts( $args );
$i = 1;
foreach($lastposts as $post){
echo '<div id="div' . $i . '">';
setup_postdata($post);
echo '<h2><a href="' ;?>
<?php the_permalink();
echo'">';?>
<?php the_title();
echo'</a></h2></div>';
$i++;
}?>
[/CODE]

Link to comment
Share on other sites

This topic is now closed to further replies.