WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get latest post link on wordpress (22 posts)

  1. hambos22
    Member
    Posted 2 years ago #

    I have that site : http://ougk.gr and i want on the navigation to have a link which points to the latest post of a specific category (full post with comments etc). How can i achieve that? thank you

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    You could use get_posts to grab the post's permalink and then add that link to whatever is generating your current nav menu.

  3. hambos22
    Member
    Posted 2 years ago #

    here is my navigation section on my header.php

    <nav>
    				<?php if (function_exists('wp_nav_menu')) {
    					wp_nav_menu(array('theme_location' => 'main-nav' , 'fallback_cb' => 'default_main_nav' , 'container'  => '' , 'menu_id' => 'main-nav' , 'menu_class' => 'main-nav'));
    				} else {
    					default_main_nav();
    				} ?>
    				<!--/main-nav -->
    			</nav>

    How can i add it? i dont know php

  4. hambos22
    Member
    Posted 2 years ago #

    i want it with a text link for example "LATEST POST" and i want it to add it on the navigation menu

  5. James Edmonston
    Member
    Posted 2 years ago #

    <?php
    query_posts('cat=YOUR CATEGORY NUMBER&posts_per_page=1');
    if(have_posts());
    while(have_posts()) :
    the_post();
    ?>
    <li>
        <a href="<?php echo get_permalink(); ?>">LATEST POST</a>
    </li>
    <?php
    endwhile;
    wp_reset_query();
    ?>
  6. hambos22
    Member
    Posted 2 years ago #

    thank you all! i added it and look what happened http://ougk.gr

    here is the code

    [Code moderated as per the Forum Rules. Please use the pastebin]

    maybe i need to make a php file and point a url to it?

  7. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    @James Edmonston: No - not query_posts! That will over-write the main (primary) Loop! This is a secondary Loop - which is why I specifically suggested get_posts.

    @hambos22: You will need to start looking at the main_nav() function (probably in functions.php). This also assumes that you are not using a custom menu.

  8. hambos22
    Member
    Posted 2 years ago #

    ooh.. i'm using custom menu... its the only way to have the menu as i want to be

  9. James Edmonston
    Member
    Posted 2 years ago #

    Since when does query_posts() overwrite the main loop (genuine question)?

    Could you not do:

    <ul class="your-class-name">
        <?php
        wp_list_pages('title_li=&depth=1&exclude=THE,IDs,OF,THE,PAGES,YOU,WANT,TO,EXCLUDE,COMMA,DELIMITED');
        query_posts('cat=YOUR CATEGORY NUMBER&posts_per_page=1');
        if(have_posts());
        while(have_posts()) :
        the_post();
        ?>
            <li>
                <a href="<?php echo get_permalink(); ?>">LATEST POST</a>
            </li>
        <?php
        endwhile;
        wp_reset_query();
        ?>
    </ul>
  10. hambos22
    Member
    Posted 2 years ago #

    here is the code that i used.. it doesnt work good.. it shows me 9 links with the title "LATEST POST"... just see at http://ougk.gr

    here is the code on paste bin (the previous one deleted from the forum system)

    http://pastebin.com/aBaUNKn4

  11. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Since when does query_posts() overwrite the main loop

    Since always (or at least as far back as I can recall).

    query_posts() is meant for altering the main loop. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered - this may or may not be the intended result

    http://codex.wordpress.org/Function_Reference/query_posts#Alters_Main_Loop

    You could use WP_Query but that seems overkill for just one link. get_posts is ideally suited for this kind of thing.

  12. James Edmonston
    Member
    Posted 2 years ago #

    Sorry, ignore my last post and use this:

    <ul class="your-class-name">
        <?php
        wp_list_pages('title_li=&depth=1&include=THE,IDs,OF,THE,PAGES,YOU,WANT,TO,INCLUDE,COMMA,DELIMITED');
        query_posts('cat=YOUR CATEGORY NUMBER&posts_per_page=1');
        if(have_posts());
        while(have_posts()) :
        the_post();
        ?>
            <li>
                <a href="<?php echo get_permalink(); ?>">LATEST POST</a>
            </li>
        <?php
        endwhile;
        wp_reset_query();
        ?>
    </ul>
  13. hambos22
    Member
    Posted 2 years ago #

    that method worked just for the link to show up one time. I cant get it to fit nice on the nav menu grrrrr

    But it worked! :) Can i add it on a php file and make a link on the menu manager to point to that php file?

  14. hambos22
    Member
    Posted 2 years ago #

    its on the top of the other links.. just take a look

  15. James Edmonston
    Member
    Posted 2 years ago #

    Are you still using your initial loop that you posted in your second post? If so, you should be replacing it with my latest post.

  16. hambos22
    Member
    Posted 2 years ago #

    this is the whole nav section on header.php

    http://pastebin.com/RcTgEA7k

  17. hambos22
    Member
    Posted 2 years ago #

    oh wait! i forgot to make the other exclude=THE,IDs,OF,THE,PAGES,YOU,WANT,TO,EXCLUDE,COMMA,DELIMITED')

  18. hambos22
    Member
    Posted 2 years ago #

    i had also submenus.. how can i add them on your code?

  19. James Edmonston
    Member
    Posted 2 years ago #

    <nav>
    	<ul id="main-nav" class="main-nav">
    	<?php
    	wp_list_pages('title_li=&include=THE,IDs,OF,THE,PAGES,YOU,WANT,TO,INCLUDE,COMMA,DELIMITED');
    	query_posts('cat=23&posts_per_page=1');
    	if(have_posts());
    	while(have_posts()) :
    	the_post();
    	?>
    		<li>
    			<a href="<?php echo get_permalink(); ?>">LATEST POST</a>
    		</li>
    	<?php
    	endwhile;
    	wp_reset_query();
    	?>
    	</ul>
    </nav>

    Try that. Obviously change where it says 'THE,IDs,OF,THE,PAGES,YOU,WANT,TO,INCLUDE,COMMA,DELIMITED' to the IDs of the pages you want to include.

    The above should also support sub-menus.

  20. hambos22
    Member
    Posted 2 years ago #

    and how can i add the submenus? sorry if i'm too strenuous

  21. James Edmonston
    Member
    Posted 2 years ago #

    If you make pages children of the pages that are included in the nav, they should automatically be added as sub-menus.

  22. hambos22
    Member
    Posted 2 years ago #

    Thank you so much! it worked! i wanted that for very long time but i couldn't find anything!

    Thank you again! :)

Topic Closed

This topic has been closed to new replies.

About this Topic