• Hi,

    I have a website with a glossary on it. I have a page called domainname.com/glossary/ which is a standard ‘page’. I have created a custom post type of ‘jw_glossary’ to store glossary items. I want the glossary items to appear under the glossary page so i would end up with a url structure like domainname.com/glossary/<item>.

    In order to do this, I have created the glossary item custom post type like so:

    function create_post_type_glossary() {
    	register_post_type( 'jw_glossary',
    		array(
    			'labels' => array(
    				'name' => __( 'Glossary Items' ),
    				'singular_name' => __( 'Glossary Item' )
    			),
    		'public' => true,
    		'has_archive' => true,
    		'rewrite' => array('slug'=>'glossary'),
    		)
    	);
    }

    My understanding is that if I were to use this line:

    'rewrite' => array('slug'=>'glossary'),

    I will be able to prepend the URL’s for the glossary items with /glossary/ and give the illusion of them being below glossary in the site structure (/glossary/<item>). I have code on my /glossary/ page which lists links to the CPT’s like so:

    $args = array('post_type'=>'jw_glossary');
    $loop1 = new WP_Query($args);
    echo "<ul>";
    while ( $loop1->have_posts() ) : $loop1->the_post();
    	echo "<li><a href='";
    	echo get_permalink();
    	echo "' title='";
    	echo the_title();
    	echo "'>";
    	echo the_title();
    	echo "</a></li>";
    endwhile;
    echo "</ul>";
    }

    These render the correct URLs (domainname.com/glossary/<item>) but when I click them and go through, I get a 404.

    Am I going about this the correct way and if not, how do I make it so that the CPT pages sit below the /glossary/ in the URL?

    Many thanks in advance for any solutions.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I have had this problem in the past. The reason why the links don’t work (domainname.com/glossary/<item>) is probably because WordPress is looking for sub pages of your glossary page but there aren’t any and therefore it gives a 404.

    What happens if you change

    'rewrite' => array('slug'=>'glossary'),

    for:

    'rewrite' => array('slug'=>'glossary-item/'),

    If that works then I guess it confirms what I was saying.

    Thread Starter andywww

    (@andywww)

    Hi,

    Thanks. This does work once I change it to the glossary-item slug. I’m sure people have come across this before with /category/product or /group/name or something where the ‘homepage’ of the category needs to be bespoke… surely theres a way to do it?

    Thanks.

    Instead of using a page as your /glossary you could always use the custom post type archive page instead:

    https://mark.mcwilliams.me/2010/10/wordpress-3-1-introduces-custom-post-type-archives/

    So the code that goes in your glossary page would go into archive-jw_glossary.php instead.

    Thread Starter andywww

    (@andywww)

    Excellent. Thats exactly what I wanted.

    Many thanks for the help.

    Thread Starter andywww

    (@andywww)

    Am I correct in thinking this will make it impossible to have the page content managed should I want a section introducing the glossary?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Creating custom post types who have the same slug as an existing page.’ is closed to new replies.