- This topic is empty.
- AuthorPosts
-
January 29, 2011 at 3:09 am #9412
keatch
ParticipantOk, you must grab the page title before the loop.
$page_title = $wp_query->post->post_title;
Check for the reference: http://codex.wordpress.org/Function_Reference/WP_Query#Properties.
Do a
print_r($wp_query)
before the loop to see all the values of the
$wp_query
object.January 29, 2011 at 3:58 am #9415ajj
ParticipantThe WordPress global variable
$pagename
should be available for you. I have just tried with the same setup you specified.$pagename
is defined in the file wp-includes/theme.php, inside the functionget_page_template()
, which is of course is called before your page theme files are parsed, so it is available at any point inside your templates for pages.-
Although it doesn’t appear to be documented, the
$pagename
var is only set if you use permalinks. I guess this is because if you don’t use them, WordPress doesn’t need the page slug, so it doesn’t set it up. -
$pagename
is not set if you use the page as a static front page.
- This is the code inside /wp-includes/theme.php, which uses the solution you pointed out when
$pagename
can’t be set:
—
if ( !$pagename && $id > 0 ) { // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object $post = $wp_query->get_queried_object(); $pagename = $post->post_name; }
January 29, 2011 at 12:58 pm #9396bernard
ParticipantWhat PHP code can be used to retrieve the current page name in a WordPress theme?
All the solutions I have seen so far:
the_title()
get_page()->post_name
get_post()
- etc.
But these don’t work for a page that contains post entries. They will all return the name of the latest blog entry.
Stated another way, assume that you have a page created in WordPress with the name "My News". This page is set as the "post page". Add a couple of posts to the page.
Now, what API can be used to retrieve the string "my-news" instead of the name of the latest post?
I’ve found the following variable which seems to work.
$wp_query->queried_object->post_name
This is actually the URL friendly version of the page name (slug), which is what I was looking for too. This was tested with the default template (Twenty Ten). I’m really not sure why the two variables given below do not work on my site. Thanks to keatch for the
print_r()
tip.Now, why is this information hidden so deep down?
March 10, 2011 at 5:24 am #9413gregb
Participant<?php wp_title(''); ?>
This worked for me.
If I understand correctly, you want to get the page name on a page that has post entries.
September 2, 2011 at 8:57 am #9400tom-auger
ParticipantOne option, if you’re looking for the actual queried page, rather than the page ID or slug is to intercept the query:
add_action('parse_request', 'show_query', 10, 1);
Within your function, you have access to the $wp object and you can get either the pagename or the post name with:
function show_query($wp){ if ( ! is_admin() ){ // heck we don't need the admin pages echo $wp->query_vars['pagename']; echo $wp->query_vars['name']; } }
If, on the other hand, you really need the post data, the first place to get it (and arguably in this context, the best) is:
add_action('wp', 'show_page_name', 10, 1); function show_page_name($wp){ if ( ! is_admin() ){ global $post; echo $post->ID, " : ", $post->post_name; } }
Finally, I realize this probably wasn’t the OP’s question, but if you’re looking for the Admin page name, use the global
$pagenow
.September 30, 2011 at 11:10 am #9414goksel
ParticipantMy approach to get the slug name of the page:
$slug = basename(get_permalink());
October 27, 2011 at 6:40 am #9405jerad-rutnam
ParticipantUse:
$title = get_the_title($post); $parent_title = get_the_title($post->post_parent); echo $title; echo $parent_title;
March 9, 2012 at 2:46 am #9410james
ParticipantYou can get the current page, post, or custom post type with the global variable
$post
:echo $post->post_title
Note: In a function or class you’ll need to specify
global $post;
prior to trying to use$post
.If you have loops on your page, make sure you end each loop with
wp_reset_postdata();
to set$post
back to the default item being displayed (the page).Note, the ‘post_title’ variable is also available for any custom loop / query… including menu items and media attachments… everything in WordPress is a ‘post’.
May 23, 2012 at 3:12 am #9397sandra
ParticipantI’ve now found this function on WordPress Codec,
which is a wrapper for
$wp_query->get_queried_object
.This post put me in the right direction, but it seems that it needs this update.
September 10, 2012 at 10:41 am #9407mohammad-sabbagh
ParticipantTry this:
$pagename = get_query_var('pagename');
April 4, 2013 at 8:57 am #9409tom-auger
ParticipantIf you’re looking to access the current page from within your functions.php file (so, before the loop, before
$post
is populated, before$wp_query
is initialized, etc…) you really have no choice but to access the server variables themselves and extract the requested page from the query string.$page_slug = trim( $_SERVER["REQUEST_URI"] , '/' )
Note that this is a “dumb” solution. It doesn’t know, for instance that the page with the slug ‘coming-soon’ is also
p=6
. And it assumes that your permalink settings are set topagename
(which they should be anyway!).Still, can be a useful little trick if you have a controlled scenario. I’m using this in a situation where I wish to redirect non-logged in visitors to a “coming soon” page; but I have to make sure that I’m not throwing them into the dreaded “redirect loop”, so I need to exclude the “coming soon” page from this rule:
global $pagenow; if ( ! is_admin() && 'wp-login.php' != $pagenow && 'coming-soon' != trim( $_SERVER["REQUEST_URI"] , '/' ) && ! is_user_logged_in() ){ wp_safe_redirect( 'coming-soon' ); }
June 21, 2013 at 6:53 am #9398toto_tico
ParticipantThis also works if you are in the functions.php. It is not the best approach since you have to use the global array, but it works.
-
First, we need to add a filter. There must exist a better filter to use than the template_include, but I don’t know all of them. Please point me to the right one.
add_filter( 'template_include', 'var_template_include', 1000 ); function var_template_include( $template ){ global $wp_query; $GLOBALS['current_page'] = $wp_query->get_queried_object()->post_name; return $template; }
-
Avoid using the variable directly
function get_current_page( $echo = false ) { if( !isset( $GLOBALS['current_page'] ) ) return false; return $GLOBALS['current_page']; }
-
Now you can use the function
get_current_page()
in any other part of the functions.php.
September 9, 2013 at 2:42 am #9406tech1337
ParticipantI have come up with a simpler solution.
Get the returned value of the page name from wp_title(). If empty, print homepage name, otherwise echo the wp_title() value.
<?php $title = wp_title('', false); ?>
Remember to remove the separation with the first argument and then set display to false to use as an input to the variable. Then just bung the code between your heading, etc. tags.
<?php if ( $title == "" ) : echo "Home"; else : echo $title; endif; ?>
It worked a treat for me and ensuring that the first is declared in the section where you wish to extract the
$title
, this can be tuned to return different variables.December 19, 2013 at 8:03 am #9401umair-hamid
ParticipantWithin the WordPress Loop:
if ( have_posts() ) : while ( have_posts() ) : the_post(); /******************************************/ echo get_the_title(); /******************************************/ endwhile; endif;
This will show you the current page title.
For reference: get_the_title()
January 29, 2014 at 10:47 am #9403bowenac
ParticipantThis seems to be the easiest to use:
<?php single_post_title(); ?>
-
- AuthorPosts
- You must be logged in to reply to this topic.