go41

wp_list_pages query children of current custom post type

von Joern am 30. Jun. 2012 | Keine Kommentare

It took me a while to find the code to query the sub-pages (children) of the current custom post type.

Just found a solution, let me share it.

The custom post type I use is set to ‚hierarchical‘ => true, enabling a hierachical order with sub-pages.
The permalink structure came out like this:

my-site/custom-post-type-name/custom-post-parent/custom-post-child/

sample here:

www.traumrabatt.de/verzeichnis/computer … fikkarten/
Working with templates for the custom-post-type archive (archive-my_verzeichnis.php) and also the single post in this series as template (single-my_verzeichnis.php) it possible to list all posts of this type in the archive template.
Now it’s also nice to show on the custom-post-parent a list of the sub-pages (custom-post-children).
There are two ways to do this, using ‚wp_list_pages‘ or a custom query with ‚get_pages‘
Because I use this query on a single-custom-template, I just need to get the page-ID and query for potential children.

With wp_list_pages the code I use looks like this:

<?php
$the_id = get_the_ID();
$menu_args = array(
‚depth‘ => 2,
‚post_type‘ => ‚my_verzeichnis‘,
’show_date‘ => 0,
‚child_of‘ => $the_id,
‚title_li‘ => “,
‚echo‘ => 0,
’sort_column‘ => ‚menu_order‘,
‚link_before‘ => “,
‚link_after‘ => “,);

echo wp_list_pages($menu_args);
?>
This code is working well, the drawback is that wp_list_pages always creates a list with li – /li around each entry.
I wanted to get a flat list, so I used now this code:

<?php
$the_id = get_the_ID();
$mypages = get_pages( array(
‚depth‘ => 2,
‚post_type‘ => ‚my_verzeichnis‘,
’show_date‘ => 0,
‚child_of‘ => $the_id,
‚title_li‘ => “,
‚echo‘ => 0,
’sort_column‘ => ‚menu_order‘,
‚link_before‘ => “,
‚link_after‘ => “)
);
if ($mypages) { ?>
<br />mehr zu <?php the_title(); ?>: <?php foreach( $mypages as $page ) { ?>
<a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>,
<?php } ?>
<?php } else { // if nothing in mypages ?>

<?php } // end mypages ?>
This reads like: if there are children (if ($mypages)) show the title of the current parent post followed by a list of the sub-pages.

If no children present I display just three ‚-‚, just for debugging or adding something in the future.
Questions are welcome..
____________________
you find me on Google+, Twitter and Facebook

(von: Joern)

Sorry, no posts matched your criteria.

Autor:

Du findest mich auch auf Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

weitere forum Beiträge: