我正在使用此代码在外部网站中显示 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/