2012-02-16 95 views
1

我想顯示標籤「醫療」的帖子,如果他們是第843頁的孩子,到目前爲止,我有一切主要工作。我需要在這個if語句中獲得一些HTML,以便我可以將它全部放在div中,並且還可以爲列表設置打開和關閉ul。WordPress的帖子查詢

有人能幫我克服這最後一個駝峯嗎?我只是不確定在哪裏添加HTML。

<?php 
if (843 == $post->post_parent) { 
global $post; 
$myposts = get_posts('numberposts=10&tag=medical&order=DESC'); 
foreach($myposts as $post) : 
?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endforeach; 
} 
?> 

回答

2

如果你想有一個包圍在div中所有的職位:

<?php 
global $post; 
if (843 == $post->post_parent) { 
    $myposts = get_posts('numberposts=10&tag=medical&order=DESC'); 
    echo '<div><ul>'; 
    foreach ($myposts as $post): ?> 
     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
    <?php endforeach; 
    echo '</ul></div>'; 
} ?> 
+0

真棒!謝謝,大衛。是否有可能得到if語句中的最後一個div?最好能夠迴應它嗎? – user1214812 2012-02-16 21:10:14

+0

@Alex是正確的。我認爲你需要if之前的'global $ post。是的,你只需在foreach循環周圍回顯div和ul標籤,但在if塊內部。 – 2012-02-16 21:16:34

1

可你只是做類似下面的?

<div id="wrap"> 
<ul> 
<?php 
if (843 == $post->post_parent) { 
global $post; 
$myposts = get_posts('numberposts=10&tag=medical&order=DESC'); 
foreach($myposts as $post) : 
?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endforeach; 
} 
?> 
</ul> 
</div> <!-- #wrap !--> 
+0

我應該指定我需要在if語句中包含div和UL,因爲如果頁面不符合標準,我不希望出現任何內容。 – user1214812 2012-02-16 21:11:17

1

首先,if語句之前所說的全球$變量後,並重新寫的條件是這樣的:

global $post 
if ($post->post_parent = 843) 

此外,您不能使用的功能the_permalink()和the_title( )外循環,你必須使用,取而代之的是,功能get_the_permalink(ID)和get_the_title(ID)這種方式(也,$崗位是模糊的,你應該使用另一個變量名):

<div><ul> 
<?php foreach($myposts as $mypost) : ?> 
<li><a href="<?php echo get_the_permalink($mypost->ID); ?>"><?php echo get_the_title($mypost->ID); ?></a></li> 
<?php endforeach; 
}?> 
</ul></div>