-2

My site currently has an opinions page which displays opinions posts (/opinions/).

The post pages are only showing as /post-title/ when I need /opinions/post-title/.

Code below, changes the default post type to "opinions".

public function revcon_change_post_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'Opinions';
    $submenu['edit.php'][5][0] = 'Opinions';
    $submenu['edit.php'][10][0] = 'Add Post';
    $submenu['edit.php'][16][0] = 'Opinion Tags';
    echo '';
}

public function revcon_change_post_object() {
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'Opinions';
    $labels->singular_name = 'Opinion';
    $labels->add_new = 'Add Post';
    $labels->add_new_item = 'Add Post';
    $labels->edit_item = 'Edit Post';
    $labels->new_item = 'Post';
    $labels->view_item = 'View Post';
    $labels->search_items = 'Search Opinions';
    $labels->not_found = 'No Post found';
    $labels->not_found_in_trash = 'No Posts found in Trash';
    $labels->all_items = 'All Posts';
    $labels->menu_name = 'Opinions';
    $labels->name_admin_bar = 'Opinions';
}

These two functions sit inside wp-content/mu-plugins alongside some custom post type set up.

What do I need to add so that it constructs the url as /opinions/post-title/?

2

Changing arguments for a post type "on the fly" by changing the global at some random point in time is below ideal. When you look at register_post_type() there is the register_post_type_args filter.

Example on how to use it as a small (mu-)plugin:

<?php
/**
 * Plugin Name: Change Post Type arguments for Post Type X
 */
add_filter( 'plugins_loaded', function() {

    add_filter( 'register_post_type_args', function( Array $args, $post_type ) {
        // Change nothing for all other post types
        if ( 'your_post_type' !== $post_type )
            return $args;

        $args['rewrite'] => [
            'with_front' => true,
            'slug'       => 'opinions',
        ];

        return $args;
    }, 20, 2 );
} );

In above example, the post type gets a /opinions "front" prepended. Example resulting Url:

https://example.com/opinions/rewrites-are-cumbersome

To adjust the labels, use the labels array that you will find in above link and in all examples below (plus changing help tabs, update messages, etc.).

Your admin menu slugs and labels will adjust accordingly by themselves, fed by the labels arguments you provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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