How do I link to a blog not set as the homepage?

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/

Read More

thanks, any help much appreciated.

Regards,

Related posts

Leave a Reply

2 comments

  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' );
    
  2. 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.