I have a function in my wordpress theme:
function ajax_get_videoposts () {
$args = array(
'numberposts' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
//get 5 posts which are published
$recent_posts = wp_get_recent_posts($args);
$append .= '<div id="recommended-videos" class="recommended-videos rec-vid rec-vid2">';
$append .= '<div class="rec-title">Check These Out Next:</div>';
$append .= '<div class="rec-videos">';
foreach ($recent_posts as $recent) {
$thumb_id = get_post_thumbnail_id($recent['ID']);
$url = wp_get_attachment_url($thumb_id);
$append .= '<div class="videos">';
//a hrefs
$append .= '<a href="' . get_permalink($recent["ID"]) . '">';
$append .= '<div class="videoinfo">' . (__($recent["post_title"])) . '</div>' . '</a>';
//img tag
$append .= '<img align="top"' . 'src="' . $url . '"' . 'alt="' . (__($recent["post_title"])) . '">';
//looping 6 times
$append .= '</div>';
}
$append .= '<div class="rec-btns">';
$append .= print_r(get_permalink());
echo $append;
}
add_action( 'wp_ajax_ajax_get_videoposts', 'ajax_get_videoposts' ); // If called from admin panel
add_action( 'wp_ajax_nopriv_ajax_get_videoposts', 'ajax_get_videoposts' ); // If called from front end
Then I call this function i call this function via AJAX. The problem is $append .= print_r(get_permalink());
Printing me the number '1' instead of the post URL because it is used in the functions.php instead of for example single.php how would i get the post URL inside functions.php?
Why using print_r ?
Isn't
$append = get_permalink();
what you really want?
Just in case, if you want to store print_r inside a variable, then you must pass TRUE as a second parameter. That will give us :
$append = print_r(get_permalink(),TRUE);
I wouldn't expect get_permalink()
to return anything useful, given that you're calling this function via Ajax, and from what can be seen of your code a loop isn't in play. It's not clear from looking at this code which page or post you were hoping get_permalink()
would be referring to; perhaps sharing your reason for doing this will help in providing a better answer.
Also, the '1' output you see is because of how print_r
works:
When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.
Your $append .= print_r(get_permalink());
effectively resolves to $append .= TRUE;
(which in being string-concatenated will be cast to the string representation '1'
).