How Can I only show a specific category in my wordpress index page? Below is my theme code...
Also, when looking at the URL for my category (how I usually get the ID) it seems a strange format..
http://oil.wpengine.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=1179&post_type=post
<?php query_posts( array ( 'Event_News' => 'event-news' ) ); ?>
<?php if (have_posts()) : ?>
<ul class="posts">
<li id="post-<?php the_ID(); ?>">
<?php
get_the_image( array( 'size' => 'loop-main', 'width' => 160, 'height' => 120, 'before' => '<div class="cover">', 'after' => '</div>' ) );
?>
<div class="postcontent">
<h2><?php the_title(); ?></h2>
<div class="postmetadata">
<ul>
<?php if (option::get('display_date') == 'on') { ?><li class="calendar"><time datetime="<?php the_time("Y-m-d"); ?>" pubdate><?php the_time("j F Y"); ?></time></li><?php } ?>
<?php if (option::get('display_author') == 'on') { ?><li class="author"><?php _e('By', 'wpzoom');?> <?php the_author_posts_link(); ?></li><?php } ?>
<?php if (option::get('display_category') == 'on') { ?><li class="category"><?php the_category(', '); ?></li><?php } ?>
<?php if (option::get('display_comments') == 'on') { ?><li class="comments"><?php comments_popup_link( __('0 comments', 'wpzoom'), __('1 comment', 'wpzoom'), __('% comments', 'wpzoom'), '', ''); ?></li><?php } ?>
</ul>
</div><!-- end .postmetadata -->
<?php the_excerpt(); ?>
<p class="more"><?php _e('continue reading »', 'wpzoom');?> <?php edit_post_link( __('Edit this post', 'wpzoom'), ' | ', ''); ?></p>
</div><!-- end .postcontent -->
<div class="cleaner"> </div>
<div class="sep"> </div>
</li><!-- end #post-<?php the_ID(); ?> -->
<?php endwhile; // ?>
</ul>
<div class="cleaner"> </div>
<?php else : ?>
<p class="title"><?php _e('There are no posts in this category', 'wpzoom');?></p>
<?php endif; ?>
<?php get_template_part( 'pagination'); ?>
</div><!-- end .box -->
You can do query_post just before the loop
query_posts( array ( 'category_name' => 'my-category-slug' ) );
If you want posts from more than one category then you can use
query_posts('cat=8,4,3');
8,4,3 are the id for the 3 categories
and after the loop use
wp_reset_query();
For more details view this link
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
Related
I have http://spainthephilippines.com/ where I use Rift Theme. Im having problem with showing the sidebar on single post page. On my index page the sidebar is showing, on the 404 page the sidebar is showing. It is working anywhere I use it just not in signle post page. I was looking 2 hours for answer but I cant find it. The theme is on version 1.0, so is it possible that to be the problem? Then why the sidebar shows on most pages? Anyone can help? Tell me if you need any code.
<?php global $theme; get_header(); ?>
<div id="main">
<?php $theme->hook('main_before'); ?>
<div id="content">
<?php $theme->hook('content_before'); ?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
/**
* Find the post formatting for the single post (full post view) in the post-single.php file
*/
get_template_part('post', 'single');
endwhile;
else :
get_template_part('post', 'noresults');
endif;
?>
<?php $theme->hook('content_after'); ?>
</div><!-- #content -->
<?php get_sidebars(); ?>
<?php $theme->hook('main_after'); ?>
</div><!-- #main -->
EDIT
Here is the sidebar:
<?php global $theme; ?>
<div id="sidebar-primary">
<?php
if(!dynamic_sidebar('sidebar_primary')) {
/**
* The primary sidebar widget area. Manage the widgets from: wp-admin -> Appearance -> Widgets
*/
$theme->hook('sidebar_primary');
}
$theme->hook("sidebar_primary_after");
?>
And also the post-single.php:
<?php global $theme; ?>
<div <?php post_class('post post-single clearfix'); ?> id="post-<?php the_ID(); ?>">
<h2 class="title"><?php the_title(); ?></h2>
<div class="postmeta-primary">
<span class="meta_date"><?php echo get_the_date(); ?></span>
<span class="meta_categories"><?php the_category(', '); ?></span>
<?php if(comments_open( get_the_ID() )) {
?> <span class="meta_comments"><?php comments_popup_link( __( 'No comments', 'themater' ), __( '1 Comment', 'themater' ), __( '% Comments', 'themater' ) ); ?></span><?php
}
if(is_user_logged_in()) {
?> <span class="meta_edit"><?php edit_post_link(); ?></span><?php
} ?>
</div>
<div class="entry clearfix">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail(
array($theme->get_option('featured_image_width_single'), $theme->get_option('featured_image_height_single')),
array("class" => $theme->get_option('featured_image_position_single') . " featured_image")
);
}
?>
<?php
the_content('');
wp_link_pages( array( 'before' => '<p><strong>' . __( 'Pages:', 'themater' ) . '</strong>', 'after' => '</p>' ) );
?>
<?php if(get_the_tags()) {
?><div class="postmeta-secondary"><span class="meta_tags"><?php the_tags('', ', ', ''); ?></span></div><?php
}
?>
<!-- Post ID <?php the_ID(); ?> wtf-->
<?php
if(comments_open( get_the_ID() )) {
comments_template('', true);
}
?>
I am trying to change the class if $firstPost = 0but i'm not sure 100% about the while loop etc as I have an idea I don't need it
PHP/HTML
<?php
$getPosts = new wp_query(array('showposts' => 5, 'orderby' => 1));
if($getPosts->have_posts()):
$firstPost = 0;
while($getPosts->have_posts()):
$getPosts->the_post();
$cssClass = '';
if($firstPost == 0)
{
$cssClass = array('article','first-post');
}else{
$cssClass = array('article');
}
?>
<article <?php post_class($cssClass); ?> id="post-<?php the_ID(); ?>" itemscope itemtype="http://schema.org/Article">
<?php if ( is_front_page() && is_home() ): ?>
<div class="post-thumbnail">
<?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else { ?>
<img src="<?php echo catch_that_image(); ?>" alt="" />
<?php } ?>
</div>
<article class="post-content">
<header class="post-header">
<div class="post-category"><?php the_category( ', ' ) ?></div>
<h2 class="post-title" itemprop="name"><?php the_title(); ?></h2>
<div class="post-date"><?php echo get_the_date( 'F j, Y' ) ?>.</div>
<div class="post-comment"><?php comments_popup_link( __( '0 comments','adelle-theme' ), __( '1 Comment','adelle-theme' ), __( '% Comments','adelle-theme' ) ); ?></div>
</header>
<?php the_excerpt(); ?>
<footer class="post-footer"></footer><!-- .post-footer -->
</article><!-- .post-content -->
<?php endif; ?>
</article><!-- .article -->
It's better to use a boolean value for $firstPost. For example:
<?php
$getPosts = new WP_Query(array('showposts' => 5, 'orderby' => 1));
if($getPosts->have_posts()):
$firstPost = true;
while($getPosts->have_posts()):
$getPosts->the_post();
$cssClass = '';
if($firstPost)
{
$cssClass = array('article','first-post');
}else{
$cssClass = array('article');
}
?>
<article <?php post_class($cssClass); ?> id="post-<?php the_ID(); ?>" itemscope itemtype="http://schema.org/Article">
<?php if ( is_front_page() && is_home() ): ?>
<div class="post-thumbnail">
<?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else { ?>
<img src="<?php echo catch_that_image(); ?>" alt="" />
<?php } ?>
</div>
<article class="post-content">
<header class="post-header">
<div class="post-category"><?php the_category( ', ' ) ?></div>
<h2 class="post-title" itemprop="name"><?php the_title(); ?></h2>
<div class="post-date"><?php echo get_the_date( 'F j, Y' ) ?>.</div>
<div class="post-comment"><?php comments_popup_link( __( '0 comments','adelle-theme' ), __( '1 Comment','adelle-theme' ), __( '% Comments','adelle-theme' ) ); ?></div>
</header>
<?php the_excerpt(); ?>
<footer class="post-footer"></footer><!-- .post-footer -->
</article><!-- .post-content -->
<?php endif; ?>
</article><!-- .article -->
<?php
$firstPost = false;
endwhile;
endif;
?>
I want to display recent images from wordpress gallery in the format like:
<div class="portfolio-item">
<div class="portfolio-overlay">
<img src="img/zoom.png" width="58" height="58" alt="" title="">
</div><!-- .portfolio-overlay-->
<img class="pi-img" src="HERE LINK ON IMAGE" alt="" title="">
</div><!-- .portfolio-item-->
How i should build my loop?
Thank You
If you sort the query results
"SELECT * FROM gallery ORDER BY date ASC LIMIT ?"
// where ? is the number of rows you want to return
you can just do a simple foreach loop:
foreach ($galleryPic as &$pic) {
//your code
}
First of all you should use thumbs for posts (read more about post's thumbs). Than in your category template use this loop:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article class="post">
<header>
<h2><?php the_title(); ?></h2>
<span class="date"><time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('j F Y'); ?> <?php _e('в');?> <?php the_time('G:i'); ?></time></span>
<span class="author"><?php _e( 'Author:', 'nikita-sp' ); ?> <?php the_author(); ?></span>
<span class="category"><?php _e( 'in category', 'nikita-sp' ); ?> <?php the_category(', '); ?></span>
<span class="comments"><?php comments_popup_link( __( '0 comments', 'nikita-sp' ), __( '1 comment', 'nikita-sp' ), __( '% comments', 'nikita-sp' )); ?></span>
<?php edit_post_link( "Edit", '<span class="edit">', '</span>'); ?>
</header>
<?php the_content('read more...'); ?>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<article>
<h2><?php printf( __( 'SEARCH RESULT for: %s' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h2>
<p class="center"><?php _e( 'Sorry, nothing was found', 'nikita_sp' ); ?></p>
<?php get_search_form(); ?>
</article>
<?php endif; ?>
Also use thumb output where y want.
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
Been trying to insert "By Author" by <?php the_author() ?> code on a Wordpress theme(Required by 8Bit). Request to help. Need to insert after the post/page title and before date.
The code is as follows:
<header>
<?php if ( 0 < strlen( get_the_title() ) ) { ?>
<h1 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h1><!-- /.entry-title -->
<?php } // end if ?>
<div class="post-meta">
<span class="post-date">
<?php if ( 0 < strlen( get_the_title() ) ) { ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php } else { ?>
<a href="<?php the_permalink(); ?>">
<?php the_time( get_option( 'date_format' ));?>
</a>
<?php } // end if/else ?>
</span><!-- /.post-date -->
<?php _e( ' | ', 'required' ); ?>
<span class="comment-link">
<?php
comments_popup_link( 'Comment', '1 comment', '% comments',
'comments-link', '');
?>
</span><!-- /.comment-link -->
<?php edit_post_link( '- edit ', '<span>', '</span>'); ?>
</div><!-- /.post-meta -->
</header>
Thanks :)
This is the loop code:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
} // end while
Here's an example, between the title and date:
<header>
<?php if ( 0 < strlen( get_the_title() ) ) { ?>
<h1 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h1><!-- /.entry-title -->
<?php } // end if ?>
<div class="post-meta">
<?php the_author(); ?>
<span class="post-date">
<?php if ( 0 < strlen( get_the_title() ) ) { ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php } else { ?>
<a href="<?php the_permalink(); ?>">
<?php the_time( get_option( 'date_format' ));?>
</a>
<?php } // end if/else ?>
</span><!-- /.post-date -->
<?php _e( ' | ', 'required' ); ?>
<span class="comment-link">
<?php
comments_popup_link( 'Comment', '1 comment', '% comments',
'comments-link', '');
?>
</span><!-- /.comment-link -->
<?php edit_post_link( '- edit ', '<span>', '</span>'); ?>
</div><!-- /.post-meta -->
</header>
I wish to insert a line of code inside my wordpress main page index.php which would display all posts except for posts that fall in 'Diary' category.
the original line of code is as follows
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div <?php post_class(); ?>>
<span class="postlabel"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></span>
<h2 class="post-title"><?php the_title(); ?></h2>
<p class="info">
<span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span>
<span class="cmtinfo"><?php comments_popup_link( __( 'Leave a comment', 'Mflat' ), __( '1 Comment', 'Mflat' ), __( '% Comments', 'Mflat' ) ); ?><?php edit_post_link(' Edit', ' | ', ''); ?></span>
</p>
<div class="entry">
<?php $Mflat_options = get_option('Mflat_theme_options'); ?>
<?php if ( $Mflat_options['home_excerpt_check']== 1 ) { the_excerpt(__('» Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?>
</div>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<p class="infobottom">
<?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?>
<?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?>
</p>
</div>
<?php endwhile; ?>
<div class="flip">
<div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div>
<div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div>
</div>
<?php else : ?>
<div id="main">
<h2><?php _e('Not Found', 'Mflat'); ?></h2>
</div>
<?php endif; ?>
I tried the following codes but to no avail
<?php if(have_posts()) : ?><?php while(have_posts()){
<if(!in_category('Diary')){
: the_post();
}
}?>
Is there any better way i can get this done?
I am new to PHP and though i know the logic of what has to be done, i cant quite generate the code to make this work. I think boolean would be a little too much work and found a similar (http://stackoverflow.com/questions/13587958/display-recent-posts-based-on-their-category-in-wordpress) thread on stark overflow but it doesnt quite suit my needs.
Every contribution is dearly appreciated. Thanks
just run the query_posts() function before the loop - it alters the loop
<?php
query_posts(array(
'category_name' => 'my-category-slug', // get posts by category name
'posts_per_page' => -1 // all posts
));
?>
<?php while(have_posts()): the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
My bad, I just noticed you want to exclude a category. Same function, different arguments:
$category = get_term_by('name', $cat_name, 'category'); // get category
$id = $category->term_id; // get the category ID
query_posts('cat=-' . $id); // exclude the found ID
<?php
$category = get_term_by('name', 'Diary', 'category'); // get category data
$id = $category->term_id; // get the category ID
// USE THIS IF YOU WANT TO _EXCLUDE_ ALL POSTS FROM "DIARY"
query_posts($query_string . '&cat=-' . $id);
// USE THIS IF YOU WANT TO GET ALL POSTS FROM "DIARY" ONLY
// query_posts($query_string . '&cat=' . $id);
?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div <?php post_class(); ?>>
<span class="postlabel"><span><?php the_time('d'); ?></span><?php the_time('M'); ?></span>
<h2 class="post-title"><?php the_title(); ?></h2>
<p class="info">
<span class="catinfo"><?php _e('Filed in ', 'Mflat');?><?php the_category(' | ') ?></span>
<span class="cmtinfo">
<?php comments_popup_link( __( 'Leave a comment', 'Mflat' ), __( '1 Comment', 'Mflat' ), __( '% Comments', 'Mflat' ) ); ?>
<?php edit_post_link(' Edit', ' | ', ''); ?>
</span>
</p>
<div class="entry">
<?php $Mflat_options = get_option('Mflat_theme_options'); ?>
<?php if ( $Mflat_options['home_excerpt_check']== 1 ) { the_excerpt(__('» Read more','Mflat')); } else {the_content(__('Continue Reading','Mflat'));} ?>
</div>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<p class="infobottom">
<?php if(function_exists('the_views')): ?><span class="count"><?php the_views(); ?></span><?php endif; ?>
<?php if (has_tag()): ?><span class="tags"><?php the_tags(' '); ?></span><?php endif; ?>
</p>
</div>
<?php endwhile; ?>
<div class="flip">
<div class="prevpost"><?php next_posts_link(__('Older Entries', 'Mflat')) ?></div>
<div class="nextpost"><?php previous_posts_link(__('Newer Entries', 'Mflat')) ?></div>
</div>
<?php else : ?>
<div id="main">
<h2><?php _e('Not Found', 'Mflat'); ?></h2>
</div>
<?php endif; ?>