2

I have my custom post type set up with hierarchicy set to true and the url works like: Lions News › S1 › Test Post 1 which is nice except that it should go one futher and be like Lions Minis › Lions News › S1 › Test Post 1 Lions Minis being the parent page to everything. Is there a way to to set that up?

The following is my setup in functions.php

/** Custom Post Types for Lions News */
 $labels = array(
'name'                          => 'Lions News Categories',
'singular_name'                 => 'Lions Category',
'search_items'                  => 'Search Lions Categories',
'popular_items'                 => 'Popular Lions Categories',
'all_items'                     => 'All Lions Categories',
'parent_item'                   => 'Parent Lions Category',
'edit_item'                     => 'Edit Lions Category',
'update_item'                   => 'Update Lions Category',
'add_new_item'                  => 'Add New Lions Category',
'new_item_name'                 => 'New Lions Category',
'separate_items_with_commas'    => 'Separate Lions Categories with commas',
'add_or_remove_items'           => 'Add or remove Lions Categories',
'choose_from_most_used'         => 'Choose from most used Lions Categories'
);

$args = array(
'label'                         => 'Lions Categories',
'labels'                        => $labels,
'public'                        => true,
'hierarchical'                  => true,
'show_ui'                       => true,
'show_in_nav_menus'             => true,
'args'                          => array( 'orderby' => 'term_order' ),
'rewrite'                       => array( 'slug' => 'lions_news/lions_articles', 'with_front' => false ),
'query_var'                     => true
);

register_taxonomy( 'lions_articles', 'lions_news', $args );

register_post_type( 'lions_news', 
array(
    'labels'                => array(
        'name'              => __( 'Lions News' ),
        'singular_name'     => __( 'Lions News' )
        ),
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'supports'              => array( 'title','editor','thumbnail','comments','revisions' ),
    'rewrite'               => array( 'slug' => 'lions_news', 'with_front' => false ),
    'has_archive'           => true
)
);
/** End custom Post Types */

Thanks!

3 Answers 3

5
+25

you should just be able to change this line

'rewrite' => array( 'slug' => 'lions_minis/lions_news', 'with_front' => false ),

Remember to always visit the Settings->Permalinks menu in wordpress after making a rewrite change for it to update the links!

2

Take a look at this line: 'rewrite' => array( 'slug' => 'lions_news', 'with_front' => false ),

6
  • I went for the obvious and added 'rewrite' => array( 'slug' => 'lions-minis', 'lions_news', 'with_front' => false ), but no joy? Also tired variations of true and false but I got nothing
    – Nsokyi
    Commented Jul 15, 2011 at 13:36
  • try 'with_front' => true
    – kaiser
    Commented Jul 15, 2011 at 13:50
  • Tried and it had no effect?
    – Nsokyi
    Commented Jul 15, 2011 at 14:19
  • Take a look at the Codex entry at the 'rewrite' part.
    – kaiser
    Commented Jul 15, 2011 at 16:06
  • nope, I read it seems if you change the rewrite part to: 'rewrite' => array( 'pages' => 'lions-minis', 'lions_news', 'lions_articles', 'with_front' => true ), Then it should work but it stubbornly stays the same?
    – Nsokyi
    Commented Jul 18, 2011 at 15:39
0

The below code works if you set hierarchical to false in register_post_type (my understanding here is you don't actually need hierarchical relationships between the news posts themselves, just that they need to be associated with the page hierarchy).

The following is lifted and modified from here: http://justintadlock.com/archives/2013/10/07/post-relationships-parent-to-child

/* Hook meta box to just the 'lions_news' post type. */
    add_action( 'add_meta_boxes_lions_news', 'my_add_meta_boxes' );

    /* Creates the meta box. */
    function my_add_meta_boxes( $post ) {

        add_meta_box(
            'my-lions-news-parent',
            'Parent Page',
            'my_lions_news_parent_meta_box',
            $post->post_type,
            'side',
            'core'
        );
    }

    /* Displays the meta box. */
    function my_lions_news_parent_meta_box ( $post ) {

        $parents = get_pages(
            array(
                'post_type'   => 'page',
                'post_status' => 'publish', 
                'sort_column' => 'menu_order',
                'parent' => -1
            )
        );

        if ( !empty( $parents ) ) {

            echo '<select name="parent_id" class="widefat">'; // !Important! Don't change the 'parent_id' name attribute.

            foreach ( $parents as $parent ) {
                printf( '<option value="%s"%s>%s</option>', esc_attr( $parent->ID ), selected( $parent->ID, $post->post_parent, false ), esc_html( $parent->post_title ) );
            }

            echo '</select>';
        }
    }

You can replace the 'parent' => -1 in get_pages with any page id to restrict the dropdown to show only children of that page.

Simplest from there would probably be to create a page template for all the pages with news articles assigned that has a custom loop through child posts of the current page.

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.