Get latest post link on wordpress
-
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
-
You could use get_posts to grab the post’s permalink and then add that link to whatever is generating your current nav menu.
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
i want it with a text link for example “LATEST POST” and i want it to add it on the navigation menu
<?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(); ?>
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?
@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.
ooh.. i’m using custom menu… its the only way to have the menu as i want to be
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>
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)
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.
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>
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?
its on the top of the other links.. just take a look
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.
this is the whole nav section on header.php
- The topic ‘Get latest post link on wordpress’ is closed to new replies.