开发者

Wordpress query_posts by title instead of page_id?

Let's say the page i'm displaying has the title "About" and the id is 49. Is there a开发者_如何学JAVA way to change it so it pulls the pages title instead of the id.

At the moment the code I am using is as follows:

<?php
    query_posts('page_id=49');
    while (have_posts()) : the_post(); ?>
    <?php the_content();
    endwhile;
?>

Thanks for your help!


I used something similar on a project, using the post's slug; save this in your theme's functions.php:

if ( ! function_exists( 'get_content_by_slug' ) ) :
/**
 * Get content using a slug
 *
 * @param   string
 * @return  string
 */    
function get_content_by_slug($page_slug) {
    $page = get_page_by_path($page_slug);
    if ($page) {
        $page_content = $page->post_content;
        $page_content = apply_filters('the_content', $page_content);            
        return $page_content;
    } else {
        return null;
    }
}
endif;


Usage:

<?php echo get_content_by_slug('page-slug'); ?>


Get WordPress Post ID from Post title

Found this for you. You want to be careful however, because titles from posts and pages can change. I'm sure you have your reasons.

EDIT

This may be better actually... http://codex.wordpress.org/Function_Reference/get_page_by_title

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