0

I have a page called "News", and I have created a custom post type for news. In "News" page I need to show all news custom posts and they need to be clickable. "News" page url is http://example.com/media/lajmet. When I click on news it goes on url "http://example.com/news/article1". How to rewrite URL on custom post click to go in "http://example.com/media/lajmet/article1" ? Also I am using polylang for languages.

This is how I have create custom post type for news:

function news_custom_post() {
    $labels = array(
        'name'               => _x( 'News', 'post type general name' ),
        'singular_name'      => _x( 'News', 'post type singular name' ),
        'add_new'            => _x( 'Add new', 'news' ),
        'add_new_item'       => __( 'Add new news' ),
        'edit_item'          => __( 'Edit news' ),
        'new_item'           => __( 'New news' ),
        'all_items'          => __( 'All news' ),
        'view_item'          => __( 'View news' ),
        'search_items'       => __( 'Search news' ),
        'not_found'          => __( 'No news found' ),
        'not_found_in_trash' => __( 'No news found in the Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'News'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our media news data',
        'public'        => true,
        'menu_position' => 6,
        'supports'      => array( 'title', 'editor', 'page-attributes', 'thumbnail'),
        'has_archive'   => true
    );
    register_post_type( 'news', $args );
}
add_action( 'init', 'news_custom_post' );
1
  • 1
    Please use example.com when linking to test sites. Commented Aug 13, 2015 at 18:52

2 Answers 2

1

You can set the rewrite slug when you register your post type:

$args = array(
    'rewrite' => array( 'slug' => 'media/lajmet' )
    // other args...
);
2
  • Cool it works, but when I click on another language, redirects me to the index.php. I am using polylang Commented Aug 13, 2015 at 23:27
  • Also need to go in permalinks after I set the rewrite slug and to save permalinks without any change. Commented Aug 13, 2015 at 23:51
0

You can do it, but it's not straightforward. When you register a custom post type in WordPress, the first argument of register_post_type is the slug that you use. This slug can only be for one URL segment. If you changed it to media then all of your articles would take the form /media/article-slug-here.

If you want to nest it deeper, you will have to register a rewrite rule to listen for the extra segment: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

Your add_rewrite_rule call would likely take a format similar to this:

add_rewrite_rule('^media/lajmet/(.*+)/?', 'index.php?post_type=news&p=$matches[1]', 'top');

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.