Get post by page name or slug

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.

Read More
<?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.

Related posts

Leave a Reply

2 comments

  1. 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?

  2. 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.