admin管理员组文章数量:1389762
I have a question.
I have 2 post. post A and post B. Post B have field ID post A. I want create a function to retrieve data from post B that have the post A ID. I don't know how to do this.
function show_post_data(){
global $post;
$cid = $post->ID; //I create this because I want to show the Post B data in Single Post A
$my_posts = get_posts( array('post_type'=>'post_b',)); //retrive Post B data
up until here, I don't know how to retrieve data from post B or to display it. someone help me
I have a question.
I have 2 post. post A and post B. Post B have field ID post A. I want create a function to retrieve data from post B that have the post A ID. I don't know how to do this.
function show_post_data(){
global $post;
$cid = $post->ID; //I create this because I want to show the Post B data in Single Post A
$my_posts = get_posts( array('post_type'=>'post_b',)); //retrive Post B data
up until here, I don't know how to retrieve data from post B or to display it. someone help me
Share Improve this question edited Mar 29, 2020 at 16:42 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Mar 29, 2020 at 15:47 user3239674user3239674 134 bronze badges 3 |1 Answer
Reset to default 0Here is a simple test drive for your question.
// suppose you paste it into a template, eg. page.php or single.php
// I leave out any test or checking, to show to concept
function display_other_post() {
global $post;
$current_post_id = $post->ID;
$other_post_id = 14;
$other_post = get_post( $other_post_id );
print_r( $other_post ); // do whatever you need
}
display_other_post();
In this example, if you run it, no matter what post are you in, you will always display post with ID=14. So I have mentioned that this is a basic method without dynamic capability unless you can get the post ID from your GUI such as adding options in backend, meta value. But it depends on your design and structure first. So, the first thing is what kind of effect you want to accomplish and how you want to input.
本文标签: Retrieve value between 2 post
版权声明:本文标题:Retrieve value between 2 post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744622425a2616107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_post()
(instead of get_posts). So that you can specific the ID of post_b. eg$post_b = get_post( post_b_id)
But this way, it is not dynamic because you need to hard code the ID. So it is a basic method. It depends on how you want to develop and design your structure. For details, you may read get_post() – 西門 正 Code Guy - JingCodeGuy Commented Mar 29, 2020 at 16:01