WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Types, Pages and Permalink Structure Problem (3 posts)

  1. RedEyedTreeFrog
    Member
    Posted 2 years ago #

    I've spent days looking all over the web for a working solution to this problem. There are similar issues with resolutions, but I haven't been able to adapt any of them to my specific problem... or I'm missing something.

    In the world of HTML websites, you have:
    example.com/events/ <-- listing of events
    example.com/events/some-event.html <-- specific event

    I'm developing a WordPress site (locally, so I can't link to it) using a custom post type of "event" being displayed with the custom post template single-event.php. The page that lists each item from that custom post type uses a custom page template named template-events.php with a title/permalink of "events".

    What I'm trying to do is have the URL (permalink) look and behave just like a user would expect. That is, when someone visits "http://www.example.com/events/", they see the list of events (looping through the custom post types named "event" and displaying excerpts). When they visit or click through to "http://www.example.com/events/some-event/", they see the post for that specific event.

    Right now I have it set up as:
    "example.com/events/ shows the list of events as described above.
    "example.com/event/some-event/ shows an event post.
    where the event post is using the custom post type name as the slug.

    The pages/posts all work individually. I just can't get that permalink structure to work. I'm not using any custom taxonomies and I don't have any 404 errors.

    Simply using:
    'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
    when creating a custom post type doesn't work. The 'events' slug conflicts with page permalink of the same name.

    I'm tearing my hair out here. Hopefully somebody here can help me with this.

  2. RedEyedTreeFrog
    Member
    Posted 2 years ago #

    Solved using archive template. In the custom post type declaration you have:

    'has_archive' => true,

    So, instead of creating a page template, create an archive template named archive-{name of custom post type}. The permalink hierarchy built into WordPress will automatically show an index of your custom post types in the parent directory, e.g. example.com/events/ would be the listing and example.com/events/some-event would be an event. There is no name conflict this way. Just be sure in your archive template you have the following before the call to the loop:

    <?php query_posts(array('post_type'=>'custom post type name')); ?>

    To call elements from the meta boxes you created in your custom post type and display them on the listing page, put the following right after the loop:

    <?php
    	$custom = get_post_custom($post->ID);
    	$meta_box_one = $custom["meta_box_one"][0];
    	$meta_box_two = $custom["meta_box_two"][0];
    	...
    ?>

    where "meta_box_one" (and two, etc.) are the names of your meta box variables defined in your custom post type functions. Display them using the following in your template markup anywhere within the loop:

    <?php echo $meta_box_variable ?>

    My mistake was using a page template (thanks to the outdated tutorial I used) to display the listing of my custom posts. While that method does technically work, it does not allow for an orderly URL structure.

    One more thing... using a dash (hyphen) in your custom post type name (e.g. "some-name") makes wordpress break. Last I saw, they punted this issue out to v3.3+ so there will be no resolution any time soon.

    Hope this helps someone!

  3. ravexl
    Member
    Posted 1 year ago #

    Registered to be a user just to say THANK YOU. The countless searches and frustration being pointed in 10 different directions is finally over.

    I have a tear soaked happy face.

Topic Closed

This topic has been closed to new replies.

About this Topic