php - 按帖子标题获取 WordPress 帖子

标签 php wordpress

我正在使用此代码在外部网站中显示 WordPress 帖子:

<?php
    require('wp_blog/wp-blog-header.php');
    
    if($_GET["p"] > '') { ?>
    
    <?php query_posts('p='.$_GET["p"].''); ?>
    <?php while (have_posts()) : the_post(); ?>
        <h4><?php the_title(); ?></h4>
        <?php the_content(); ?>
    <?php endwhile; ?>
    <?php } else { ?>
    
    <?php
    $posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
    foreach ($posts as $post) : setup_postdata( $post ); ?>
        <?php the_date(); echo "<br />"; ?>
        <?php the_title(); ?>    
        <?php the_excerpt(); ?> 
    <?php endforeach; ?>
    
    <?php } ?>

如何让它根据帖子标题选择帖子,而不是根据 ID 选择帖子?

最佳答案

您可以使用 get_page_by_title() 按标题获取帖子。像这样:

$page = get_page_by_title('About', OBJECT, 'post');
echo $page->ID

详情为here .

OR custom query like this :

$posttitle = 'About';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;

关于php - 按帖子标题获取 WordPress 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040938/

相关文章:

javascript - 无法根据 div 中的当前值更新 div

javascript - WordPress 自动填写表单

php - 是否可以从插件扩展 Wordpress XMLRPC 接口(interface)?

php - 对 PHP 程序进行负载平衡以支持不断增加的用户?

PHP:将数组堆叠在表中的多个列上

php - 哇哦!此页面没有请求 token

wordpress - 使用 wordpress 和 ssl 要求托管多个站点

html - Div 没有响应 [基础]

android - WooCommerce API : create order and checkout

php - MySQL 连接 ('max_user_connections' )