现在,我正在使用get_posts
要检索指定了自定义分类法的cusstom帖子类型,以便生成如下静态内容:
<?php
/**
* Template Name: Front Page
* @package WordPress
* @subpackage Prominent
* @since Prominent 1.0
*/
get_header(); ?>
<div class="shadow-top">
<!-- Shadow at the top of the slider -->
</div>
<div id="intro">
<div class="container">
<div id="slider-wrapper">
<div id="slider">
<?php // Create custom loop ?>
<?php $custom_posts = get_posts(\'post_type=page_content&page_sections=Slider (Front Page)\'); ?>
<?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</div>
<div class="shadow-slider">
<!-- Shadow at the bottom of the slider -->
</div>
</div><!-- #slider-wrapper -->
</div><!-- .container -->
</div><!-- #featured -->
<div class="shadow-bottom">
<!-- Shadow at the bottom of the slider -->
</div>
<div id="tagline">
<div class="container">
<?php
$page_id = $post->ID; // 123 should be replaced with a specific Page\'s id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), Wordpress will generate an error.
$content = apply_filters(\'the_content\', $page_data->post_content); // Get Content and retain Wordpress filters such as paragraph tags. Origin from: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
$title = $page_data->post_title; // Get title
?>
<div class="content0">
<h2><?php echo $content; // Output Content ?></h2>
</div>
</div><!-- .container -->
</div><!-- #content-bottom -->
<div id="content">
<div class="container">
<div class="mainbar">
<?php // Create custom loop
$custom_posts = get_posts(\'post_type=page_content&page_sections=Content (Front Page)\'); ?>
<?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
<div class="content-block">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div><!-- .content-block -->
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</div><!-- #mainbar -->
</div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>
在这种情况下,什么是更好的做法?
要使用query\\u posts、WP\\u query或get\\u posts?
最合适的回答,由SO网友:MikeSchinkel 整理而成
你好@janoChen:
如果你有选择,go with WP_Query
. 其他两个功能(query_posts()
和get_posts()
) 呼叫WP_Query
间接地。
前者允许您在标准查询已经运行之后修改主查询,例如,当您需要第二个循环时。但是query_posts()
影响全局变量并可能产生副作用。如果可能的话,使用WP\\u查询,您的代码将更加健壮。
至于get_posts()
, 这只是个包装WP_Query
具有一些潜在的意外默认值,因此您不妨调用WP_Query
直接和避免这些问题。