3

I have a CPT called 'vacancies' here is the code that registers it:

function register_cpt_vacancy() {
    $args = array('labels' => $labels, 'hierarchical' => false, 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions'), 'taxonomies' => array('departments'), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post');
    register_post_type('vacancy', $args);
}

In a template footer I'm trying to do this:

wp_list_pages('post_type=vacancy');

Which - from reading the docs I thought would work.

Is there another way to do this besides creating my own query and menu styles? This list of vacancies is appearing as a menu between two other wp menus called with wp_nav_menu so I would like it to be in a list structure like they are.

Any help or suggestions for different approaches welcomed :)

1 Answer 1

5

On the wp_list_pages docs, it states that:

If a given custom post type is hierarchical in nature, then wp_list_pages() can be used to list the member of that custom post type.

Based on your code, the CPT that you are attempting to use with wp_list_pages() is not hierarchical.

Therefore, two solutions:

1. Change the Custom Post Type

Make your post type hierarchical (like pages) by setting 'hierarchical' => true and 'capability_type' => 'page'.

2. Use a Different Function

I recommend using something like:

$vacancy_menu = get_posts( 'post_type=vacancy' );

if( $vacancy_menu ) : ?>
    <ul>
    <?php foreach( $vacancy_menu as $menu_item ) : 
      setup_postdata( $menu_item );?>
        <li><a href="<?php the_permalink(); ?>" title="Go to <?php the_title(); ?>"> <?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

If you'd like, this could easily be setup as a function to create your own "wp_list_posts()" or set to match the html of wp_nav_menu().

Also, be sure to check out this example from CSS Tricks as well for a similar explanation with info on how to apply "current" classes to the links.

2
  • Brilliant mate, thanks for the explanation, and workarounds! Jul 26, 2012 at 12:03
  • You need to add setup_postdata( $menu_item ) in the loop, otherwise you end up with all the same links to the page you're on.
    – Janghou
    Feb 24, 2016 at 11:49

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.