2010-07-14 68 views
0

我需要在靜態HTML頁面上添加WordPress帖子的鏈接。我得到了一些有用的信息,但需要更多的元素才能完成。在靜態HTML頁面中顯示WordPress帖子 - 添加更多詳細信息

這裏是我到目前爲止的代碼:

<?php 

$number = 5; 
$wordpress_header = "blog/wp-blog-header.php"; 

     // Include wordpress header 
     if (file_exists($wordpress_header)) 
     { 
     include ($wordpress_header); 

     $myposts = get_posts('numberposts=$number&offset=0&category=0'); 

     echo "<ul class='Bloglinks'>"; 

     foreach(array_slice($myposts, 0, $number) as $post) 
     { 
      echo '<li><a href="'; 
      the_permalink(); 
      echo '">'; 
      the_date(); 
      echo " "; 
      the_title(); 
      echo '</a></li>'; 
     } 

     echo "</ul>"; 

     } 
     else 
     { 
     echo "Unable to connect to Wordpress header file."; 
     die(); 
     } 

?> 

這隻能說明中最近發表的標題。我需要能夠顯示以下內容:

<h5>The truth about Lazy Eye</h5> 
<p class="blog-info">07.16.10 | <a class="comment-ref">3 Comments</a></p> 
<h5>More Adult Learning Problems Linked to Eyes</h5> 
<p class="blog-info">06.12.10 | <a class="comment-ref">1 Comments</a></p> 
<h5>New Vision Examination Instruments Arrived!</h5> 
<p class="blog-info">05.10.10 | <a class="comment-ref">13 Comments</a></p> 

回答

1

你應該使用功能query_posts()與功能have_posts()和the_post()。這裏是一個WordPress API的例子:

//The Query 
query_posts('posts_per_page=5'); 

//The Loop 
if (have_posts()) : while (have_posts()) : the_post(); 
.. 
endwhile; else: 
.. 
endif; 

//Reset Query 
wp_reset_query(); 

這將遍歷所有您查詢過的帖子。所以你可以將你的查詢從get_posts()函數插入到query_posts()函數中。

編輯:我認爲,如果你想堅持的get_posts()函數,你必須調用setup_postdata()函數來獲得新的職位(source code for the API):

<ul> 
    <?php 
     global $post; 
     $myposts = get_posts('numberposts=5&offset=1&category=1'); 
     foreach($myposts as $post) : 
      setup_postdata($post); 
    ?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
    <?php endforeach; ?> 
</ul> 

但我會建議採取query_posts()函數。

0
<?php 
     require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); 
     $args = array(
     // 'cat' => 3, // Only source posts from a specific category 
     'posts_per_page' => 6 // Specify how many posts you'd like to display 
     ); 
     $latest_posts = new WP_Query($args); 
     if ($latest_posts->have_posts()) { 
     while ($latest_posts->have_posts()) { 
     $latest_posts->the_post(); ?> 

            <article class="vertical-item content-padding ls"> 
             <div class="item-media"> 
          <img src="<?php the_post_thumbnail() ?>" alt="<?php the_title(); ?>"> 
             </div> 
             <div class="item-content"> 
              <h4 class="entry-title"> 
               <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
              </h4> 
              <div> 

               <div class="media-body media-middle greylinks"> 

                <br><a class="small-text" href="#"><?php the_time('l jS F, Y') ?></a> 
               </div> 
              </div> 
             </div> 
             <div class="item-footer"> 

              <a class="lato lightgrey weight-black" href="<?php the_permalink(); ?>">Read this Article</a> 
             </div> 
            </article> 
            <? } 
     } else { 
     echo '<p>There are no posts available</p>'; 
     } 
     wp_reset_postdata(); 
     ?> 
+0

請在代碼中添加一些上下文 – 2017-09-11 10:29:54

相關問題