1

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,

2
  • What are 'section' and 'news'? Are those nested pages, a page and a subpage? I am guessing that you are using a ponstname-only pretty permalink?
    – s_ha_dum
    Dec 17, 2012 at 14:55
  • section is a static page and news is a page within for displaying news items based on a specific category. Dec 17, 2012 at 15:06

2 Answers 2

1

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 page Section/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 in Dashboard -> 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.:

function wpse76370_filter_pre_get_posts( $query ) {
    if ( ! is_admin() && is_home() && $query->is_main_query() ) {
        // This is the main loop query for the blog posts index;
        // Only display a specific category
        $query->set( 'cat', '14' );
    }
}
add_action( 'pre_get_posts', 'wpse76370_filter_pre_get_posts' );
0

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.

<?php
    $args = array(
        'numberposts' => 2,
        'category' => 14 
    );
    $recent_posts = wp_get_recent_posts($args);
    $base_page = get_permalink(<id-of-news-subpage>);
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . $base_page . $recent['post_name'] . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
?>

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.

1
  • Thanks, I will give this a try as soon as i have time and reprt back. Moved on to somethin else. grr. Dec 19, 2012 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.