wordpress首页如何展示其他页的标题和内容

wordpress 函数get_post( int|WP_Post|null $post = null, string $output = OBJECT, string $filter = ‘raw’ ), 展现特定post的页面或者帖子数据。get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = ‘page’ ) 获取特定title的页面,如get_page_by_title('World Peace Now', OBJECT, 'link'); 如果要获取页面标题为about us的页面内容的话,用法如下:

get_post(get_page_by_title(‘About Us’));

展现的内容为:

object(WP_Post)#871 (24) { [“ID”]=> int(21) [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2020-11-13 12:27:10” [“post_date_gmt”]=> string(19) “2020-11-13 12:27:10” [“post_content”]=> string(512) “

浙江景祥是一家专业研发、生产、销售制冷换热设备系列产品的综合性制造企业,为家用空调、商用空调、汽车空调、船用空调、新能源、轨道交通、工业制冷、冷冻冷藏等领域提供专业化、系统化的制冷解决方案和设备配套,年生产能力达60万套。公司现为国家高新技术企业、浙江省科技型企业、AAA级信用企业和诚信经营示范单位。” [“post_title”]=> string(8) “About Us” [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish” [“comment_status”]=> string(6) “closed” [“ping_status”]=> string(6) “closed” [“post_password”]=> string(0) “” [“post_name”]=> string(8) “about-us” [“to_ping”]=> string(0) “” [“pinged”]=> string(0) “” [“post_modified”]=> string(19) “2020-11-14 13:46:06” [“post_modified_gmt”]=> string(19) “2020-11-14 13:46:06” [“post_content_filtered”]=> string(0) “” [“post_parent”]=> int(0) [“guid”]=> string(33) “http://www.method.top/?page_id=21” [“menu_order”]=> int(0) [“post_type”]=> string(4) “page” [“post_mime_type”]=> string(0) “” [“comment_count”]=> string(1) “0” [“filter”]=> string(3) “raw” },

如果要调取about us页面的内容的话,用法为:

$about = get_post(get_page_by_title(‘About Us’)); echo $about->post_content;

还有一种选取页面的方式为:get_page_by_path().

另一种获取页面内容的方式为:

<?php 
$id=47; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content; 

中文字符要截取特定字数的话写法是:echo mb_strimwidth(get_the_excerpt(),0,100,’ …..’),

当然,你也可以用wp_query来创建自定义查询到page的页面,用法如下:

Display post by ID:

$query = new WP_Query( array( 'p' => 7 ) );

Display page by ID:

$query = new WP_Query( array( 'page_id' => 7 ) );

Show post/page by slug

$query = new WP_Query( array( 'name' => 'about-my-life' ) );

Display page by slug:

$query = new WP_Query( array( 'pagename' => 'contact' ) );

Display child page using the slug of the parent and the child page, separated by a slash (e.g. ‘parent_slug/child_slug’):

$query = new WP_Query( array( 'pagename' => 'contact_us/canada' ) );

Display child pages using parent page ID:

$query = new WP_Query( array( 'post_parent' => 93 ) );

Display only top-level pages, exclude all child pages:

$query = new WP_Query( array( 'post_parent' => 0 ) );

Display posts whose parent is in an array:

$query = new WP_Query( array( 'post_parent__in' => array( 2, 5, 12, 14, 20 ) ) );

Display only the specific posts:

$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );

Display all posts but NOT the specified ones:

$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

Note: you cannot combine post__in and post__not_in in the same query.

Also note that using a string containing a comma separated list will not work here. If you’re passing a variable, make sure it’s a proper array of integer values:

// This will NOT work
$exclude_ids = '1,2,3';
$query = new WP_Query( array( 'post__not_in' => array( $exclude_ids ) ) );

// This WILL work
$exclude_ids = array( 1, 2, 3 );
$query = new WP_Query( array( 'post__not_in' => $exclude_ids ) );

几个具体的参数:
p (int) – use post id.
name (string) – use post slug.
page_id (int) – use page id.
pagename (string) – use page slug.
post_parent (int) – use page id to return only child pages. Set to 0 to return only top-level entries.
post_parent__in (array) – use post ids. Specify posts whose parent is in an array. (available since version 3.6)
post_parent__not_in (array) – use post ids. Specify posts whose parent is not in an array. (available since version 3.6)
post__in (array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.
post__not_in (array) – use post ids. Specify post NOT to retrieve.
post_name__in (array) – use post slugs. Specify posts to retrieve. (Will be available in version 4.4)
Share :