Forums

[resolved] List Pages with Page Custom Field (5 posts)

  1. teeds
    Member
    Posted 2 years ago #

    I would like to add a description to my page navigation menu.

    The html I need to make the CSS work is.
    <li><a href="#"><span>About this site</span><br />About</a></li>

    My guess would be to do this through custom fields within each parent page.

    Where I am struggling is coding some php for listing only parent pages along with the associated custom field.

    My current understanding is that I'll need to set up a wp-query that loops through the pages grabbing the title, permalink and custom field.

    Pointers? Further reading?

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    <?php
    $args=array(
      'orderby' => 'title',
      'order' => 'ASC',
      'post_parent' => 0,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Pages (just parent pages)';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        $link = get_post_meta($my_query->post->ID, 'link_url', true);
        $text = get_post_meta($my_query->post->ID, 'link_text', true);
        if ($link) {
          echo 'Credit: <a href="'.$link .'">'.$text.'</a>';
        }
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. teeds
    Member
    Posted 2 years ago #

    Thanks MichaelH

    This is what I ended up using, it works but the opening and closing of the php doesn't look correct.

    <?php
    					$args=array(
    						'orderby' => 'title',
    						'order' => 'ASC',
    						'post_parent' => 0,
    						'post_type' => 'page',
    						'post_status' => 'publish',
    						'posts_per_page' => -1,
    						'caller_get_posts'=> 1
    					);
    					$my_query = null;
    					$my_query = new WP_Query($args);
    					if( $my_query->have_posts() ) {
    						// List of Pages (just parent pages)
    						while ($my_query->have_posts()) : $my_query->the_post(); ?>
    						<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><span><?php echo get_post_meta($post->ID, 'description', true); ?></span><br /><?php the_title(); ?></a></li>;
    						<?php endwhile;
    					}
    					wp_reset_query();  // Restore global post data stomped by the_post().
    			?>
  4. MichaelH
    Volunteer
    Posted 2 years ago #

    Looks okay.

  5. ryezack
    Member
    Posted 1 year ago #

    Hi MichaelH. I have replaced the wp_nav_menu of twentyten theme and applied the codes above to my website and it worked. Just one concern, how do we maintain the current active page feature of wordpress?

    Here's my site: http://www.inventivewebs.net/wp-sample/

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic