I have a loop for showing recents posts in wordpress:
<ul class="homepage-section homepage-era-jazzu fusion-clearfix">
<?php
$args = array( 'numberposts' => '4', 'post_type' => 'era-jazzu', 'post_status' => array('publish', 'future') );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . get_the_post_thumbnail($recent["ID"], '260-160') . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
</ul>
It works but not in the way I would love to. My issue is following - sometimes one of the recent posts is 'scheduled' post. That's why it's permalink is UGLY like: http://yourdomainurl.com/?post_type=jazz&p=10603
how to make it work with nice permalink like yourdomainurl.com/nice-address
? from admin standpoint everything is ok (so in dashboard i see nice permalinks; only issue is with my loop code).
thanks!
Just change get_post_permalink($recent["ID"]) on get_permalink($recent["ID"]) and everything will work fine.
Related
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.
How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>
today i need to get all the posts from a specific category on wordpress.
The code it's pretty basic, and it's like that:
print_r(get_posts(array('numberposts' => -1, 'category' => 3)));
The id of the category it's obviously 3.
But i receive always the last 5 posts of that category, not all the post present there (something like 60posts).
Anyone know why could happen this strange stuff?
Try something like this:
<ul>
<?php
$args = array( 'numberposts' => -1,'category' => 3 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; wp_reset_postdata();?>
</ul>
If this works for you, all you need to do then is to change the output display(the ul and li)
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.
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;
?>