Limiting feed to only certain categories - php

I'm using the following code to pull articles up for display on my homepage, i'd like to limit it to only two or three categories - can anyone point me in the right direction?
<?php
$i = 1;
$my_categories = get_option('of_news_page');
$wp_query = new WP_Query("cat=' . $my_categories . '&posts_per_page=14");
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<?php $image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];?>
<?php if($i==1) { ?>
<div class="featured_single">
<div class="featured_single_image">
<?php if($image_url) { ?><a class="image_article" href="<?php the_permalink(); ?>"><img src="<?php echo bloginfo('template_directory'); ?>/js/timthumb.php?src=<?php echo $image_url; ?>&h=170&w=255&zc=1" alt="" /></a><?php } ?>
<div class="clear"></div>
<span>Posted in : <?php the_category(', '); ?></span>
<span><?php comments_popup_link('No comments yet', '1 Comment »', '% Comments »'); ?></span>
</div>
<div class="featured_single_text">
<span><?php the_time('M j, Y') ?></span>
<h3><?php the_title(); ?></h3>
<?php wpe_excerpt('wpe_featured_single'); ?>
<p>Read More »</p>
</div>
</div>
<div class="clear"></div>
<div id="featured-posts-news">
<?php } elseif($i>1 && $i<6) { ?>
<div class="featured-post-news-container clearfix">
<?php if($image_url) { ?><img src="<?php echo bloginfo('template_directory'); ?>/js/timthumb.php?src=<?php echo $image_url; ?>&h=120&w=209&zc=1" alt="" /><?php } ?>
<h2><?php the_title(); ?></h2>
<span><?php the_time('M j, Y') ?></span>
<?php wpe_excerpt('lotf_news_page'); ?><span class="news-morelink">[ Read More → ]</span>
</div>
<?php } ?><?php $i++; ?><?php endwhile; $i=0; ?>
</div>

You can use the query_posts() function for ease, here is an example of how to pull posts from category_ids 1,3 and 5, in ascending order and 5 posts per page:
query_posts('cat=1,3,5&order=ASC&posts_per_page=5');
You can find out more here: http://codex.wordpress.org/Function_Reference/query_posts
Note, the WP_Query object you are using works in much the same way as query posts, in terms of arguments that is.

Related

Error accessing my widgets.php in wordpress [duplicate]

I have been trying to show the posts of my website in a grid view in one of the category page. For doing so, i implemented the following code:
<div class="row-fluid">
<div class="span6 featured-block">
<?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
<a class="featured_image" href="<?php the_permalink();?> "target="_blank">
<?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
if(!#getimagesize($image_url)){
$image_url = "/wp-content/uploads/2014/02/logo2.jpg";
}
$blogurl = get_bloginfo('url');
$image_url = str_replace($blogurl, '', $image_url);
?>
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo ($image_url); ?>&w=390&h=216&zc=1&q=100" alt="<?php echo the_title(); ?>" />
<?php }
else { ?>
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo "http://www.nepaljapan.com/wp-content/uploads/2014/02/logo2.jpg"; ?>&w=390&h=216&zc=1&q=100" alt="<?php echo the_title(); ?>" />
<?php }?>
</a>
<a href="<?php the_permalink();?>" target="_blank">
<h2><?php the_title(); ?></h2></a>
<p><?php echo excerpt(37); ?>
<?php endwhile; ?>
</div>
</div>
But i am facing the problem with this code.
This gives out the error as
Fatal error: Call to a member function have_posts() on a non-object in /home/npjp/public_html/wp-content/themes/nepaljapan/content-category.php on line 15
The line 15 on my code is
<?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
What is the problem? Am i actually doing it correct??
Much confused.
Please help me.
Thank you in advance
Your custom query has some problems. You can use this guid to write custom queries in wordpress:
<?php
$block1Featured= new WP_Query( 'category_name=staff+news' );
if ( $block1Featured->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $block1Featured->have_posts() ) : $block1Featured->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Wordpress Posts Page - loop is not showing latest post

