返回介绍

setup_postdata()

发布于 2017-09-11 10:21:36 字数 3836 浏览 1266 评论 0 收藏 0

setup_postdata( WP_Post|object|int $post )

Set up global post data.


description


参数

$post

(WP_Post|object|int) (Required) WP_Post instance or Post ID/object.


返回值

(bool) True when finished.


源代码

File: wp-includes/query.php

function setup_postdata( $post ) {
	global $wp_query;

	if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
		return $wp_query->setup_postdata( $post );
	}

	return false;
}

更新日志

Versiondescription
4.4.0Added the ability to pass a post ID to $post.
1.5.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-query.php: WP_Query::setup_postdata()

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::prepare_item_for_response()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_response()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_embed()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_media_shortcode()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::single_row()
  • wp-includes/deprecated.php: start_wp()
  • Show 2 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by mslade

    An important note about setup_postdata and the $post global: setup_postdata( $new_post ) sets various globals 相关函数 to the current post but it does not update the $post global. This disjoint can cause problems both in WP internals and in plugins/themes.

    Therefore if you call setup_postdata( $new_post ), you should also assign it to the global $post object.

  2. Example 1

    
    <ul>
    	<?php
    	global $post;
    	
    	$myposts = get_posts( array(
    		'posts_per_page' => 5,
    		'offset'         => 1,
    		'category'       => 1
    	) );
    	
    	if ( $myposts ) :
    		foreach ( $myposts as $post ) :
    		  setup_postdata( $post ); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		endforeach; 
    		wp_reset_postdata();
    	endif;
    	?>
    </ul>
    

    Example 2

    
    <ul>
    <?php
    global $wpdb, $post;
    
    $str    = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'";
    $result = $wpdb->get_results( $str );
    
    if ( $result ) {
    	foreach ( $result as $post ):
    		setup_postdata( $post );
    		?>
    		<li><a href="<?php the_permalink()?>"><?php the_title();?></a></li>
    		<?php 
    	endforeach;
    }
    ?>
    </ul>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文