I have a wordpress site I am creating.
on the post pages, I have text widget with PHP allowed, where I have a custom loop:
<?php $my_query2 = new WP_Query("showposts=1&cat=9,10,11,18,19&orderby=rand"); ?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>">
<div class="home-widget-thumb">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('home-thumb');
}
?>
</div>
<h2><?php the_title(); ?></h2></a>
<div class="body">
<?php echo get_the_excerpt(); ?>
</div><!--body-->
</br>
<span class="more-link">
[more]
</span>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
For some reason, whatever blog post you are on is the same as in the loop I created.
See an example here:
http://counselingandtraining.com/play-therapy/
The loop for the post is not modified.
Can anyone tell me why this is happening?
Let me know if I can provide further info.
Thanks in advance for your time.
Chris
It looks to me like the problem is that you are still using the base query.
You can change this by adding your variable $my_query2 in the code like this:
<?php if ($my_query2->have_posts()) : ?><?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
This will make all the functions like the_title(), the_content(), etc work as intended since they will be set to $my_query2.
Related
So im trying to add a function called "latest posts" in a sidebar section.
This is how my code looks like in sidebar.php-file.
<?php
query_posts('category = all');
if (have_posts()) :
while (have_posts()) : ?>
<?php the_post();
the_excerpt(); ?>
<p class="datum2"><?php the_time('Y-m-d'); ?></p>
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
<p class="textnyhet2">
</p>
<?php endwhile;
endif;
?>
My problem is, no matter how much I move around in the code I just cant seems to get the correct title/date to appear above the correct post.
Now it's like the post comes first, then right under the post the date and title appears above another incorrect post etc.
Example:
Latest post text
space---->
title/date, (though to the post above)
latest post text
space----->
and continue like that.
Would be grateful if anyone could help me out.
Thanks!
The reason the title/date appears below the post is because that's the way you have specified it to appear. Change
<?php the_post();
the_excerpt(); ?>
<p class="datum2"><?php the_time('Y-m-d'); ?></p>
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
<p class="textnyhet2">
</p>
to
<?php the_post(); ?>
<p class="datum2"><?php the_time('Y-m-d'); ?></p>
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
<?php the_excerpt(); ?>
<p class="textnyhet2">
</p>
It should display the way you want it.
the_excerpt(); is printing out an excerpt of the post, then the time/title displays. Now it should display the time and title first, then display the excerpt of the post.
Good afternoon, I'm trying to display posts using PHP in the template, but I'm using the same template to output different posts depending on the age ID.
I currently have this code which works...
<?php //GET MEMBERS ?>
<?php query_posts('category_name=members&orderby=date'); ?>
<div class="row-fluid">
<ul class="thumbnails">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="span4">
<div class="thumbnail">
<?php // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
?>
<div class="pad">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
But I need to output a different "category" depending on page ID... E.G
if page id is 7 echo "php script"
else page id is 13 echo "different php script"
Thanks, Brad
Wordpress has some built in functions. You can use get_the_ID() to return the ID number to use in your if statements.
Regards
I'm trying to make a custom search page, by creating all new search.php file, for my wordpress template...so far, so good.
The problem is that when I search for something it doesn't show any results.
I'm guesing that it has something to do with some php script or something I don't know.
How can I fix that ?
P.S The function for the number of the results works fine, but there isn't any results.
Here is the content of search.php
<?php
get_header();
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<?php endwhile; ?>
<?php else : ?>
<?php _e( 'Nothing Found' ); ?>
<?php endif; ?>
<?php
get_footer();
?>
The problem is that you don't have anything in your loop to print the results, i.e.
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<!-- Needs something here -->
<?php endwhile; ?>
To fix the problem, simple replace <!-- Needs something here --> with the following
<a href="<?php the_permalink() ?>">
<h2><?php the_title(); ?></h2>
</a>
<p><?php the_excerpt(); ?></p>
You also need to move <h1>Search Results</h1> to above the loop to stop it from displaying multiple times. It may be best to move it above the if statement if you don't intend on adding it to your else statement as well.
I'm having trouble getting this to work. Can someone provide a quick snippet for category template that displays posts that belong to a category called 'Product A'. I've been using the trial and error method for the past 3 hours with no luck.
Thank you!
Here's what I've been playing around with -
<?php
/*
Template Name: yadayada
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php query_posts('cat=32&showposts=5'); ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
You can used the WP_Query class.
One way I've done it before is by first creating a category name of Product-A and making the slug 'product-a' all lower case.
Then instantiate a new instance of the class. Pass in the parameter of 'category_name=product-a' You do no pass in the category name with this parameter, but rather the slug name. once you do that you should be able to use the WP_Query as follows:
<?php $my_query = new WP_Query( 'category_name=product-a' ); ?>
<?php if ($my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts()) : $my_query->the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="product-excerpt"><?php the_content(); ?> </div>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
pretty much everything is the same as the regular loop but instead of just
<?php if(have_post()) : while(have_post()) : the_post() ?>
You would used object notation to refer to this particular query.
<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?>
hope it helps.
First get your Product A category id; (if you use, your cat id in your custom query it 's gonna work perfectly instead of category name.)
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
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')