I am relatively new to php.
I have a loop in place for my Wordpress posts page - The posts have to alternate between left and right alignments.
I have this working by assigning an even or odd class to each post, however now the latest post does not display on the posts page.
For example, if I have say 5 posts; 4 of the posts will display and the latest post will remain hidden until I make a new post - the previously hidden post will then join the others and the new "latest post" will remain hidden.
I can't figure out why my loop is skipping over the first post, I have already tried adding rewind_posts(); however this created an infinite loop of the same post.
Any help is much appreciated!
<?php
$postcount=1;
while(have_posts()) :
if( ($postcount % 2) == 0 ) $post_class = ' even';
else $post_class = ' odd';
?>
<div class="row">
<div id="stories-box-alt" class="stories-column-circle-main"
style="background-color:transparent;">
<div id="circle-shape" class="post <?php echo $post_class; ?>">
<?php the_post(); ?>
<img src="<?php the_field('post_preview_image'); ?>" class="curve">
<h2><?php the_title(); ?></h2>
<h3><span class="featured-title"><?php the_field('post_category'); ?> .
</span></h3>
<p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink();
?>">read more...</a></p>
</div>
</div>
</div>
<?php $postcount++;
endwhile; ?>
<?php
$postcount=1;
while(have_posts()) :
?>
<div class="row">
<div id="stories-box-alt" class="stories-column-circle-main"
style="background-color:transparent;">
<div id="circle-shape" class="post <?php if(($postcount % 2) == 0){ ?> even <?php } else{ echo " odd"; }?>">
<?php the_post(); ?>
<img src="<?php the_field('post_preview_image'); ?>" class="curve">
<h2><?php the_title(); ?></h2>
<h3><span class="featured-title"><?php the_field('post_category'); ?> .
</span></h3>
<p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink();
?>">read more...</a></p>
</div>
</div>
</div>
<?php $postcount++;
endwhile; ?>
OR
<?php echo $postcount % 2 == 0 ? ' even ': ' odd '; ?>
Please try to use the_post() first.
<?php
$postcount=1;
while(have_posts()) :
the_post();
if( ($postcount % 2) == 0 ) $post_class = ' even';
else $post_class = ' odd';
?>
<div class="row">
<div id="stories-box-alt" class="stories-column-circle-main"
style="background-color:transparent;">
<div id="circle-shape" class="post <?php echo $post_class; ?>">
<img src="<?php the_field('post_preview_image'); ?>" class="curve">
<h2><?php the_title(); ?></h2>
<h3><span class="featured-title"><?php the_field('post_category'); ?> .
</span></h3>
<p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink();
?>">read more...</a></p>
</div>
</div>
</div>
<?php $postcount++;
endwhile; ?>
Basically there is a basic loop in wordpress to make what you want to do : https://wpchannel.com/wordpress/tutoriels-wordpress/afficher-articles-recents-site-wordpress/
You can modify this one with your own properties but this is usually loop used.

Print category of post Wordpress

I have try all the functions related to print categories that the codex provide, but i havent found any way that works for me.
Im trying to print the categories slug for put it inside a class.
I want to print the category of the actual post in div with the class proyect, so then i can use it to filter with isotope.
<!-- feature posts -->
<div id="container">
<?php $the_query = new WP_Query('showposts=5&orderby=post_date&order=DESC'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();
$id = get_the_ID(); ?>
<div class="proyect <?php wp_get_post_categories($id); ?>">
<div class="view view-tenth">
<a style="display:block;" href="<?php the_permalink(); ?>">
<article> <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail('', array("class" => "")); } ?></article>
</a>
<div class="mask">
<h2><?php echo substr(strip_tags(get_the_title()),0,35); ?></h2></a>
<p class="fecha-post"><?php the_time('F j, Y'); ?></p>
<?php echo substr(strip_tags(get_the_content()),0,100); ?>
<a class="info" href="<?php the_permalink(); ?>">Ver más...</a>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<!-- #feature post -->
From http://wordpress.org/support/topic/getting-category-slug-from-posts-in-the-loop:
<li class="<?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
You should be able to use:
$cats = wp_get_post_categories($post->ID);
This will be an array of the categories associated with this post. Then you can loop through them and do whatever you need.

