2015-10-06 66 views
0

我有一個WordPress網站,我想顯示當前類別(當前文章除外)中的所有帖子的列表。如何顯示自定義字段,而不是the_title

我需要使用名爲「test」而不是帖子標題的特定custom-field的值作爲錨文本。

如何編輯下面的代碼以完成該操作並排除當前帖子?

<?php 
     global $post; 
     $categories = get_the_category(); 
     foreach ($categories as $category) :?> 
      <ul> 
      <?php 
       $posts = get_posts('numberposts=3&category='. $category->term_id); 
       foreach($posts as $post) : ?> 
       <li> 
        <a href="<?php the_permalink(); ?>"> 
<?php the_title(); ?> 
    </a>   </li> 
      <?php endforeach; ?> 
    <?php endforeach; ?> 
      </ul> 

回答

1

這可能會實現:

<?php 

global $post; 
$categories = get_the_category(); 
$currentID = get_the_ID(); 
foreach ($categories as $category) :?> 
    <ul> 
     <?php 
     $posts = get_posts('numberposts=3&category='. $category->term_id); 
      foreach($posts as $post) : 
      if ($currentID != $post->ID) { 
       ?>     
       $test_value = get_post_meta($post->ID, 'test', true); 
       ?> 
       <li> 
        <a href="<?php the_permalink(); ?>"> 
         <?php if (! empty($test_value)) {echo $test_value; } else { echo 'Nothing'; } ?> 
        </a> 
       </li><?php 
       } ?> 
      <?php endforeach; ?> 
    </ul> 
<?php endforeach; ?> 
+0

'是編輯的主要部分 – GingerGirl

+0

對不起,忽略了那部分。更新。 – danjah

+0

該代碼有PHP語法錯誤,並且它不會加載:( – GingerGirl