2

Stack 的伟大人物,我再次需要您的帮助。

我目前有一个外部网站,它从包含 WordPress 安装的子文件夹中提取博客内容,例如:

网站 A:外部静态网站 网站 B:WordPress 安装

我使用以下代码在网站 A 的主页上发布了帖子。

WordPress 调用:

<?php  
//db parameters  
$db_username = '###';  
$db_password = '###';  
$db_database = '###';  

$blog_url = 'http://www.website.com/blog/';

//connect to the database  
mysql_connect('###', $db_username, $db_password);  
@mysql_select_db($db_database) or die("Unable to select database");  

//get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.  
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 1";   

$query_result = mysql_query($query);  
$num_rows = mysql_numrows($query_result);  

//close database connection  
mysql_close();  

// html page starts after 
?>  

博客文章包含:

<div class="contentBox">
    <?php  
    for($i=0; $i< $num_rows; $i++){   

   //assign data to variables, $i is the row number, which increases with each run of the loop  
   $blog_date = mysql_result($query_result, $i, "post_date");  
   $blog_title = mysql_result($query_result, $i, "post_title");  
   $blog_content = mysql_result($query_result, $i, "post_content");  
   //$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.  

   $blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format  

                        //format date  
                        $blog_date = strtotime($blog_date);  
                        $blog_date = strftime("%b %e", $blog_date);  

                        //the following HTML content will be generated on the page as many times as the loop runs. In this case 5.  
                        ?>
                        <div class="post"></div>  
                        <img src="img/headers/news-from.png" /><br />

                                <p class="blogName"><a href="http://www.website.com/blog"><?php echo $blog_title; ?></a></p>  

                                <p style="margin-top: -10px; margin-right: 10px;"><?php echo $blog_content;?></p>  

                                <p>Submitted on: <span class="green"><?php echo $blog_date; ?></span></p>   

                                <p><a href=”&lt;?php echo $blog_permalink; ?>”&gt;Read Full Post</a></p>  
                        <?php  
                            } //end the for loop  
                        ?>
                    </div>

这完美地工作,它拉出必要的帖子并显示它,所有格式都非常精美等。我遇到的问题是我真的需要限制通过的字符数,就目前而言,这个当前回声是整个帖子,我只需要回显帖子的前 15 个字符。任何想法将不胜感激。

4

3 回答 3

2

您可以通过回显以下内容来限制字符输出。

从:

 <p style="margin-top: -10px; margin-right: 10px;"><?php echo $blog_content;?></p>

至:

 <p style="martin-top: -10px; margin-right: 10px;"><?php echo substr($blog_content,0,40); ?></p>
于 2013-12-11T10:46:12.187 回答
2

这似乎是一个复杂问题的简单解决方案,但为什么不使用 asubstr来修剪帖子呢?

你甚至可以通过设置一个单独的变量来做到这一点?

例如

$trimmed_post = substr($blog_content, 0,15);

这一切所做的就是创建一个新变量以显示在页面上,该变量从$blog_content变量的字符 0 开始,并在 15 个字符后停止。

于 2013-12-11T10:48:56.763 回答
1

一种方法是删除该行

echo $blog_content;

并将其替换为

echo substr(strip_tags($blog_content), 0, 50);

将 50 更改为您想要的长度。

于 2013-12-11T10:48:39.580 回答