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.
Related
Im having a little problem. I want to display all categories connected to a wordpress post.
I found several codes and plugins, but all of them have problems. Some dont display correct anchor text, others dont link to correct url.
So i took the working parts from different places and put them together to the code below, and its working.
The problem is that it does not generate links to ALL connected categories, it only show link to 1 single category (first one that was connected to the post)
what is wrong with the code ? Why will it not show links to all connected categories. I have been working on it for 2 days now, and i give up.
<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>"title=”<?php echo $category_name ?>” ><?php echo $category_name ?></a>
Please try this code
<?php
$category_args_query = array(
'orderby' => 'name',
'parent' => 0,
'hierarchical' => 1,
'number'=> 10
);
$categories = get_categories( $category_args_query );?>
<ul>
<?php
foreach ( $categories as $category )
{ ?>
<li><?php echo $category->name; ?> </li>
<?php
}
?>
</ul>
If you want to show the category that is related with the posts on post details page and archive page add this code in your child theme functions.php and call the function where you want to display it.
function postsCategories()
{
$categories_list = get_the_category_list( esc_html__( ', ', 'your-text-domain' ) );
$allowed_tags_before_after=array('span' => array('class'=>array()),'i'=>array('class'=>array()),'a'=>array('class'=>array(),'href'=>array(),'rel'=>array()));
if ( $categories_list )
{
printf(wp_kses(sprintf(__('<span class="blogmeta cat-links"> <i class="fa fa-folder-open"></i> %1$s </span>','your-text-domain'), $categories_list ),$allowed_tags_before_after));
}
}
Call in the template : <?php printf(__('%s','your-text-domain'),postsCategories()); ?>
Actually just <?php the_category(', ') ?> should be enough (within the WP while loop). The , determines what should go in-between, if there is more than one category.
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.
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.
I tried posting this on the WordPress exchange, but unfortunately I didn't get any further along, so I'm trying it here. Hopefully this works!
I have a feeling that I'm on the right track, I just don't have enough solid PHP knowledge to get much further than where I'm currently at.
I'm currently using the following code to return a list of child categories from a single category:
<?php
$taxonomyName = "category";
$terms = get_terms($taxonomyName,array('parent' => 79));
echo '<ul>';
foreach($terms as $term) {
echo '<li>';
echo ''.$term->name.'<br/>';
$thumbnails = get_posts('numberposts=1&orderby=rand');
foreach ($thumbnails as $thumbnail) {
echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
if ( has_post_thumbnail($thumbnail->ID)) {
echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
} else {
echo 'no thumbnail';
}
echo '</a>';
}
echo '<li>';
}
echo '</ul>';
?>
This code works somewhat. It returns a list of all six sub categories under the parent, ID 79. However, I want to also return one random thumbnail in each of the list items for each of the 6 sub categories.
Unfortunately, this code returns a random thumbnail from all of my posts, not just ID 79 and it's specific child. I need it to return one thumbnail from the same category that is returned in it's parent <li>.
Additionally, the code returns "no thumbnail" if there is no code, or if I take the else out, it returns nothing. I'd like to make it to where it returns at least one image every time, so ideally there would be some kind of logic that says to always return at least one image. I just don't know how to do that.
Is there some easy way to do this? I'm thinking I need to sort through that array and return the category in the nested foreach loop, but unfortunately it's over my head.
I think I'm looking for something similar to this person, but unfortunately, they didn't get any replies. -> https://stackoverflow.com/questions/18750040/random-featured-image-based-on-category
Thanks in advance for any help!
So in order to do this, I needed to first do a for each loop, store the category slug as a variable, JAMterm, and then use that in a query to pull one random thumbnail from the category.
Thanks to #Renishkhunt for helping me along the way to get this answer.
<?php
$taxonomyName = "category";
$terms = get_terms($taxonomyName,array('parent' => 79));
echo '<ul>';
foreach($terms as $term) {
echo '<li>'.$term->name.'<br/>';
$JAMterm = $term->slug;
global $wp_query;
$term = $wp_query->queried_object;
$args=array(
'orderby' => 'rand',
'posts_per_page' => 1,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $JAMterm
)
)
);
$new_query = null;
$new_query = new WP_Query($args);
while ($new_query->have_posts()) : $new_query->the_post();
the_post_thumbnail();
endwhile;
wp_reset_postdata();
echo '</li>';
}
echo '</ul>';
?>
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;
?>