WordPress.org

Ready to get started?Download WordPress

Forums

Using get_page- is there a better way? (3 posts)

  1. Joshuwar
    Member
    Posted 4 years ago #

    Hi, Thanks for looking.

    I'm trying to pull part of a page into my sidebar, throughout my site. I started using query_posts, but this didn't work, because it changes the main query for the page, so my main post would come up as the sidebar page.

    I've found a solution in using get_page, but I'm wondering if there's a better way to do it- specifically whether there's a way to obtain the page by title rather than id...

    Here's the code I'm using:

    <?php
    $page_id = 2;
    $page_data = get_page( $page_id );
    
    $title = $page_data->post_title; // Get title
    $content = $page_data->post_content; // Get Content
    
    echo $title;
    echo $content;
    ?>

    Any help would be much appreciated..

    J

  2. MichaelH
    Member
    Posted 4 years ago #

    <?php
        $args=array(
          'pagename' => 'About',
          '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';
          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
          endwhile;
        }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. Joshuwar
    Member
    Posted 4 years ago #

    Thanks for this. I had no idea about wp_reset_query and didn't know how to describe it to search for it. Much appreciated.

    J

Topic Closed

This topic has been closed to new replies.

About this Topic