Solved

Wordpress Hierarchical List Pages In Custom Format

Posted on 2013-05-30
15
681 Views
Last Modified: 2013-05-31
I need to generate a list of all Wordpress Pages and have them display in a HTML Table, that will be printed for offline use.

First column is Blank
Second column will have the Page Creation Date
Third column will have the last Page Modified Date
Fourth column will have the Page Title

However, the Pages need to be listed by Menu_Order and if a Page is a child of the one above it, that Page Title either will need to be intended.

Basically, I need something similar to the Wordpress "All Pages" list, but in a simple table format and only the information above.

I have searched Google for several hours and have found some code snippets, but nothing has worked.

wp_list_pages will not allow me to display both Creation and Modified Dates at the same time, nor can I get the formatting I need.
0
Comment
Question by:manticlight
  • 6
  • 5
  • 4
15 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39209625
Why not just create a custom page template and add PHP code (wp_query) that directly queries the wp_posts table and returns the columns you need?
0
 

Author Comment

by:manticlight
ID: 39209637
Because wp_query would not necessarily return the pages in a Hierarchical Order, and I would still have the problem of needing indentation of SubPages, Sub-SubPages, etc.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39209673
Ah-ha...now I understand.  Okay, let me dig through some code snippets I have and see if I can cobble something together.
0
 

Author Comment

by:manticlight
ID: 39209754
Thanks.  I look forward to hearing back from you.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39209892
Sometimes people get so caught up in the siren call of wp_list_pages() they forget about get_pages()

<?php
/**
 Template Name: List Pages With get_page()
 */

get_header(); ?>

		<div id="primary">
			<div id="content" role="main">
				<table>
				<tr><td>Blank</td><td>Page Creation</td><td>Page Modified</td><td>Page Title</td></tr>
				
				<?php 
					$args = array( // including all of the defaults here so you can play with them
						'sort_order' => 'ASC',
						'sort_column' => 'post_title',
						'hierarchical' => 1,
						'exclude' => '',
						'include' => '',
						'meta_key' => '',
						'meta_value' => '',
						'authors' => '',
						'child_of' => 0,
						'parent' => -1,
						'exclude_tree' => '',
						'number' => '',
						'offset' => 0,
						'post_type' => 'page',
						'post_status' => 'publish'
					); 
					$pages = get_pages( $args ); 
						foreach ( $pages as $page ) {
						$tablerow = '<tr><td>&nbsp;</td>';
						$tablerow .= '<td>' . $page->post_date . '</td>';
						$tablerow .= '<td>' . $page->post_modified . '</td>';
						$tablerow .= '<td>' . $page->post_title . '</td></tr>';
						echo $tablerow;
					}
				?>
				
				</table>
			</div><!-- #content -->
		</div><!-- #primary -->

<?php get_footer(); ?>

Open in new window


http://codex.wordpress.org/Function_Reference/get_pages
0
 

Author Comment

by:manticlight
ID: 39209898
Thank you for your response, but yes - I do know about get_pages().  However, that does not indent the post_title as I require, and mentioned in my original question.  If a page is a subpage, sub-subpage, sub-sub-subpage, etc - I need to have it indented accordingly.

While get_pages() will list the pages in a Hierarchical Order, it does not indent - and that is where I am stuck the most.

As the column Page Title is populated, I need the page_title to indent, similar to:

ID 1 PARENT-ID 0
ID 2 PARENT-ID 0
    ID 3 PARENT-ID 2
    ID 4 PARENT-ID 2
        ID 5 PARENT-ID 4
    ID 6 PARENT-ID 2
ID 7 PARENT-ID 0
ID 8 PARENT-ID 0
   ID 9 PARENT-ID 8

Open in new window

0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39209909
You can fake it by doing a check on $page->post_parent as the foreach loop begins and if the parent value is there, indent by opening a new unordered list or something.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:manticlight
ID: 39209915
Unfortunately, I do not know how to do that - as there can be any number of pages with the same parent, and while it is indenting those, there can be sub-subpages, sub-sub-subpages, etc. - and I do not know how to put that into a function so that regardless of how deep a page is, it will indent accordingly.
0
 
LVL 15

Expert Comment

by:eemit
ID: 39210263
Try this:

Put this to your Theme's Functions.php

function PagesToHtmlTable() {
	// Query pages.
	$args = array(
		'hierarchical' => 0,
		'post_type' => 'page',
		'parent' => 0, //returns all top level pages 
		'sort_column' => 'menu_order',
		'sort_order' => 'asc' //or 'desc'
	); 
	
	$output = '';
	$output = '<table class="my-pages-to-html-table">';

	$output .= '<tr>';
	$output .= '<th>Notice</th>';
	$output .= '<th>Creation Date</th>';
	$output .= '<th>Modified Date</th>';
	$output .= '<th>Page Title</th>';
	$output .= '</tr>';

	$iLevel = 0;

	$mypages = get_pages($args);
	
	foreach( $mypages as $page ) {
		$iLevel = 0;
		
		$output .= AppendPageToHtmlTable($page, $page->post_date, $page->post_modified, $page->post_title, $iLevel);
		
		$mysubpages = get_pages( array( 'child_of' => $page->ID, 'sort_column' => 'menu_order', 'sort_order' => 'asc' ) );

		if ( !empty($mysubpages) ) {
		
			foreach ( $mysubpages as $page ) {
				$iLevel = 1;
				
				// Get depth value
				$iLevel = sizeof($page->ancestors);
				
				$output .= AppendPageToHtmlTable($page, $page->post_date, $page->post_modified, $page->post_title, $iLevel);
			}	
		}
	}
	$output .= '</table>';
	return $output;
}
add_shortcode( 'pages_to_html_table', 'PagesToHtmlTable' );

function AppendPageToHtmlTable($page, $cDate, $mDate, $pTitle, $iLevel) { 

	$output .= '<tr>';
	$output .= '<td>&nbsp;</td>';

	$output .= '<td>' . $cDate . '</td>';
	$output .= '<td>' . $mDate . '</td>';

	if ( 0 == $iLevel ) {
		$output .= '<td>';
	} else {
		$output .= '<td style="text-indent: ' . $iLevel * 15 . 'px">';
	}
	
	
	$output .= $pTitle;
	$output .= '</td>';

	$output .= '</tr>';

	return $output;

}

Open in new window


Put this into your Theme's css file:
table.my-pages-to-html-table{
	border-collapse:collapse;
	border: 1px;
	border-style:solid;
	border-color:black;
	width: 100%;
	margin: 24px 0;
}

table.my-pages-to-html-table td,
table.my-pages-to-html-table th {
	border:1px solid black;
	padding-left:5px;
}

table.my-pages-to-html-table th {
	background-color:yellow;
	/*color:white;*/
	color:black;
}

Open in new window



Add this to your Post or Page:
[pages_to_html_table]

Open in new window


Or add this to your Template file:
<?php echo do_shortcode('[pages_to_html_table]'); ?>

Open in new window

0
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 39210836
Version 2:
function PagesToHtmlTable2() {
	// Query pages.
	$args = array(
		'hierarchical' => 1,
		'post_type' => 'page',
		'parent' => -1, //returns all pages 
		'sort_column' => 'menu_order',
		'sort_order' => 'asc' //or 'desc'
	); 

	$output = '';
	$output = '<table class="my-pages-to-html-table">';

	$output .= '<tr>';
	$output .= '<th>Notice</th>';
	$output .= '<th>Creation Date</th>';
	$output .= '<th>Modified Date</th>';
	$output .= '<th>Page Title</th>';
	$output .= '</tr>';

	$iLevel = 0;

	$mypages = get_pages($args);
	
	foreach( $mypages as $page ) {
		$iLevel = 0;
		
		// Get depth value
		$iLevel = sizeof($page->ancestors);
		
		$output .= '<tr>';
		$output .= '<td>&nbsp;</td>';

		$output .= '<td>' . $page->post_date . '</td>';
		$output .= '<td>' . $page->post_modified . '</td>';

		if ( 0 == $iLevel ) {
			$output .= '<td>';
		} else {
			$output .= '<td style="text-indent: ' . $iLevel * 15 . 'px">';
		}
		
		
		$output .= $page->post_title;
		$output .= '</td>';

		$output .= '</tr>';
		
	}
	$output .= '</table>';
	return $output;
}
add_shortcode( 'pages_to_html_table_2', 'PagesToHtmlTable2' );

Open in new window

0
 

Author Comment

by:manticlight
ID: 39211220
@eemit - Thank you for providing the code you did.  I feel it is very close to what I need.  However, with both versions of the code, I get the following error "Notice: Undefined property: stdClass::$ancestors" referring to $iLevel = sizeof($page->ancestors);

Referring to http://codex.wordpress.org/Function_Reference/get_pages - "ancestors" is not a key returned with get_pages().
0
 
LVL 15

Assisted Solution

by:eemit
eemit earned 500 total points
ID: 39211445
Hier works well!
Try to replace this:
$iLevel = sizeof($page->ancestors);

Open in new window

with:
$ancestors = get_ancestors( $page->ID, 'page' );
$iLevel = sizeof($ancestors);

Open in new window

0
 
LVL 15

Expert Comment

by:eemit
ID: 39211578
With:
$iLevel = sizeof($page->ancestors);
var_dump($page->ancestors);
var_dump($iLevel);

I get:
array
  empty
int 0
array
  empty
int 0
array
  0 => int 87
int 1
array
  0 => int 87
int 1
array
  0 => int 424
  1 => int 87
int 2
array
  empty
int 0

The same with:
$ancestors = get_ancestors( $page->ID, 'page' );
$iLevel = sizeof($ancestors);
var_dump($ancestors);
var_dump($iLevel);

But not with:
$ancestors = get_ancestors( $page->ID ); //as in codex
$iLevel = sizeof($ancestors);
var_dump($ancestors);
var_dump($iLevel);
0
 

Author Comment

by:manticlight
ID: 39211601
@eemit - The solution to the error message I reported, was for me to use - as you suggested:

$ancestors = get_ancestors( $page->ID, 'page' );
$iLevel = sizeof($ancestors);

Open in new window


That got me exactly to what I need, and to a point where all I need to do is style it more to my liking.

I chose "Version 2" as the solution because code was several lines smaller and didn't require the extra Append Function.

Awesome work!
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39211612
That's good stuff.  Nice work eemit
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now