【问题标题】:echo custom post type ID from custom post title WordPress从自定义帖子标题 WordPress 中回显自定义帖子类型 ID
【发布时间】:2013-10-25 13:48:42
【问题描述】:

如果一直在寻找几个小时 :-( 希望任何人都可以帮助我。

我在一个名为“grhdogs”的自定义帖子类型中。在那里,我需要获取该页面的当前页面标题,并在另一个名为“slideshow”的自定义帖子类型中使用该标题进行搜索,并在“slideshow”中回显(作为字符串)匹配页面标题的页面 ID。

背景是在这两种自定义帖子类型中始终匹配页面标题。我必须将它们放在一起,因为“grhdogs”是一个帖子,而“slideshow”是我想在帖子中显示的匹配幻灯片。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    你可以试试这样的。

    <?php
    //get the current title of the page
    $postTitle = get_the_title(); 
    
    //search for similer post in db table
    $postID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE   post_type='slideshow' AND post_title = %s",$postTitle));
    
    //print id
     echo $postID;  
    
    //or use get_post($post_id) to get whatever you want
    $getCustomPost = get_post($postID);
    echo $getCustomPost->id;
    ?>
    

    【讨论】:

    • 您好,感谢您的帮助。我快到了。我只需要在“幻灯片”中回显匹配帖子的帖子 ID。所以我需要回显“the_ID();” $postID 的结果。
    • 首先检查 print_r($postID) 以检查您的 id,而不是像 echo $getCustomPost->id 或 echo $getCustomPost->ID 那样打印它;
    • 喜欢吗? $getCustomPost = print_r($postID); echo $getCustomPost->ID;
    • 没有 print_r($postID) 只会打印整个对象/数组。 print_r 只是为了检查你的价值观。您无法使用 print_r 打印该值。您必须使用 echo $getCustomPost->id 来打印 id。
    • 我已经修改了我的代码以打印 id。如果你只需要 id 而不是删除最后两行代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    相关资源
    最近更新 更多