2016-09-27 71 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