$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Check if Page Slug Exists, then display that Page's Title|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Check if Page Slug Exists, then display that Page's Title

matteradmin11PV0评论

Came across this function to check if a page exists by its slug and it works great

function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return true;
} else {
    return false;
}

}

but what i'm needing to do now is check if that exists, then output the title for that page, any help is appreciated.

so when i put this code on the page to lookup the slug:

<?php if (the_slug_exists('page-name')) {
                    echo '$post_title';
} ?>

everything is perfect other than the fact I need the function to output the title as well as lookup the slug, hopefully that makes more sense.

Came across this function to check if a page exists by its slug and it works great

function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return true;
} else {
    return false;
}

}

but what i'm needing to do now is check if that exists, then output the title for that page, any help is appreciated.

so when i put this code on the page to lookup the slug:

<?php if (the_slug_exists('page-name')) {
                    echo '$post_title';
} ?>

everything is perfect other than the fact I need the function to output the title as well as lookup the slug, hopefully that makes more sense.

Share Improve this question edited Mar 17, 2016 at 4:42 John Carlos asked Mar 17, 2016 at 1:23 John CarlosJohn Carlos 31 gold badge1 silver badge3 bronze badges 1
  • 1 In general, the_ prefixed functions echos its output and get_ prefixed functions returns – Pieter Goosen Commented Mar 17, 2016 at 4:28
Add a comment  | 

3 Answers 3

Reset to default 6

looking at MySQL query in the function it becomes clear that though it matches the post_name and returns true or false. What you want to find is, if a page exists by its slug. So, in that case, the function you have chosen is wrong.

To get the page by title you can simply use:

$page = get_page_by_title( 'Sample Page' );
echo $page->title

Just in case if you want to get page by slug you can use following code:

function get_page_title_for_slug($page_slug) {

     $page = get_page_by_path( $page_slug , OBJECT );

     if ( isset($page) )
        return $page->post_title;
     else
        return "Match not found";
}

You may call above function like below:

echo "<h1>" . get_page_title_for_slug("sample-page") . "</h1>";

Let me know if that helps.

It is almost always never adviced to run custom SQL when WordPress have native functions to do a specific job. The slug or post_name of a page is also the used in the page's path (URL) to identify and return the content of that page when such a page is visited. This means that, we can use get_page_by_path() and just pass the slug (post_name) to the function and get the returned page object.

get_page_by_path ( 
    string $page_path, 
    string $output = OBJECT, 
    string|array $post_type = 'page' 
)

Like many other functions and naming conventions (like post_name which is actually the slug, and not the name) in WordPress, get_page_by_path()'s name is confusing and IMHO stupid as you can pass any post type to the function

We can use get_page_by_path() which will return nothing if the page is not found, or the page object on success. Lets write a proper wrapper function which will return false on failure and will always return the page object regardless if the page exists

function get_post_by_post_name( $slug = '', $post_type = '' )
{
    //Make sure that we have values set for $slug and $post_type
    if (    !$slug
         || !$post_type
    )
        return false;

    // We will not sanitize the input as get_page_by_path() will handle that
    $post_object = get_page_by_path( $slug, OBJECT, $post_type );

    if ( !$post_object )
        return false;

    return $post_object;
}

You can then use it as follow

if ( function_exists( 'get_post_by_post_name' ) ) {
    $post_object = get_post_by_post_name( 'post-name-better-known-as-slug', 'your_desired_post_type' );
    // Output only if we have a valid post 
    if ( $post_object ) {
        echo apply_filters( 'the_title', $post_object->post_title );
    }
}

If you really just need the post title returned, we can slightly alter the function

function get_post_title_by_post_name( $slug = '', $post_type = '' )
{
    //Make sure that we have values set for $slug and $post_type
    if (    !$slug
         || !$post_type
    )
        return false;

    // We will not sanitize the input as get_page_by_path() will handle that
    $post_object = get_page_by_path( $slug, OBJECT, $post_type );

    if ( !$post_object )
        return false;

    return apply_filters( 'the_title', $post_object->post_title );
}

and then use it as follow

if ( function_exists( 'get_post_title_by_post_name' ) ) {
    $post_title = get_post_title_by_post_name( 'post-name-better-known-as-slug', 'your_desired_post_type' );
    // Output only if we have a valid post 
    if ( $post_title ) {
        echo $post_title;
    }
}

Here is an update to the same code for you

function the_slug_exists($post_name) {
global $wpdb;
//Sanitize title
$post_name = sanitize_title($post_name);
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
    return $post_name;
} else {
    return false;
}

then you can run it by simply calling it with the echo function as in the following code, which will only print the post name if it exists.

echo the_slug_exists($post_name); 

UPDATE

Alternatively you can rely on the default conditional WordPress function is_page() so your code becomes as simple as follows..

if( is_page($post_name) ):

   echo $post_name; 

endif;
Post a comment

comment list (0)

  1. No comments so far