I've edited the single.php to suit my needs and it works. I only left in the part of the loop in in which is as follows:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content('<p class="serif">Read the rest of this entry »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> </div>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
It only displayes the text, like I want it to.
The problem I get is when I add the following code to be used as the sidebar in the template;
<?php query_posts('showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>">
<?php the_title(); ?></a><br />
<?php endwhile;?>
It should display the title of the last 10 posts. But now the loop also is displaying the latest (full0 10 posts instead of just the one post that belongs to the permalink... I think a variable or so is being reused and needs to be rest.. Note that in the single.php first you get the 'sidebar' code, and then you get the 'loop' code.
So why is wordpress behaving this way?
The reason this happens is because Wordpress is a nightmarish maze of global variables. query_posts() is one of the worst offenders. If you check the documentation for this function, you'll see that they even have to warn you to basically not use it:
Important note
The query_posts function is intended
to be used to modify the main page
Loop only. It is not intended as a
means to create secondary Loops on the
page. If you want to create separate
Loops outside of the main one, you
should create separate WP_Query
objects and use those instead. Use of
query_posts on Loops other than the
main one can result in your main Loop
becoming incorrect and possibly
displaying things that you were not
expecting.
They've added some object oriented stuff that you can use now instead, namely the WP_Query object (why they haven't revamped the "main" pages to get rid of the ridiculous "The Loop" stuff yet is questionable).
You're going to want to do something like this in the sidebar:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=10');
while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark"
title="Link to <?php the_title(); ?>">
<?php the_title(); ?></a><br />
<?php endwhile;?>
Google around about how to use WP_Query if you want more examples.
query('showposts=10');
while ($recentPosts->have_posts()) :
$recentPosts->the_post(); ?>
"
rel="bookmark" title="Link to ">
reading the code u putting in the sidebar, u are trying to get the last 10 titles of posts to show in sidebar , right ? if so u can just use this line :
`<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>
Related
I'm using a basic wordpress loop to show 3 blog posts + pagination, using query_posts to loop these - code attached. The loop and pagination work well, but it's showing a strange glitch: the full page content (for all 3 posts) is also looped and displayed. The full content appears after the pagination and after the endwhile closing tag, at the end of all code.
My suspicion is that the loop is somehow not closed, but any closing tags I know to check are already present. My php is still early beginner level and mostly focused on standard wordpress functions, so any suggestions how I can close this up would be welcome.
Note: the loop is using query_posts instead of new WP_Query because of the pagination, this did not work using any new WP_Query loops. I want to get rid of the additional content but keep the base loop the same so the pagination is still functional.
Final note: I have verified the extra content is caused by this loop as removing the loop also removed the content.
<article class="row">
<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'post', 'paged' => get_query_var('page')));
if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<span class="inline-button">Read more <i class="fas fa-chevron-right"></i></span>
</a>
<?php endwhile; ?>
<?php endif;?>
<?php wp_reset_postdata(); ?>
</article>
<article class="row">
<?php the_posts_pagination(); ?>
</article>
<!------ in html, all extra generated content will appear here below the above article --->
Resolved!
I found the answer and simply closed it adding this to the end:
<?php wp_reset_query() ;?>
I am trying to have two different queries in my Loop, which is located in index.php. I am working with WP codex, but it isnt working. I want to have every post in its own special DIV later, so this is just start of my work.
The problem is, that the second part of code doesnt work, and I have no idea why. As far as I have read the codex, everything should be fine. Please help me.
<div class="col1">
<?php
$my_query = new WP_Query('category_A tym=featured&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<!-- Do stuff... -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<!--Over here everything works fine!-->
<!--This code doesnt show up. It is supossed to show 1 post, only heading and date with author. But it doesnt show nothing at all.-->
<?php
$my_queryOne = new WP_Query('posts_per_page=1');
while ($my_queryOne->have_posts()) : $my_queryOne->the_post();
if ($post->ID == $do_not_duplicate)
continue;
?>
<!-- Do stuff... -->
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<?php
endwhile;
?>
</div>
WP_Query takes an array as a parameter - i.e.
$query = new WP_Query( array( 'category_name' => 'featured','posts_per_page' => 1 ));
Also, when running multiple queries - use wp_reset_postdata() after the first loop.
Great examples here:
https://codex.wordpress.org/Class_Reference/WP_Query
Its probably best to review the coding guidelines that WordPress has laid out. You need to ensure that every open and close tag for inline PHP is on its own line.
Check here for reference:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#opening-and-closing-php-tags
I'm trying to pull post types into my Bones based theme, and I'd like those posts to live inside of divs. eg.
<div class="fourcol"></div>
The problem is, some of the divs will also need a class of "first" or "last".
Right now, I'm using JQuery to find all of the divs with the class "fourcol" and adding a "first" class to the first div in the row, and a "last" class to the last div in the row. It seems to work, but there's a little bit of shifting when the page loads, and it doesn't seem as safe as I'd like it to be. It also causes issues when trying to filter the post types, as the first and last div are constantly changing, and the jQuery isn't reloading to reflect the posts new position.
Is there better way to pull these posts into divs?
I've tried everything I can to come up with minor fixes, but I'm sure it's come up before so I figured I'd post it here. Let me know if you have any suggestions!
Here's my loop so far:
<?php query_posts( array( 'cat' => 10, 'post_type' => 'people', 'showposts'=>-1 ) ); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="threecol">
<article id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix' ); ?> role="article">
<header class="article-header">
<center><h3><?php the_title(); ?></h3></center>
</header> <?php // end article header ?>
<section class="entry-content clearfix">
<a class="ctp-hover" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<span class="hover-caption">VIEW</span>
<?php the_post_thumbnail('full'); ?>
</a>
</section> <?php // end article section ?>
</article> <?php // end article ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I have three pages on my site. Let's call them home, page2, and page3.
My 'home' page is set as a static front page. My 'page2' is set up as the blog page.
What I want is the following:
I want page2 to display blog posts with a certain category (of which ID is known).
AND
I want page3 to display blog posts with a certain category (of which ID is known).
The PHP code to only show posts with a certain category (or actually in my case, show posts excluding two categories) is the following:
<?php query_posts($query_string . '&cat=-3,-8'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h3>
<?php the_excerpt('Read the rest of this entry »'); ?>
</div><!-- /.post-->
Now, in my page.php, I have the following code to display posts with one category:
<?php
// BEGIN IF PAGE is newspaper articles page
if ( is_page('newspaper') ) {
//BEGIN POST REGION
query_posts($query_string . '&cat=8'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h3><?php the_title(); ?></h3>
<?php the_content('Read more »'); ?>
</div><!-- /.post-->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php
} //end if is_page
?>
But it doesn't show the proper posts on the newspaper page (or page3 in this question). It does, however, work for the articles page (main index.php blog page).
EDIT: I've also tried the following (but it doesn't work). I put this in the index.php file:
<?php
if ( is_page('newspaper') || is_home() ) { // START if is home
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to
<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h3>
<!--<p><?php the_time('F jS, Y') ?> <?php //the_author() ?></p>-->
<?php the_excerpt('Read the rest of this entry »'); ?>
</div><!-- /.post-->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php
} //end if is_home() or is_page()
?>
Again, this shows the posts on the main blog page, but doesn't show any posts on the newspaper page...
The question is therefore simple (I think). How do I show posts on another page OTHER than the main blog page?
Thanks!
Amit
Rather than exclude categories and exclude pages and change the standard Wordpress loop, use a new query, like this:
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a></h3>
<?php the_excerpt('Read the rest of this entry »'); ?>
<?php endwhile; ?>
This can be used inside the standard WP loop and be used mutiple times in a page/post or page template without conflicting. (Enable php execution to use it in the page/post editor). Function Reference/WP Query « WordPress Codex
This also works well to use page templates to create different pages with blog posts: Page Templates « WordPress Codex, but don't forget that WP also uses category pages, too, depending on your theme: Category Templates « WordPress Codex.
I think you have to make different templates for different page. Check this link http://codex.wordpress.org/Pages
I think this thread answers the question and does what you want.
http://wordpress.org/support/topic/show-only-x-category-posts-on-page?replies=9#post-1053767
Using the string 'newspaper' in is_page('newspaper') is a potential source of the problem. It might be misspelled easily.
Have you ever tried using the page id? Something like
is_page('999')
I want to show last 5 posts in a wordpress page, but when i use this code it will return the whole posts body, while i have <!--more--> in my post and I want to show till this part.
here is the code that I am using :
<div>
<ul>
<? query_posts('showposts=5'); ?>
<?php while (have_posts()): the_post(); ?>
<li>
<?php the_contenet(); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
Change it from the_content to the_excerpt then it shows a teaser instead of full content...
I can't tell what you want; do you want a list of the last five posts, or to print the content of the last five posts?
Some things: you've mispelled the_content. And you have an unneeded closing tag: </a> And if you want a list of posts, the_content isn't what you want, anyway: you want the_title.
And, if you're using this inside the main wordpress Loop, it needs to be a new query so it doesn't conflict with The Loop:
<?php $my_query = new WP_Query('showposts=5'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_content(); ?><?php endwhile; ?>