How to select a specific page

I'm having an issue selecting a page.

There are guides that tell you how to find the page ID and I just couldn't find it in the admin panel to figure out what the page ID is. If only there was a way to search by slug in the admin panel that would be great.

the other way was to look at the elements which worked beautifully for me.

I used that and that worked great!

$classes = get_body_class();
if (in_array('page-magazine', $classes))..

Unfortunately I was told that this isn't good practice and got rejected.

Could anybody suggest a better way to target a specific page?

Topic wp-blog-header.php pages Wordpress

Category Web


I may not have been clear with my question and I apologize. I mainly just wanted to target the page in anyway I can whether it's by slug, ID, etc. I wasn't able to get the page ID the admin way because I couldn't find it in the page list.

I discovered another way to target the specific page and I hope that it gets approved.

In function.php

function slug_name() {
    $page_slug = trim( $_SERVER["REQUEST_URI"] , '/' );
    return $page_slug;
}

And then in header.php I just use an if statement to see if it matches with magazine.

if (slug_name() == 'magazine') {...}

Hopefully this one isn't too dirty but it does the job just like the class search one.


My understanding of your question is that you want to figure out the ID of a post/page on the Admin Post/Pages list.

On the Admin Posts (or Pages) screen, if you hover over the post's name (or one of the links), you will see an ID in the linked URL.

There are plugins that will add an "ID" column to the post list. One is Reveal ID https://wordpress.org/plugins/reveal-ids-for-wp-admin-25/. That's the easiest way, although it requires a plugin.


From WordPress Codex Get Posts.

<?php
$the_slug = 'my_slug';
$args = array(
  'name'        => $the_slug,
  'post_type'   => 'post',
  'post_status' => 'publish',
  'numberposts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) :
  echo 'ID on the first post found ' . $my_posts[0]->ID;
endif;
?>

That way you can get a post or page by its slug.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.