2016-09-27 34 views
0

我已經做了一些研究,並沒有找到任何關於它。post_type如何獲得1標題

我有4個不同的職位。

$args = array(
    'post_type'=> 'post', 
    'order' => 'ASC');    
$the_query = new WP_Query($args); 
if($the_query->have_posts()) : while $the_query->have_posts()) : $the_query->the_post(); 
the_title();    
endwhile; 
endif; 
wp_reset_postdata(); 

這段代碼顯示了所有titels,輸出是這樣的:

1post2post3post4post 
1post2post3post4post 
1post2post3post4post 
1post2post3post4post 

,但我想是這樣的:

1post 
2post 
3post 
4post 

我如何獲得呢?

編輯:@Vincent的幫助下做了一些改變。我如何獲得這兩部分代碼來相互協作?

<?php 
$args = array(
'post_type'=> 'cubeportfolio', 
'order' => 'ASC' 
);    

$the_query = new WP_Query($args); 
if($the_query->have_posts()) : 
while ($the_query->have_posts()) : $the_query->the_post(); 
?> 
<?php  
the_permalink(); 
endwhile; 
endif; 
wp_reset_postdata();        
?> 

<?php 
$args = array(
'post_type'=> 'post', 
'order' => 'ASC' 
);    

$the_query = new WP_Query($args); 
if($the_query->have_posts()) : 
while ($the_query->have_posts()) : $the_query->the_post(); 
?> 
<?php  
echo get_the_title()."<br/>"; 
endwhile; 
endif; 
wp_reset_postdata();        
?>  
+0

'the_title()。 '
';'是多種方式之一 – RiggsFolly

+0

@RiggsFolly謝謝你,是的,我做到了,但就像我說的,我仍然有4個職位,我只想要1.我添加了我的洞代碼,也許你明白了現在我正在嘗試做的事 – di477

回答

0

編輯:你只有這部分得到了什麼?

  <?php 
       $args = array(
        'post_type'=> 'post', 
        'order' => 'ASC' 
        );    

       $the_query = new WP_Query($args); 
       if($the_query->have_posts()) : 
        while ($the_query->have_posts()) : $the_query->the_post();  
         echo '<a href="'.get_the_permalink().'" >'.get_the_title().'</a>'.'<br/>'; 
        endwhile; 
       endif; 
       wp_reset_postdata();        
      ?>              
+0

謝謝您的幫助,但是,輸出現在想: 1post 2post 3post 4post 1post 2post 3post 4post 1post 2post 3post 4post 1post 2post 3post 4post 的仍然得到四行一排我想要 – di477

+0

嘗試評論foreach部分:'<?php foreach($ posts as $ post):setup_postdata($ post)?>我認爲你會得到這個結果,因爲除了foreach之外,你還用while循環進行迭代。 –

+0

我沒有評論foreach部分,但奇怪的是輸出沒有變化。即使沒有錯誤 – di477

0

後編輯 - 試試這個代碼

<?php 
$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 10, 
    'order' => 'ASC' 
); 
$the_query = new WP_Query($args); 
if ($the_query->have_posts()) : 
    ?> 
    <ul> 
     <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
      <li> 
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
      </li> 
      <?php 
     endwhile; 
     wp_reset_postdata 
     ?> 
    </ul> 
<?php endif; ?> 
<?php wp_reset_postdata(); ?> 
+0

感謝abdo我已經這樣做了,但我想'post_type'=>'cubeportfolio'的永久鏈接和'post_type'=>'post'的標題 那就是爲什麼它對我來說如此之難 – di477

+0

我認爲這個查詢不是正確的,我會再次嘗試搜索 –

+0

嗨,abdo,我想知道你是否找到了某種東西 – di477