通过 slug/ID 在 wordpress 中查询页面
Query page in wordpress by slug/ID
我想使用 id 或 slug 显示我的页面内容。我设法在其他一些 WP 主题中做到了这一点,但出于某种原因,这次它不起作用...
我想显示标题、摘录和特色图片
这是我的代码:
<h1 class="text-center light-title"><?php echo the_title() ?></h1>
<span class="light-text"><?php the_excerpt(); ?></span>
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full', array( 'class' => 'center-block img-responsive')); // show featured image
}
?>
<?php
query_posts("page_id=222");
while ( have_posts() ) : the_post()
?>
<?php //the_content() ?>
<?php
endwhile;
wp_reset_query();
?>
你没有说失败的地方,但你的代码顺序似乎不对。 the_title() 和 the_excerpt() 通常 在 循环中。像这样:
<?php
query_posts("page_id=222");
while ( have_posts() ) : the_post()
?>
<h1 class="text-center light-title"><?php echo the_title() ?></h1>
<span class="light-text"><?php the_excerpt(); ?></span>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full', array( 'class' => 'center-block img-responsive')); // show featured image
}
?>
<?php //the_content() ?>
<?php
endwhile;
wp_reset_query();
使用 WP_Query
而不是 query_posts
可以让您更好地控制范围:
<?php
$page = new WP_Query("page_id=374");
while ( $page->have_posts() ) : $page->the_post();
the_content();
endwhile;
?>
我想使用 id 或 slug 显示我的页面内容。我设法在其他一些 WP 主题中做到了这一点,但出于某种原因,这次它不起作用...
我想显示标题、摘录和特色图片
这是我的代码:
<h1 class="text-center light-title"><?php echo the_title() ?></h1>
<span class="light-text"><?php the_excerpt(); ?></span>
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full', array( 'class' => 'center-block img-responsive')); // show featured image
}
?>
<?php
query_posts("page_id=222");
while ( have_posts() ) : the_post()
?>
<?php //the_content() ?>
<?php
endwhile;
wp_reset_query();
?>
你没有说失败的地方,但你的代码顺序似乎不对。 the_title() 和 the_excerpt() 通常 在 循环中。像这样:
<?php
query_posts("page_id=222");
while ( have_posts() ) : the_post()
?>
<h1 class="text-center light-title"><?php echo the_title() ?></h1>
<span class="light-text"><?php the_excerpt(); ?></span>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full', array( 'class' => 'center-block img-responsive')); // show featured image
}
?>
<?php //the_content() ?>
<?php
endwhile;
wp_reset_query();
使用 WP_Query
而不是 query_posts
可以让您更好地控制范围:
<?php
$page = new WP_Query("page_id=374");
while ( $page->have_posts() ) : $page->the_post();
the_content();
endwhile;
?>