I have a blog here /www.mysite.com/section/news/ that only shows news in a specific category.
When I use wp_get_recent_posts() and get_permalink($recent[“ID”]) the link goes here /www.mysite.com/the-post-title/
<?php
$args = array(
'numberposts' => 2,
'category' => 14 );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
How do I direct links to /www.mysite.com/section/news/the-post-title/
thanks, any help much appreciated.
Regards,
The permalinks by default are output relative to the site’s blog posts index (aka
'page-for-posts'
, aka posts page). You’re using a custom page template for the static pageSection/News
. That static page is unrelated to the site blog posts index. That’s why the permalinks don’t reference that page.If you want your permalinks to reference your static page
Section/News
, then you need to assign that static page as your posts page inDashboard -> Settings -> Reading
.If you want to be able to display only posts for a specific category on your blog posts index, you can do that via a query filter hooked into
pre_get_posts
; e.g.:Those function are going to return URLs based on your permalink settings. If the only place you want to change this is the code you posted, just construct the URL manually.
post_name
is the post slug, which is what WordPress uses to create URLs, so it should be a pretty safe way to generate the URL.