douyi6168 2013-12-11 10:39
浏览 64
已采纳

WordPress blog_content字符限制

Great people of Stack, I once again require your assistance.

I currently have an external website that pulls through blog content from a sub-folder containing a WordPress installation, for example:

Website A: External Static Website Website B: WordPress installation

I have the posts including on the homepage of Website A using the following code.

WordPress call:

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

The Blog Post inclusion:

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

This works perfectly, it pulls the necessary post and displays it, all wonderfully formatted etc. The problem I have is that I really need to limit the character count that is pulled through, as it stands, this currently echo's the entire post, I only need to echo the first 15 characters of the post. Any ideas would be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • duanpendan8067 2013-12-11 10:46
    关注

    You can limit the character output by echoing the following.

    From:

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

    To:

     <p style="martin-top: -10px; margin-right: 10px;"><?php echo substr($blog_content,0,40); ?></p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测