admin管理员组文章数量:1288016
I want to display on single post three things:
- List of all posts in general
- Single post title
- Content
What I achieved right now is not satisfying because I am displaying all posts (and it works ok) but the second part (connected with showing only specific post title and content) don't work because it shows only the newest one.
For example:
I have 2 posts called "News about IT" and "TV series". Post about TV is the newest one. After clicking on News about IT i will see all posts in the top and as a main part - title and content of NEWS ABOUT IT. Right now using my solution it shows TV series as a newest post..
How can I achieve my solution?
This is my current code:
<div class="wrapper">
<div class="center">
<div class="sub-menu-1">
<a href="#" title="O nas" class="about-us"> O nas </a>
<a href="#" title="Oferta" class="offer"> Oferta</a>
<div class="clear"></div>
</div>
<div class="news-slider">
<a href="#" title="Nowsze wpisy" class="arrow-left" id="mycarousel-prev"></a>
<div class="news-content">
<ul id="mycarousel" class="jcarousel-skin-tango">
<?php rewind_posts(); ?>
<?php query_posts('post_type=post&posts_per_page=-1'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li>
<p class="post-date"><?php the_time('j F Y') ?></p>
<p class="separator"></p>
<p class="post-title"><a href="<?php the_permalink(); ?>"
title="<?php printf(esc_attr__('Odsyłacz do %s', 'sp3-rybnik'), the_title_attribute('echo=0')); ?>"
rel="bookmark"><?php the_title(); ?></a></p>
</li>
<?php endwhile;
else: ?>
<?php wp_reset_query(); ?>
<h4>404 - brak strony...</h4>
<p>Wielka otchłań internetu połknęła wskazaną stronę - wyszukaj interesującą Cię treść
korzystając z
menu.</p>
<?php endif; ?>
</ul>
</div>
<a href="#" title="Starsze wpisy" class="arrow-right" id="mycarousel-next"></a>
<div class="clear"></div>
</div>
</div>
</div>
<div class="content-single">
<div class="content-single-entry">
<?php rewind_posts(); ?>
<?php query_posts('post_type=post&posts_per_page=1'); ?>
<?php if(have_posts()) : while(have_posts()) :
the_post(); ?>
<h2><?php the_title(); ?></h2>
<p class="separator"></p>
<p class="post-date"><?php the_time('j F Y') ?></p>
<div id="scrollbar1">
<div class="scrollbar">
<div class="track">
<div class="thumb">
<div class="end"></div>
</div>
</div>
</div>
<div class="viewport">
<div class="overview">
<?php the_content(); ?>
<?php endwhile;
else: ?>
<?php wp_reset_query(); ?>
<h4>404 -brak strony...</h4>
<p>Wielka otchłań internetu połknęła wskazaną stronę - wyszukaj interesującą Cię treść
korzystając z menu.</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
I want to display on single post three things:
- List of all posts in general
- Single post title
- Content
What I achieved right now is not satisfying because I am displaying all posts (and it works ok) but the second part (connected with showing only specific post title and content) don't work because it shows only the newest one.
For example:
I have 2 posts called "News about IT" and "TV series". Post about TV is the newest one. After clicking on News about IT i will see all posts in the top and as a main part - title and content of NEWS ABOUT IT. Right now using my solution it shows TV series as a newest post..
How can I achieve my solution?
This is my current code:
<div class="wrapper">
<div class="center">
<div class="sub-menu-1">
<a href="#" title="O nas" class="about-us"> O nas </a>
<a href="#" title="Oferta" class="offer"> Oferta</a>
<div class="clear"></div>
</div>
<div class="news-slider">
<a href="#" title="Nowsze wpisy" class="arrow-left" id="mycarousel-prev"></a>
<div class="news-content">
<ul id="mycarousel" class="jcarousel-skin-tango">
<?php rewind_posts(); ?>
<?php query_posts('post_type=post&posts_per_page=-1'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li>
<p class="post-date"><?php the_time('j F Y') ?></p>
<p class="separator"></p>
<p class="post-title"><a href="<?php the_permalink(); ?>"
title="<?php printf(esc_attr__('Odsyłacz do %s', 'sp3-rybnik'), the_title_attribute('echo=0')); ?>"
rel="bookmark"><?php the_title(); ?></a></p>
</li>
<?php endwhile;
else: ?>
<?php wp_reset_query(); ?>
<h4>404 - brak strony...</h4>
<p>Wielka otchłań internetu połknęła wskazaną stronę - wyszukaj interesującą Cię treść
korzystając z
menu.</p>
<?php endif; ?>
</ul>
</div>
<a href="#" title="Starsze wpisy" class="arrow-right" id="mycarousel-next"></a>
<div class="clear"></div>
</div>
</div>
</div>
<div class="content-single">
<div class="content-single-entry">
<?php rewind_posts(); ?>
<?php query_posts('post_type=post&posts_per_page=1'); ?>
<?php if(have_posts()) : while(have_posts()) :
the_post(); ?>
<h2><?php the_title(); ?></h2>
<p class="separator"></p>
<p class="post-date"><?php the_time('j F Y') ?></p>
<div id="scrollbar1">
<div class="scrollbar">
<div class="track">
<div class="thumb">
<div class="end"></div>
</div>
</div>
</div>
<div class="viewport">
<div class="overview">
<?php the_content(); ?>
<?php endwhile;
else: ?>
<?php wp_reset_query(); ?>
<h4>404 -brak strony...</h4>
<p>Wielka otchłań internetu połknęła wskazaną stronę - wyszukaj interesującą Cię treść
korzystając z menu.</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
Share
Improve this question
edited Jan 9, 2016 at 23:13
jgraup
9,8843 gold badges32 silver badges69 bronze badges
asked Feb 17, 2013 at 10:54
Paweł SkabaPaweł Skaba
1502 silver badges12 bronze badges
1
- 2 Please include your code in the question itself. If that pastebin links breaks the question become meaningless. – s_ha_dum Commented Feb 17, 2013 at 15:58
1 Answer
Reset to default 1When the single post template is loaded, WordPress has already queried the database for that post. When you call query_posts('post_type=post&posts_per_page=1');
, and then run the loop, this simply loads and outputs a single post, which by default will be the latest post. It also has the side-effect of overwriting the original main query.
You don't need to, nor should you ever call query_posts
in the template.
To load additional posts beside the single post you are currently viewing, use WP_Query
, and leave the main query unaltered.
// load and display all posts
// setting post_type is unnecessary, it will default to post
$all_posts = new WP_Query( array( 'posts_per_page' => -1 ) );
while( $all_posts->have_posts() ):
$all_posts->the_post();
// your markup for all posts
endwhile;
// reset the global post variable
wp_reset_postdata();
// now output the single post
// no need for a query, the post data already exists
// in the global $wp_query
while( have_posts() ):
the_post();
// your markup for the single post
endwhile;
本文标签: wp querySingle postdisplay list of all postsspecific post
版权声明:本文标题:wp query - Single post - display list of all posts + specific post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741334495a2372961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论