通过帖子标题获取 wordpress 帖子 [英] get wordpress post by post title
本文介绍了通过帖子标题获取 wordpress 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码在外部网站中显示 wordpress 帖子:
i am using this code to show wordpress posts in an external website:
<?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选择帖子,如何让它根据帖子标题选择帖子?
rather than selecting the post based on the ID, how can i make it select the post based on the post title?
推荐答案
您可以使用 get_page_by_title()
来按标题获取帖子.像这样:
You can use get_page_by_title()
to fetch post by title.Like this:
$page = get_page_by_title('About', OBJECT, 'post');
echo $page->ID
详情在这里.
或者像这样的自定义查询:
OR custom query like this :
$posttitle = 'About';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;
这篇关于通过帖子标题获取 wordpress 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文