如何从WP_Query循环中获取帖子ID?
-
-
$post_id=get_the_ID();可以在循环中使用.这将检索循环处理的当前帖子的ID.`$post_id = get_the_ID();` can be used within the loop. This retrieves the ID of current post handled by the loop.
- 4
- 2016-01-16
- N00b
-
@ N00b,您应该将其发布为答案.@N00b you should post that as an answer.
- 0
- 2016-01-16
- Pieter Goosen
-
您是要获取类别,还是要创建一个称为"类别"的自定义帖子类型?如果是前者,则应该使用[`get_categories()`](https://developer.wordpress.org/reference/functions/get_categories/);如果是后者,则应阅读以下内容:https://codex.wordpress.org/Reserved_TermsAre you trying to get categories, or have you a custom post type called "category"? If the former then you should be using [`get_categories()`](https://developer.wordpress.org/reference/functions/get_categories/) if the latter then you should read this: https://codex.wordpress.org/Reserved_Terms
- 0
- 2018-08-31
- Peter HvD
-
2 个回答
- 投票数
-
- 2016-01-16
只能在循环中使用get_the_ID()
(仅).这将检索循环处理的当前帖子的
ID
.
如果只需要一次,则可以单独使用它:
$dish_meta = get_post_meta( get_the_ID(), 'dish_meta', true );
如果需要多次,也可以将其存储为变量:
$post_id = get_the_ID(); $dish_meta = get_post_meta( $post_id, 'dish_meta', true ); $drink_meta = get_post_meta( $post_id, 'drink_meta', true ); print_r( $post_id ); //etc
参考:get_the_ID()
get_the_ID()
can (only) be used within the loop.This retrieves the
ID
of the current post handled by the loop.
You can use it on it's own if you need it only once:
$dish_meta = get_post_meta( get_the_ID(), 'dish_meta', true );
You can also store it as a variable if you need it more than once:
$post_id = get_the_ID(); $dish_meta = get_post_meta( $post_id, 'dish_meta', true ); $drink_meta = get_post_meta( $post_id, 'drink_meta', true ); print_r( $post_id ); //etc
Reference: get_the_ID()
-
- 2018-08-31
get_the_ID()函数将为您提供帖子ID ..
$args = array( 's' => $_POST['search_text'], 'posts_per_page' => -1, 'post_type' => 'address' ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $address_post_id = get_the_ID() ; } }
get_the_ID() function will give you post ID..,
$args = array( 's' => $_POST['search_text'], 'posts_per_page' => -1, 'post_type' => 'address' ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $address_post_id = get_the_ID() ; } }
我有一个WP_Query循环,该循环获取某种类型的帖子.这些帖子具有自定义的帖子元,因此我需要能够获取帖子的ID而不回显它,因此我可以显示该帖子的元.如何在不回显信息的情况下获取帖子的ID?这是我的代码: