Wordpress - outputting relevent posts in sidebar.php on selected posts - php

This seems like something that should be really easy, I want to output the latest three events when on an event post and the latest three news items when on a news post, I've got this working in a very basic sense however I can work out how to do the following:
When on a post in events show the latest three events, when on a post in news show the latest three news posts. I've looked on here http://codex.wordpress.org/Conditional_Tags and thought that is_category('news') and is_category('events') would do the trick however these conditionals seem to only be available on category archive pages.
I currently have the following code which simple checks you're on a single post page (I'm only using posts for news and events, everything else is a page):
<?php
if ( is_single() ) {
query_posts('category_name=news&order=DSC&offset=1&orderby=ID&posts_per_page=2');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<h3><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h3>
<span class="date"><?php the_time(get_option('date_format')); ?></span>
<?php the_excerpt($strip_teaser); ?>
<a class="more" href='<?php the_permalink() ?>'>Read more</a>
</article>
<?php endwhile; endif; wp_reset_query();?>
<?php
}
else {
}
?>

Conditional comments should work, you were just one letter off (in vs. is)
You want in_category(), not is_category()
http://codex.wordpress.org/Template_Tags/in_category

Related

Get post from a single category with have_posts () WordPress

I am showing the last post of a blog on an external website that I am developing out of wordpress.
<?php
//Include WordPress
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
//Define quantos posts serão exibidos
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<li>
<h4><?php the_title(); ?></h4>
<span><?php the_time("d/m/Y"); ?></span>
<?php the_category_ID(); ?>
<?php the_content(); ?>
<div>
« Leia Mais...
</div>
</li>
<?php endwhile;?>
The point is that the blog is separated into two categories and I would like to display only one of them.
Link where I got those functions.
http://codex.wordpress.org/Template_Tags
When you query posts, you can add the conditions for categories as well, like this:
query_posts('showposts=3&cat=CAT_ID'); //replace CAT_ID with the category ID that you want
Since you mentioned that you wanted to show the latest posts, you might need to do this:
query_posts('showposts=3&cat=CAT_ID&orderby=date&order=DESC');

Wordpress - Posts and Pages

I am currently developing a mutlisite wordpress setup, each site is a different language (EG: site.com, site.dk etc).
The sites consist of a number of pages which contain static content, however I also want to include posts (a blog) into both of the sites.
Q1. Is it possible to create a page that shows all the posts listed by latest publish date, with a dropdown that filters by category? How do I do this? Do I need to refer to loop.php?
Basically it should return the following code for all post articles...
<article class="post">
<a href="<URL Link to Post Article>" rel="bookmark">
<figure>
<img title="<Post Title>" alt="<Post Title>" src="<http://url/PostImage.jpg>" width="900" height="600" />
</figure>
<div class="cover">
<h2>Post Title</h2>
<time pubdate="2013-03-27T21:09:59+00:00">November 18, 2012</time>
</div>
</a>
</article>
Why doesn't this work? It doesn't return anything?
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="post">
<?php get_posts(); ?>
<div id="grid-switcher">
featured
latest
</div>
<div id="view-blocks">
<div id="latest-post" class="post-grid active">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(array(250,250)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
<!-- /post thumbnail -->
<div class="cover">
<h2><?php the_title(); ?></h2>
<time pubdate="<?php the_date(); ?>"><?php the_date('Y-m-d', '<h2>', '</h2>'); ?></time>
</div>
</a>
I have already created a page template portfolio-page.php for the above but cannot findout how to loop through the posts and return them with the above code?
Q2. How do I return a dropdown list with all the categories?
Q3. How do I filter by category from the dropdown list?
Thanks for any help! :)
Sorry I am new to PHP and wordpress...
The question is a bit vague so I will just share some starting points:
A1) To get a list of posts you can use the get_posts function from wordpress. It already sorts them by the post date desc and you can also add a parameter for the category if one is picked.
A2) To get a dropdown of categories there's a function that does exactly that: wp_dropdown_categories
Check the docs as there are a lot of parameters that can be used with the get_posts function so you should be able to filter them as you wish.
I think its a good idea for you to check the database you have wordpress running on.
if you check the wp_posts table you can see the differend fields that you can filter on.
You will be better off using custom query's to filter te posts and order them

Wordpress If post title already on page remove from list

This is a bit of a complex one I have been working on. I have four main categories. Formal, Fasion, Social and Wedding. I have Wordpress set up so it shows the twelve most recent posts of all these in one section.
<li>
<?php $recent = new WP_Query("category_name=social,fashion,wedding,formal&showposts=1&offset=1"); while($recent->have_posts()) : $recent->the_post();?>
<div class="show">
<a class="thumb" href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink() ?>">
<section class="phototitle"><p><?php the_title(); ?></p></section>
</a>
</div>
<?php endwhile; ?>
</li>
This works fine and I use the offset function to show them over and over.
I then have the same four categories listed below this in separated boxes. So they have a bit of order.
<section class="postlist">
<h1>Formal</h1>
<hr>
<?php $recent = new WP_Query("category_name=formal&showposts=6"); while($recent->have_posts()) : $recent->the_post();?>
<?php the_title(); ?><br />
<?php endwhile; ?>
</section>
My question is how do I stop repeat posts from the top section so the postlist section will have different link titles.
I thought it might be something to do with the exclude technique and if the title already appears on the page remove it. Or if title is on page and if child of top section remove or something along those lines.
The problem is the top section is random so I can just offset it by 12.
Any input appreciated.
Cheers
You can collect the post id's you already displayed and then make use of the WP_Query exclude feature for the second query. Already displayed posts will be excluded then.

Showing Wordpress posts on two pages

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')

wordpress sidebar and loop php code distorts each other

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'); ?>

Categories