Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按帖子标题获取wordpress帖子_Php_Wordpress - Fatal编程技术网

Php 按帖子标题获取wordpress帖子

Php 按帖子标题获取wordpress帖子,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_t

我使用此代码在外部网站中显示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选择文章,如何使其根据文章标题选择文章?

您可以使用
按标题获取页面()
按标题获取文章。如下所示:

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

或像这样的自定义查询:


添加这样的函数

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; 
试试这个

$post_title = 'Contact';
$post_id = get_page_by_title($post_title, OBJECT, 'your_post_type_name');
echo $post_id->ID; // your id

你需要用slug显示页面吗?我试过:$page=get_page_by_title('Hello world!'));echo$page->ID,但它不返回任何内容通过第二和第三个参数传递
hello world
。第二和第三个参数是什么?像这样
get_page_by_title(('About',OBJECT,'post');
像这样?$page=get_page_by_title('hello world!',OBJECT,'post');
$post_title = 'Contact';
$post_id = get_page_by_title($post_title, OBJECT, 'your_post_type_name');
echo $post_id->ID; // your id