4

I know this is very avant-garde, but bear with me. I've read that one is able to query a page by the page/post name or slug. I'm trying to do this because I need information from a page with a similar title/slug and will not have the ability to get the page id (unless there's a way to convert a title to an ID).

I've tried multiple variations without success. This seems like the most reasonable way of handling this, but it's simply not working.

<?php 
    $args = array(
    'pagename' => 'CM-145',
    'post_type' => 'page',
    'posts_per_page' => 1,
    'numberposts' => 1
); ?>
<div>
    <?php
        query_posts( $args );
        get_template_part( 'loop' );
        wp_reset_query();
    ?>
</div>

The Loop

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php if ( is_front_page() ) { ?>
                        <h2 class="entry-title"><?php the_title(); ?></h2>
                    <?php } else { ?>
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    <?php } ?>

                    <div class="entry-content">
                        <?php the_content(); ?>
                        <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                        <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                    </div><!-- .entry-content -->
                </div><!-- #post-## -->

                <?php comments_template( '', true ); ?>

<?php endwhile; // end of the loop. ?>

I've also tried 'name' => 'CM-145', I'm not sure if this is the correct or reasonable way to perform this action. Ultimately I simply need to pull the page's thumb and excerpt so if you have a better Idea don't hesitate to let me know.

Thanks in advance.

2 Answers 2

3

Hi @Zach Shallbetter:

If I understand your question, then you are looking to solve your problem using theming functions when you really need to use more of WordPress' API. The following code can be copied to a test.php file and run using http://yoursite.com/test.php for you to see how it works (assuming you replace http://yoursite.com with your own website's domain, of course!) Then read the comments to see where to place the code for use in your site:

<?php 

// The function should go into your theme's functions.php file
function get_excerpt( $post_id ) {
  $post = get_post( $post_id );
  $excerpt = $post->post_excerpt;
  return ( post_password_required($post) ? false : 
     apply_filters( 'get_the_excerpt', $excerpt ) );
}

include('../wp-load.php');

// This code goes where you need to get and display the excerpt and thumbnail
$post = get_page_by_path('CM-145');
$excerpt = get_excerpt($post->ID);
$thumbnail = get_the_post_thumbnail($post->ID);
?>
<div style="width:300px">
<span style="float:right;"><?php echo $thumbnail; ?></span>
<?php echo $excerpt ?>
</div>

Also, I'm a bit concerned you may be experiencing a bit of the "hammer-and-nail" syndrome; i.e. when you have a problem and you only have a hammer you treat the problem like a nail when maybe what you need is to find a screwdriver?

Specifically I'm concerned that you are using a Page for something that should either simply be an Option, or maybe at least a Custom Post Type? Can you explain your use-case more in-depth, and why you have chosen to use a Page?

8
  • Absolutely, I'm currently working on redesign.openeye.net. I have the products set as pages and all of the other little add-on as posts (videos, software, documents, etc.) These items (posts) are categorized by model number so multiple posts have the ability to share a single model number. We have a situation where a customer visits the support page chooses the category from the drop down and then sees a list of the "posts" that are associated with that product. What I'd like to do is pull the product page's image and description to put at the top of this page. Apr 22, 2011 at 16:58
  • Also, I'm using custom post types, fields and taxonomies pretty vigorously. Very little of this site is generated by hand. Oh, and pardon the inline styles everywhere, I'm still working on the design. Apr 22, 2011 at 17:01
  • @Zach Shallbetter - Why not create a 'product' and 'add-on' custom post types? Apr 22, 2011 at 17:29
  • Because they would clash with the category listings and we put in an immense amount of time so it's not cost effective until I begin resolving bugs. Also the permalink is prettier redesign.openeye.net/products/recorders/e-series/oe2-e480 Apr 22, 2011 at 17:46
  • For the sake of posterity, I'm still curious as to why his code, which should be working (and incidentally, works for me), is not working.
    – Milo
    Apr 22, 2011 at 17:47
1

change get_template_part( 'loop' ); to get_template_part( 'loop','page' ); or manually insert a loop directly into that template and see if that's the issue. your code is otherwise correct.

*edit- I'm guessing your loop file has some conditional checks to determine how to display posts in different contexts that aren't met by your page query, so nothing is displayed.

4
  • Yeah I thought it was correct too, but it doesn't want to return any results at all. Even with page in the template. Even at a minimum it should return the page's title. I wonder if there's something else I'm missing. Apr 22, 2011 at 16:14
  • is that your whole loop? it's missing the_post();
    – Milo
    Apr 22, 2011 at 16:17
  • I created a page with the title "CM-145" and copy/pasted your code. when I inspect $wp_query, it is indeed returned, but the loop you posted here doesn't work without a call to the_post(); first. as a sanity check I'd print_r($wp_query); to see if it's a query or loop issue.
    – Milo
    Apr 22, 2011 at 16:26
  • Sorry bad copy, this is just the default loop Apr 22, 2011 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.