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
Related
I have been working on creating a custom loop and setting how many posts per page. I needed to make this loop separate to the blog posts per page - hence the custom loop.
Now the loop does work, however I noticed it is conflicting with the pagniation... pagination is showing but pages are not changing when you click 'next'.
Here is my loop:
<?php
$args = array('posts_per_page'=>12, 'post_type' =>'office');
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="office-item-wrapper">
<div class="office-item">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
</article>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article><!--/ Article -->
<?php endif; ?>
Thanks in advance to anyone who might be able to help me solve this issue :-)
In WP, the pagination is sent by a GET variable under page, just add it to the query args:
$page = get_query_var('paged');
$args = array('posts_per_page'=>12, 'post_type' =>'office', 'paged'=>$page);
I recommend you to use get_query_var('paged') because if it's empty it will be send you to the first page.
Note: In some themes, the page number is sent in the page key, so try with page or paged.
My Wordpress site has a loop that creates posts, and I want to target specific posts to change their css values.
html - index.php
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
content.php
<?php
if (has_post_thumbnail()) {
$thumbnail_data = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'my-fun-size' );
$thumbnail_url = $thumbnail_data[0];
}
?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>')" <?php post_class('container-fluid'); ?> >
<div class="container-fluid">
<div class="col-md-12 text-cell">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
It was suggested that I use 'get_post_meta ...' but I am not familiar with how to use it. I just want to change css values (padding, font-size, etc) for different posts
I would suggest using Custom Fields for this, so that you can define the values in the post itself, as opposed to having to edit the code every time you add a new post.
In the post, make sure you have Custom Fields visible (Togglable from screen options at the top)
Then make a field called "Alignment" or whatever you prefer and assign a value to it. (for example 'left')
Then you can add a conditional in the loop.
<?php $alignment = get_post_meta(get_the_ID(),'Alignment',true);
if($alignment) == 'left'):?>
<p>Do stuff and things here...</p>
<?php endif;?>
You can read more about it here: https://codex.wordpress.org/Custom_Fields
Hopefully that will work for you. If you want to get fancier with it, I would suggest looking at the Advanced Custom Fields plugin, which allows a lot more flexibility and options.
EDIT from comments:
1st option:
Set a field of "ExtraCSS" to "color:green;"
<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>'); <?php echo $extraCSS;?>" <?php post_class('container-fluid'); ?> >
2nd option:
(In your stylesheet:)
article:nth-child(2n+0)
{
color:green;
}
http://www.w3schools.com/cssref/sel_nth-child.asp
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'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'); ?>