Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoredrew/db20d5f4c3f3d09adf7732336f826bb1 to your computer and use it in GitHub Desktop.
Save bmoredrew/db20d5f4c3f3d09adf7732336f826bb1 to your computer and use it in GitHub Desktop.
Get the WordPress post ID no matter where you are.

This extends the built-in WordPress function get_the_ID() to return the post ID both inside and outside the loop.

Used outside the loop (in header.php):

<?php if ( function_exists( 'gt_hide_nav' ) && ! gt_hide_nav() ) : ?>
  <nav role="navigation">
    <?php if ( function_exists( 'bones_main_nav' ) ) bones_main_nav(); ?>
  </nav>
<?php endif; // hide nav ?>

Used inside the loop on a page (part of a shortcode):

<?php
function gt_wistia_enqueue() {
  if ( has_shortcode( 'wistia' ) ) {
		wp_enqueue_script( 'wistia-embed-shepherd', '//fast.wistia.com/static/embed_shepherd-v1.js', array(), 'v1', false );
	}
}
add_filter( 'the_post', 'gt_wistia_enqueue' );
<?php
/**
* Gets the ID of the post, even if it's not inside the loop.
*
* @uses WP_Query
* @uses get_queried_object()
* @extends get_the_ID()
* @see get_the_ID()
*
* @return int
*/
function gt_get_the_ID() {
if ( in_the_loop() ) {
$post_id = get_the_ID();
} else {
/** @var $wp_query wp_query */
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
}
return $post_id;
}
/**
* Hide nav if option checked in page editor.
*
* @return bool
*/
function gt_hide_nav() {
$post_id = gt_get_the_ID();
$meta_key = '_gt_hide_nav';
$hide_nav = false;
if ( ! empty( $post_id ) ) {
$hide_nav = 'on' == get_post_meta( $post_id, $meta_key, true ) ? true : false;
}
return $hide_nav;
}
/**
* Check the current post for the existence of a short code
*
* @link http://wp.tutsplus.com/articles/quick-tip-improving-shortcodes-with-the-has_shortcode-function/
*
* @param string $shortcode
*
* @return bool
*/
function has_shortcode( $shortcode = '' ) {
//global $post;
//$post_to_check = get_post();
$post_to_check = get_post( gt_get_the_ID() );
// false because we have to search through the post content first
$found = false;
// if no short code was provided, return false
if ( ! $shortcode ) {
return $found;
}
// check the post content for the short code
if ( false !== stripos( $post_to_check->post_content, '[' . $shortcode ) ) {
// we have found the short code
$found = true;
}
// return our final results
return $found;
}
<?php
// Add to theme's functions.php file.
/**
* Gets the ID of the post, even if it's not inside the loop.
*
* @uses WP_Query
* @uses get_queried_object()
* @extends get_the_ID()
* @see get_the_ID()
*
* @return int
*/
function gt_get_the_ID() {
if ( in_the_loop() ) {
$post_id = get_the_ID();
} else {
/** @var $wp_query wp_query */
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
}
return $post_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment