如何在wordpress中按页面标题获取页面ID

时间:2014-09-15 05:15:50

标签: wordpress

在这些情况下,当单词为空格

时,我无法按页面标题获取页面ID
  1. 想想
  2. 你好吗
  3. 但是当我的单词标题像"用户"

    时,我能够获得id

    我的代码在这里

    function get_ID_by_page_name($page_name)
    {
        global $wpdb;
        $page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
        return $page_name_id;
    }
    
    
    
    $thinkid = ks_get_option('ks_thinkid'); //getting page title from theme option
    echo $thinkid; // works successfull till this 
    
    $the_page_id = get_ID_by_page_name($thinkid);
    echo $the_page_id;
    

4 个答案:

答案 0 :(得分:2)

$page = get_page_by_title( 'How are you' ); //as an e.g.

echo $page->ID;

答案 1 :(得分:2)

最后我为此找到了一个解决方案,谢谢你能帮我找到像我这样的页面标题页面标题的人才是解决方案

function get_page_id_by_title($title)
{
$page = get_page_by_title($title);
return $page->ID;
}


get_page_id_by_title('how are you');

答案 2 :(得分:1)

global $wpdb;

$page_name = "Your page title";

$page_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name
 = '".$page_name."' AND post_type = 'page'");

return $page_id;

从此链接获取的代码:http://chandreshrana.blogspot.in/2016/03/how-to-get-page-id-from-page-title.html

答案 3 :(得分:0)

尝试此操作以从页面标题获取页面ID:

$page = get_page_by_title( 'Sample Page' );