So, for a post loop, $post->ID
is used to get the post id's.
Is there a way to "change" it to a custom "id"?
Here is what I mean by it:
So, I have a page that is passed onto a template via ajax as following:
There is a first php template called "first.php
".
In this file, I have a post loop which contains a button:
$id = get_the_ID();
<?php echo '<button type="button" class="rh_show_image" data-post_id="' .$id. '">' ;?>
<?php echo get_post_meta($post->ID, 'rh_image', true); ?>
<?php echo '</button>';?>
This button has data-post_id
which contains the post id. When it is clicked, then a second.php
is loaded via ajax:
<script>
jQuery(document).ready(function() {
jQuery('.rh_show_image').click(function(e) {
e.preventDefault();
var rh_contact_form_id = jQuery(this).data("post_id");
jQuery.ajax({
type: "GET",
url: "<?php echo admin_url('admin-ajax.php'); ?>",
dataType: 'html',
data: ({ action: 'rh_second_php', rhc_post_id: rh_contact_form_id}),
success: function(data){
jQuery('#rh_iamge_' + rh_contact_form_id).html(data);
},
error: function(data)
{
alert("Error!");
return false;
}
});
});
});
</script>
When the second.php
is called, as you can see from the script above, the post_id
of the post is saved and parsed onto the template (data: ({ action: 'rh_second_php', rhc_post_id: rh_contact_form_id}),
)
Then in the second.php
, I have the following:
<!--Show title-->
<?php echo get_the_title($_REQUEST['rhc_post_id']); ?>
<!--Show image-->
<div class="images">
<?php
$I_will_call_you = callme_image();
foreach ($I_will_call_you as $slideshowimage)
{
echo "<img src='{$slideshowimage}' />";
}
?>
</div>
So, essentially, I am using $_REQUEST['rhc_post_id']
instead of $post->ID
Here is the problem
In the image
div from the second.php, it does not show any images because it does not recognized the post id
from $_REQUEST['rhc_post_id']
.
So, is there a way to make it equal
between $post->ID
and $_REQUEST['rhc_post_id']
?
Something like this: $_REQUEST['rhc_post_id']
= $post->ID
Thanks guys!
EDIT: The image
div works fine when it is placed with a post loop, instead via ajax. So, the code itself works fine.
Edit 2: Syntax in the second.php is fixed now