Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation in Wordpress

In my Wordpress site, I am trying to retrieve the most recent 10 posts and store them in a string. After that I will write this content into a text file. Below is the code I am using.

<?php $str  = ''; ?>
<?php
require_once('../wp-blog-header.php');
query_posts('&showposts=10&order=DESC&caller_get_posts=1');  
while (have_posts()) : the_post(); ?>     
 <?php $str .= '<a href="' . the_permalink() . '">' .the_title() . '</a>'; ?>
<?php endwhile; ?>
<?php $fp = fopen("latestposts.txt", "w");
 fwrite($fp, $str);
fclose($fp);?>

The problem is, when I execute this page, the permalink and title are returning in this page and empty ''....'' tags are coming in text file. If I am not using the string, the href tags are returning correctly in the same file.

like image 810
Gnanadeep Avatar asked Sep 14 '26 01:09

Gnanadeep


2 Answers

the_permalink() and the_title() does not return anything they are to print values.

You have to use their get_ version. Those are get_permalink() and get_the_title()

<?php $str .= '<a href="' . get_permalink() . '">' .get_the_title() . '</a>'; ?>

like image 66
Shakti Singh Avatar answered Sep 16 '26 13:09

Shakti Singh


This is more of a wordpress question, but you should be using get_permalink() and get_the_title() instead of the functions you have there. Those functions will echo the link and title, and not return it in string form for use in your concatenation.

like image 28
Dan Smith Avatar answered Sep 16 '26 13:09

Dan Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!