PHP学习笔记之获取文章内容(2种方法)

 theme/../page.php


<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */


ini_set('display_errors',true);
error_reporting(E_ALL);


echo "测试查询数据库返回数据<br/>";


$postID = $_GET['page_id'];
echo "postID = $postID<br/>";


//1.通过get_post()方法获取content
echo get_post($postID)->post_title;
echo '<br />';
echo get_post($postID)->post_content;
echo "<br/>";




//2.通过wpdb sql 查询方法获取content
/*$fivesdrafts = $wpdb->get_results( 
"
SELECT * 
FROM $wpdb->posts
WHERE 
ID = $postID
"
);


echo "输出数据库查询数据<br/>";
foreach ( $fivesdrafts as $fivesdraft ) 
{
echo $fivesdraft->post_title;
echo $fivesdraft->post_content;
}*/


die();
?>



<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

ini_set('display_errors',true);
error_reporting(E_ALL);

$postID = $_GET['page_id'];

//1.通过get_post()方法获取content
//echo get_post($postID)->post_title;
//echo "<br/>";
//echo get_post($postID)->post_content;
//echo "<br/>";

echo json_encode(get_post($postID)->post_content);


//2.通过wpdb sql 查询方法获取content
/*$fivesdrafts = $wpdb->get_results( 
	"
	SELECT * 
	FROM $wpdb->posts
	WHERE 
	ID = $postID
	"
);

echo "输出数据库查询数据<br/>";
foreach ( $fivesdrafts as $fivesdraft ) 
{
	echo $fivesdraft->post_title;
	echo $fivesdraft->post_content;
}*/

die();
?>


<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

ini_set('display_errors',true);
error_reporting(E_ALL);

$postID = $_GET['page_id'];

//1.通过get_post()方法获取content
/*echo get_post($postID)->post_title;
echo "<br/>";
echo get_post($postID)->post_content;
echo "<br/>";*/


$post_content = get_post($postID)->post_content;

$pageContent = array('content' => strip_tags($post_content));
var_dump($pageContent);die();
echo json_encode($pageContent);


//2.通过wpdb sql 查询方法获取content
/*$fivesdrafts = $wpdb->get_results( 
	"
	SELECT * 
	FROM $wpdb->posts
	WHERE 
	ID = $postID
	"
);

echo "输出数据库查询数据<br/>";
foreach ( $fivesdrafts as $fivesdraft ) 
{
	echo $fivesdraft->post_title;
	echo $fivesdraft->post_content;
}*/

die();
?>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值