duancan1900
duancan1900
2011-06-27 06:02
浏览 87
已采纳

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.

图片转代码服务由CSDN问答提供 功能建议

在我的Wordpress网站中,我试图检索最近的10个帖子并将它们存储在一个字符串中。 之后我会将这些内容写入文本文件。 以下是我正在使用的代码。

 &lt;?php $ str ='';  ?&GT; 
&LT; PHP中
require_once(” ../ WP-博客-的header.php '); 
query_posts(' &安培; showposts = 10安培;为了= DESC&安培; caller_get_posts = 1' );  
while(have_posts()):the_post();  ?&GT;  
&lt;?php $ str。='&lt; a href =“'。the_permalink()。'”&gt;'  。标题() 。  '&LT; / A&GT;';  ?&gt; 
&lt;?php endwhile;  ?&gt; 
&lt;?php $ fp = fopen(“latestposts.txt”,“w”); 
 fwrite($ fp,$ str); 
 nclclose($ fp);?&gt; 
   
 
 

问题是,当我执行此页面时,永久链接和标题将在此页面中返回,并且空的''....''标签将出现在文本文件中。 如果我没有使用该字符串,则href标记在同一文件中正确返回。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtx9931
    dtx9931 2011-06-27 06:05
    已采纳

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

    点赞 评论
  • dongzou3751
    dongzou3751 2011-06-27 06:05

    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.

    点赞 评论

相关推荐