簡體   English   中英

WordPress blog_content字符限制

[英]WordPress blog_content character limit

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=”<?php echo $blog_permalink; ?>”>Read Full Post</a></p>  
                        <?php  
                            } //end the for loop  
                        ?>
                    </div>

這完美地工作了,它拉出了必要的帖子並顯示了它,並且格式都很好,等等。我遇到的問題是,我真的需要限制所拉過的字符數,就目前而言,這是整個帖子的回聲,我只需要回顯帖子的前15個字符。 任何想法將不勝感激。

您可以通過回顯以下內容來限制字符輸出。

從:

 <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>

這似乎是一個簡單的解決方案,但是為什么不使用substr來簡化帖子呢?

您甚至可以通過設置單獨的變量來做到這一點?

例如

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

完成的所有操作就是創建一個新變量以在頁面上顯示,該變量從$blog_content變量的字符0開始,並在15個字符后中斷。

一種方法是刪除行

echo $blog_content;

並替換為

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

將50更改為所需的長度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM