WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Latest News on static hompage (12 posts)

  1. jbdev
    Member
    Posted 3 years ago #

    Hi

    I can't see this anywhere else, apologies If I have missed it.

    I have set up a static page as a homepage (with no sidepanel). I would like to show a 'latest news' section on the home page only, the latest news page would be a news blog page located somehwere else on the site.

    Ideally the 'latest news' section on the homepage would show a title a few lines of copy and a read more button linking to the exact article or the news blog page.

    Thanks in advance

  2. chinmoy
    Member
    Posted 3 years ago #

    <?php
     $my_latestNews = new WP_Query('category_name=news&showpost=5');
     while($my_latestNews->have_posts()): $my_latestNews->the_post();
    ?>
       <h2><a href="<?php the_permalink()?>" rel="Bookmark" ><?php the_title()?></a></h2>
       <p><?php the_content("Read more..."); ?></p>
    <?php
     endwhile;
    ?>

    Try this.

  3. jbdev
    Member
    Posted 3 years ago #

    sorry to be dense but how do I get this to show only on the home page if I insert in to page.php?

  4. chinmoy
    Member
    Posted 3 years ago #

    use this

    <?php if(is_home()) :
    $my_latestNews = new WP_Query('category_name=news&showpost=5');
     while($my_latestNews->have_posts()): $my_latestNews->the_post();
    ?>
       <h2><a href="<?php the_permalink()?>" rel="Bookmark" ><?php the_title()?></a></h2>
       <p><?php the_content("Read more..."); ?></p>
    <?php
     endwhile;
    endif;
    ?>

    or use is_front_page() function

  5. jbdev
    Member
    Posted 3 years ago #

    hmmm
    Have added in the code and this is bringing up nothing, please can you show where i need to enter site specific information into the code.

    Sorry very new to wordpress

  6. chinmoy
    Member
    Posted 3 years ago #

    do you create a "news" category for latest News?

    Put the code before the while(have_posts()): line

  7. jbdev
    Member
    Posted 3 years ago #

    Ah progress

    I have now have the relevant category showing on every page using the following

    <h1>Latest News</h1>
    <ul class="latest-news">
    <?php
     global $post;
     $myposts = get_posts('numberposts=3');
     foreach($myposts as $post) :
     ?>
    
    <li><span class="post-info"><?php the_title(); ?><?php the_time('F jS, Y'); ?></span><a>"></a></li>
     <?php endforeach; ?>

    2 questions
    I would like to put this in a div and show the div only on the homepage how?
    I would like to have 3 or 4 lines of each post showing as a teaser, how?

  8. jbdev
    Member
    Posted 3 years ago #

    Progress

    I now have the 'latestnews' categories showing only on the front page using the follwing,

    <?php if( is_front_page() ) { ?>
    <div class="latestnews">
    <h1>Latest News</h1>
    <ul class="latest-news">
    <?php
     global $post;
     $myposts = get_posts('numberposts=3');
     foreach($myposts as $post) :
     ?>
        <li><span class="post-info"><?php the_title(); ?><br/><?php the_time('F jS, Y'); ?></span><a href="<?php the_permalink(); ?>"></a></li>
     <?php endforeach; ?>
    </ul>
    </div>
    	<?php } ?>

    I would like to show 3 lines of copy from each post, any ideas?

  9. jbdev
    Member
    Posted 3 years ago #

    Thanks for your help chinmoy29 I now the following code working

    <?php if( is_front_page() ) { ?>					<!--This controls the small boxes div on the homepage-->
    <div class="coverboxes">
    <?php $posts = get_posts( "category=4&numberposts=2" ); ?>
    <?php if( $posts ) : ?>
         <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
           <div class="post">
    
              <p class="post-info-home"><span class="coverbox_header"><?php the_title(); ?></span></p>
              <div class="post-title-home">
           </div>
           <div class="entry">
              <?php the_excerpt(); ?>
              <em><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">Read more</a></em>
           </div>
         </div>
       <?php endforeach; ?>
    <?php endif; ?>
    </div>
    <?php } ?>

    This code does not show the image in the post is there any way of pulling this into the homepage above the excerpt. Help great;y appreciated

  10. rayv1988
    Member
    Posted 3 years ago #

    I tried<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>' ) ); ?> but it doesn't work.

  11. chinmoy
    Member
    Posted 3 years ago #

    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>' ) ); ?>

    Its have no error. Did you put this <!--more--> in your post? If you don't use that tag, the 'Continue reading>>' would not be came. Go to EDITOR through admin and put the "more" tag.

  12. rayv1988
    Member
    Posted 3 years ago #

    Place `function new_excerpt_length($length) {
    return 10;
    }
    add_filter('excerpt_length', 'new_excerpt_length');` in your functions.php

Topic Closed

This topic has been closed to new replies.

About this Topic