Get latest post link on WordPress - php

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?

There are a few ways to do this. This one uses wp_get_recent_posts(), and prints a basic link:
<nav>
<?php
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo 'Latest Post';
}
?>
// .. other menu code ..
</nav>
Where CAT_ID is the id of the targeted category. For your situation the simple answer is to insert the link code just after the opening nav tag, as above.
To place the link somewhere else in the nav, you'll have to dive into some of the other functions called in the code you pasted. It might be a good idea to get your hands dirty..

<?php
$args = array(
'numberposts' => '1',
);
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ):
$post_id = $recent['ID'];
$post_url = get_permalink($recent['ID']);
$post_title = $recent['post_title'];
$post_content = $recent['post_content'];
$post_thumbnail = get_the_post_thumbnail($recent['ID']);
endforeach;
?>

You need ot get the title and perma link
<?php
// retrieve one post with an ID of 5
query_posts( 'cat=X&posts_per_page=1&order=DESC' );
// the Loop
while (have_posts()) : the_post();
echo "<a href='<?php the_permalink(); ?>'>";
the_title();
echo "</a>";
endwhile;
?>

Related

get and show 5 published post and show in diffrent div one by one wordpress

In WordPress, I want to show first published post to in one single div and show second published post in second single div far my 5 recently published post. How should get each published post one by one and show it in different place in page?
According to codex, but can't grab specific post
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
Not a good solution but this may solve your problem
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
$i = 0;
foreach( $recent_posts as $recent ){
echo '<li class="post".$i>' . $recent["post_title"].' </li> ';
$i++;
}
wp_reset_query();
?>
Then U will have post0,post1,2,3,4 classes you can reach all of these classes from CSS but repeating again This is not a good solution.

Wordpress loop using wp_query category_name => category+textstring

I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);

Extract title and add to post in Wordpress

I have a Wordpress site with 500+ posts. I would like to extract the title of each post, wrap it with some span class code, add the title and code to the end of each post (the same post from which it was extracted), and hide its visibility. How would I go about doing this in Wordpress/PHP?
Thanks!
EDIT: #unixmiah, would this be achieved like so?
<?php
global $post;
$args = array( 'numberposts' => 500, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li span class="name hidden"><?php the_title();?></li>
<?php endforeach; ?>
Which file do I insert this into? functions.php? Also, how do I ensure this code will run site-wide?
You can use the code below to accomplish this with wordpress internal functions to get that information in a loop.
For example
<?php
global $post;
$args = array( 'numberposts' => 500, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
your javascript span question seems vague, do you mean to hide it and show it on mouseover?

Can't separate Ascii symbol from hyperlink

I'm currently displaying my recent posts in WordPress with the below script, but I can't get the ASCII symbol to be apart from the hyperlink.
If I hover on a recent post then I see that the ASCII symbol is also part of the hyperlink.
<ul>
<?php
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a>></li> ';}
?>
</ul>
How can I have it to be like this:
Recent post 1 >
Recent post 2 >
Instead of:
Recent post 1>
Recent post 2>
Just to follow KISS principle: try this
<ul>
<?php
$args = array( 'numberposts' => '2' );
// get two most recent posts using wp_get_recent_posts()
$recent_posts = wp_get_recent_posts( $args );
// loop through both the posts
foreach( $recent_posts as $recent ){
// get permanent link from id using get_permalink()
$permalink = get_permalink($recent["ID"]);
// esc_attr is basically used for escaping the output
$title = esc_attr($recent["post_title"]);
// finally, echo the output
echo "<li><a href='$permalink' title='$title'>".$recent['post_title']."</a>></li>";
}
?>
</ul>
Refer wp_get_recent_posts, get_permalink and esc_attr on official documentation site for detailed information.
Edit: amended the answer after Ofir Baruch's comment.

wordpress query_posts alternative

I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!

Categories