我正在使用这段代码来显示外部网站中的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选择帖子不同,我如何让它根据帖子标题选择帖子?
发布于 2014-01-10 17:55:47
您可以通过以下title.Like使用get_page_by_title()获取post:
$page = get_page_by_title('About', OBJECT, 'post');
echo $page->ID详情请参阅here。
OR自定义查询如下所示:
$posttitle = 'About';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;发布于 2017-05-09 01:39:34
尝尝这个
$post_title = 'Contact';
$post_id = get_page_by_title($post_title, OBJECT, 'your_post_type_name');
echo $post_id->ID; // your id发布于 2014-01-10 20:14:21
像这样添加函数
function get_page_by_post_name($post_name, $output = OBJECT, $post_type = 'post' ){
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $post_name, $post_type ) );
if ( $page ) return get_post( $page, $output );
return null;
}
add_action('init','get_page_by_post_name');然后像这样跑:
$page = get_page_by_post_name('hello-world', OBJECT, 'post');
echo $page->ID; https://stackoverflow.com/questions/21040938
复制相似问题