Get post by page name or slug

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?

Leave a Comment