2

スタックの偉大な人々、私はもう一度あなたの助けを必要としています.

現在、WordPress インストールを含むサブフォルダーからブログ コンテンツを取得する外部 Web サイトがあります。たとえば、次のようになります。

Web サイト A: 外部の静的 Web サイト Web サイト B: WordPress のインストール

次のコードを使用して、Web サイト 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 に答える