How do I remove the most recent post from the sidebar in Wordpress?

On both the front page and the blog page - the sidebar shows the most recent post, which I find doesn't look very good duplicated against the same post expanded on the main page.
This is my code for the sidebar:
<div class="blog-sidebar">
<?php query_posts('showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="blog-sidebar-feature">
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_title();
?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
</div>
<?php endwhile;?>
<br />
<?php wp_pagenavi(); ?>
</div>
and the relevant code for how the blog appears on the home page:
<div class="blog-sidebar">
<div class="blog-sidebar-feature">
<?php query_posts('orderby=date&order=DESC&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>"
rel="bookmark" title=""><?php the_title(); ?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
<?php endwhile;?>
</div>
</div>
<div id="connect">
<?php query_posts('page_id=1');
while (have_posts()): the_post();
the_content();
endwhile;
wp_reset_query(); ?>
</div>
Is there any way to remove only the most recent post from the sidebar when it appears in full on the main container? Thanks in advance for any help.
UPDATE V2
So you do want recent posts, just not the post currently showing in the main content.
UPDATE V3:
This should work now. I had to change arguments of query_posts to array to make it work.
Try it now:
<?
global $wp_query;
$skip_posts=array();
if (is_single()) //only exclude posts when single post is shown
$skip_posts[]=$wp_query->post->ID;
?>
<?php query_posts( array( 'showposts'=>5,'post__not_in'=>$skip_posts)); ?>
<?php query_posts('posts_per_page=5&offset=1'); ?>
Thanks to 850010 for all the help, I went back and had a look at the offset rule and the 'array' wasn't needed. Deceptively simple.

How to view three different category of post in three different columns in wordpress?

I am really new in 'WordPress' and current facing a problem that is I am using FUNDA theme and try to show the three different category of post in three different column depending on category ID but can not find the post for a specific category.
I try this
<?php
$data = cats_to_select();
$cat_id = $data[1][value];
global $cat_id;?>
<?php print_r($cat_id);?>
<?php if($cat_id==3):?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(260,200), array("class" => "alignleft post_thumbnail")); } ?>
<h2 class="title"><?php the_title(); ?></h2>
<div class="postdate"><img src="<?php bloginfo('template_url'); ?>/images/date.png" /> <?php the_time('F jS, Y') ?> <img src="<?php bloginfo('template_url'); ?>/images/user.png" /> <?php the_author() ?> <?php if (current_user_can('edit_post', $post->ID)) { ?> <img src="<?php bloginfo('template_url'); ?>/images/edit.png" /> <?php edit_post_link('Edit', '', ''); } ?></div>
<div class="entry">
<?php the_content('<strong>Read more »</strong>'); ?>
</div>
</div><!--/post-<?php the_ID(); ?>-->
<?php endwhile; ?>
<div class="navigation">
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } else { ?>
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
<?php } ?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
<?php endif; ?>
Can any one help me? Thanks in advance.
<?php query_posts('cat=3'); ?> // here, 3 will be replaced with desired
// category id, or you may add array
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(260,200), array("class" => "alignleft post_thumbnail")); } ?>
<h2 class="title"><?php the_title(); ?></h2>
<div class="postdate"><img src="<?php bloginfo('template_url'); ?>/images/date.png" /> <?php the_time('F jS, Y') ?> <img src="<?php bloginfo('template_url'); ?>/images/user.png" /> <?php the_author() ?> <?php if (current_user_can('edit_post', $post->ID)) { ?> <img src="<?php bloginfo('template_url'); ?>/images/edit.png" /> <?php edit_post_link('Edit', '', ''); } ?></div>
<div class="entry">
<?php the_content('<strong>Read more »</strong>'); ?>
</div>
</div><!--/post-<?php the_ID(); ?>-->
<?php endwhile; ?>
That code only shows one category. You would need to add an array of the category ids and possible use a for loop to multiply the code, instead of the if statement. Maybe something like that.
Then make a bit of css that would divide the content into different boxes? I'm not sure what you mean by three columns. Also not sure what the FUNDA theme is.
It would be easier if you could give us a better idea of what you're using. Link?

Categories