Forums

[resolved] how to make links on the header to the recent posts ? (6 posts)

  1. eguru
    Member
    Posted 1 year ago #

    Hello All,
    I am working on a website where I have links (Main Navigations) news gallery, awards, competition etc.. I would like to make these links to direct to the recent post from the each category which I have mentioned above. example if some one clicks on the NEWS link it the recent post from the NEWS category will open. I am using currently this code

    <div id="home"><?php
    global $post;
    $latest_posts = get_posts('numberposts=1');
    foreach($latest_posts as $latest) {
    	echo '<a href="'.get_permalink($latest->ID).'" class=main_menu_links>NEWS</a>';
    }
    ?></div>

    If it is only one category this can work well. What if different categories like the one which I have mentioned above? Please help me if you have any answer. Your help will be highly appreaciate.

    Thank you in advance

  2. Gisha James
    Member
    Posted 1 year ago #

    If news gallery, awards, competition etc are categories and if you are using wp_list_categories to display navigation menu, won't it link to the recent posts from that particular category...

  3. eguru
    Member
    Posted 1 year ago #

    <?php wp_list_categories( ); ?> will only list the category links..it will not show the posts. What I need is if i click on the main menus (category name) i will be ale to see the recent post in side that particular gallery. Just like we are clicking on a static websites main Links.

  4. eguru
    Member
    Posted 1 year ago #

    Please help me!!!! any body here?

  5. MAS
    Member
    Posted 1 year ago #

    <?php
    global $post;
    $cats = get_categories();
    foreach($cats as $cat){
     $myposts = get_posts('numberposts=1&category=$cat->term_id');
     foreach($myposts as $post) :
       setup_postdata($post);
       echo '<a href="'.the_permalink().'" class=main_menu_links>'.$cat->name.'</a>';
     endforeach;
    }
    ?>
  6. MJEG
    Member
    Posted 1 year ago #

    cicking on the category names form your sidebar should show the all the posts from that category

Topic Closed

This topic has been closed to new replies.

About this Topic