Gnanadeep
Gnanadeep

Reputation: 45

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.

Upvotes: 3

Views: 7820

Answers (2)

Shakti Singh
Shakti Singh

Reputation: 86436

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>'; ?>

Upvotes: 4

Dan Smith
Dan Smith

Reputation: 5685

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.

Upvotes: 1

Related Questions