0

I'm creating a custom options page on my site. One of the options is controlling a box on the homepage and i'm trying to set the link for the item to any page on the site via a select menu.

as of now I can list all the pages including my custom post types - what I'm trying to do is also have the custom post type archive page as a link as well as some sort of ordering in the list.

So for example the select hierarchy should be something like this: Work - (Custom Post Type Archive) Matchbox - (Custom Post Type Page) Page - (Regular Page)

My current code:

$sitePages= get_posts(array('post_type'=>array('page', 'work', 'people'), 'posts_per_page'=>-1, 'post_status'=>'publish', 'depth'=> 0));

$wp_cats = array();
foreach ($sitePages as $page_list ) {
       $wp_cats[$page_list->ID] = $page_list->post_title;
}
array_unshift($wp_cats, " ");

and then a foreach on $wp_cats calls it back.

any help would be greatly appreciated!

*****UPDATE*** Based on Sven's answer ∨∨

I used the following code to output everything.

<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
        <option value="">Choose A Page</option>
        <?php 
        $post_types = array( 'page', 'work', 'people' );
        $wp_cats = array();
        foreach ( $post_types as $post_type ) {

        $pages = get_pages( array( 'post_type'=>$post_type, 'posts_per_page'=>-1, 'post_status'=>'publish', 'depth'=> 0 ) );

        $obj = get_post_type_object( $post_type );
        // output custom post type archive with link
        if ( $post_type !== 'page' ){
        echo '<option value="' . get_post_type_archive_link( $post_type ) .'"';
        if (get_option( $value['id'] ) == get_post_type_archive_link( $post_type )) { echo 'selected="selected"'; }
        echo '>'. $obj->labels->singular_name . '</option>';
        }
        foreach ( $pages as $page ) {
       $wp_cats[$page->ID] = $page->post_title;
       // output posts with link
       echo '<option value="' . get_permalink($page->ID).'" ';
       if (get_option( $value['id'] ) == get_permalink($page->ID)) { echo 'selected="selected"'; }
       echo '>&nbsp;&nbsp;&nbsp;'. $page->post_title .'</option>';
         }
       }
        ?>
</select>

1 Answer 1

1

Yep, this is definitely possible. Grab your post types and insert them in $post_types to loop through every one of them to manually generate the archives and posts:

<?php
// post types of your choice
$post_types = array( 'page', 'work', 'people' );

$wp_cats = array();
foreach ( $post_types as $post_type ) {

    $pages = get_pages( array( 'post_type'=>$post_type, 'posts_per_page'=>-1, 'post_status'=>'publish', 'depth'=> 0 ) );

    $obj = get_post_type_object( $post_type );
    // output custom post type archive with link
    echo '<a href="' . get_post_type_archive_link( $post_type ) . '">' . $obj->labels->singular_name . '</a><br />';

    foreach ( $pages as $page ) {
       $wp_cats[$page->ID] = $page->post_title;
       // output posts with link
       echo '<a href="' . get_permalink($page->ID) . '">' . $page->post_title . '</a><br />';
    }
}
?>

Note: As you can see this will output plain links; I think you can easily adjust the HTML-markup yourself as you didn't request something special in your question.

You can also auto-generate the structure via wp_list_pages(), but this is sometimes getting tricky if you'll need a custom HTML-markup:

<?php
// post types of your choice
$post_types = array( 'page', 'work', 'people' );

foreach ( $post_types as $post_type ) {
    // generate complete list links and titles
    wp_list_pages( array( 'post_type'=>$post_type, 'title_li'=>$post_type ) );
}
?>
1
  • Thank You! I ended up going with the first option and outputted it into a <select>
    – Moishy
    Commented Dec 8, 2013 at 17:43

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.