Showing Wordpress posts on two pages - php

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

Related

How to include comments under posts in a clean build template?

I am new to wordpress theme development and currently building building my first one from scratch, I have hit an issue with adding comments with blog posts.
I have the loop the that returns each post:
<div class="eachPost">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>"><?php
the_title();?></a></h2>
<p class="entry-meta">by <?php the_author_meta('first_name'); ?>
<?php the_author_meta('last_name'); ?> in <?php the_category(", ")
?></p>
<p class='right'><a class='comments-count' href='<?php the_permalink() ?>'><?php comments_number('0', '1', '%')
?></a></p>
<?php comments_template(); ?>
</article>
<h4>Posted on <?php the_time('F jS, Y') ?></h4>
<p><?php the_content(__('(more...)')); ?></p>
<hr> <?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?>
</div>
I believe that I need to call the function:
<?php comments_template(); ?>
but adding this to the code has no effect.
I have comments.php and comment-template.php in the same folder as the file and believe that I need to include these in the page as follows
<?php get_template_part('comments.php'); ?>
<?php get_template_part('comments_template.php'); ?>
Using in config
<?php define('WP_DEBUG', true);?>
<?php define('WP_DEBUG_LOG', true);?>
<?php define('WP_DEBUG_DISPLAY', true);?>
Create no error in logs or info.
In a clean build template how do you include comments under posts?
The comments.php needs to be inside the Template root folder, please take a look at the Wordpress Codex http://codex.wordpress.org/Function_Reference/comments_template
If you want to have it somewhere else, here's the Wordpress recommendation:
The path to the file used for an alternative comments template should
be relative to the current theme root directory, and include any
subfolders. So if the custom comments template is in a folder inside
the theme, it may look like this when called:
<?php comments_template( '/custom-templates/alternative-comments.php' ); ?>
You see no errors because there's no mistake there. The system is calling a function that should load a file that if it doesn't exist, no error is thrown.

featured post same as post content

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.

Switching between posts in wordpress

I have created a sidebar.php-file with the code;
<section class="sidomeny"><p class="Senastenytt2"><?php get_search_form();?></p></section><!--Slut på sidomeny-->
<div id="nyhet3">
<?php
query_posts('category = all');
if (have_posts()) :
while (have_posts()) : ?>
<?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>
<?php
endwhile;
endif;
?>
</div><!--Slut på Nyhet3-->
And now I would like to add the function that if you press the sidebar news section, the "textnyhet2" section will get updated with the news you clicked on. That is how it's working now.
But the problem is that the sidebar vanished whenever you click on one of those "latest news" links. So what I need help with is to edit the sidebar.php-file so the "latest news" function with links is still there so you don't have to go back one page whenever you would like to read about another post.
Im not sure how to do this. If it's php involved or if you can do it by simple visiting the posts/pages section in wordpress.
Would be grateful if anyone could help me. If I have forgotten any necassary information please tell me and I will the post.
Thanks!
<?php
get_header();
?>
<section class="textinnehall">
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;?>
</section>
<section class="sidomeny">
<?php
get_sidebar(); ?>
</section>
<?php
get_footer();
?>
This is my page.php file. Don't know if it will be to any help. But the sidebar is already active.
May be the sidebar is not called in your single page.
you need to have the same design in your themes single.php page(As per twenty ten theme standard).
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
The href <?php the_permalink() ?> takes the post to single page for full view
Just call the sidebar in your theme's single page.

How to strip embedded video from content on category pages in WordPress?

I have a couple of posts that have YouTube videos embedded in them. I only want the videos to show up on the full version of the post. On the category page that lists the posts, I don't want the video to appear.
Here is the site: http://tsa-watch.com/
and the full version of the post:
http://tsa-watch.com/2013/03/25/tsa-makes-double-amputee-marine-remove-prosthetic-legs-during-screening/
Here is a bit of code I use in functions.php to remove the first image from a post ONLY if it is listed on a category page:
function remove_first_image ($content) {
if (is_category()) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
$content = preg_replace("/<object[^>]+>/i", "", $content, 1);
} return $content;
}
add_filter('the_content', 'remove_first_image');
Also, here is the loop that I am using on my index.php and category.php files:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="votes" style="min-width:60px"><strong>VOTES:</strong><?php wdpv_vote(false); ?></div>
<div class="alignleft" style="max-width:590px">
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<div class="byline"><?php the_time('F jS, Y') ?> by <?php the_author() ?> in <?php the_category(', ') ?> <strong>:</strong> <?php comments_number( 'No Comments', '1 Comment', '% Comments' ); ?></div>
<div class="excerpt">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(array(115,115));
}
?>
<?php the_content('Read more »'); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<h1>Not Found</h1>
<p><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
Any help would be appreciated...
This is a WordPress site I believe?
If so you need to look at editing the theme template file that is responsible for showing the preview of your post on the category pages and remove the section where it is showing images/videos from the post.
Unfortunately not knowing what theme it is you are using or what the file names are called it's hard to tell but look for a page template which has the word home or categories in it or open up the category.php from within your activated theme folder if you have them to see if you can find something related to this and comment it out to see if it fixes it.
Also try adding the following inside your custom function:
$content = preg_replace("/<embed[^>]+>/i", "", $content, 1);

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