Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 310 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get the current page name in WordPress?

#11
I've now found this function on *WordPress Codec*,

*[get queried][1]*

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.

[1]:

[To see links please register here]

Reply

#12
This 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.

1. 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;
}

2. Avoid using the variable directly

function get_current_page( $echo = false ) {
if( !isset( $GLOBALS['current_page'] ) )
return false;
return $GLOBALS['current_page'];
}

3. Now you can use the function `get_current_page()` in any other part of the functions.php.
Reply

#13
I 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.



Reply

#14
Within the [WordPress *Loop*][1]:

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()][2]*


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#15
This seems to be the easiest to use:

<?php single_post_title(); ?>

Reply

#16
Here's my version:

$title = ucwords(str_replace('-', ' ', get_query_var('pagename')));

get_query_var('pagename') was just giving me the page slug. So the above replaces all the dashes, and makes the first letter of each word uppercase - so it can actually be used as a title.



Reply

#17
I believe that the [Roots starter theme][1] has a fantastic function to get the current page title. It is very hackable, covers all bases, and can be easily used with the `wp_title` hook.

/**
* Page titles
*/
function roots_title() {
if (is_home()) {
if (get_option('page_for_posts', true)) {
echo get_the_title(get_option('page_for_posts', true));
} else {
_e('Latest Posts', 'roots');
}
} elseif (is_archive()) {
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
if ($term) {
echo $term->name;
} elseif (is_post_type_archive()) {
echo get_queried_object()->labels->name;
} elseif (is_day()) {
printf(__('Daily Archives: %s', 'roots'), get_the_date());
} elseif (is_month()) {
printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
} elseif (is_year()) {
printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
} elseif (is_author()) {
$author = get_queried_object();
printf(__('Author Archives: %s', 'roots'), $author->display_name);
} else {
single_cat_title();
}
} elseif (is_search()) {
printf(__('Search Results for %s', 'roots'), get_search_query());
} elseif (is_404()) {
_e('Not Found', 'roots');
} else {
the_title();
}
}

[1]:

[To see links please register here]




Reply

#18
We just need to use the "post" global variable:

global $post;
echo $post->post_title;

This will echo the current page/post title.

Reply

#19
Show the title before the loop starts:

$page_title = $wp_query->post->post_title;
Reply

#20
The 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 function `get_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;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through