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; ?>
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'm attempting to build a category.php page for a custom Wordpress theme. Ideally, the page would find all category posts, then grab the sticky posts from that category, and if there's at least two, it would display them at the top (it's fine if these same posts repeat below).
This is the page I have built so far. It can gather all of the results without error, but the nested loop which tries to find the most recent two stickies fails. I receive the error:
"Warning: Attempt to assign property of non-object in C:\dev\xampp\htdocs\wordpress\wp-content\themes\ampersand\category.php on line 22"
How do I fix this loop? (I've included a github gist in case that's easier to read, category page for Wordpress theme in development)
<?php // Category Page Content Start ?>
<div id="content">
<main id="main" class="category-page" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Periodical">
<h1><?php single_cat_title(); ?></h1>
<?php // Find all posts for the queried category ?>
<?php $catID = get_queried_object_id();
$args = array( 'numberposts' => -1, 'posts_per_page' => 20,
'cat' => $catID, 'post-type' => 'post' );
$query = new WP_query( $args ); $count = 0;
if ($query -> have_posts()) : ?>
<section class="featured">
<?php // Find the most recent sticky posts for this category; if at least 2, post them seperately ?>
<?php $sticky = get_option( 'sticky_posts' );
$findsticky = array( 'post__in' => $sticky, 'numberposts' => 2 );
$stickyposts = get_posts( $findsticky ); if ($stickyposts -> count = 2) :
foreach ($stickyposts as $post) : $post -> the_post(); ?>
<article class="featured-article" itemtype="http://schema.org/Article">
<meta itemprop="wordCount" content="<?php echo word_count(); ?>">
<figure class="thumbnail">
<div class="image-wrapper">
<a href="<?php the_permalink() ?>">
<img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
</a>
</div>
</figure>
<section>
<h2 class="headline">
<?php the_title(); ?>
</h2>
<span class="byline">
<?php $check = get_field( 'contributor' );
if ($check):?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
<?php the_field( 'contributor' ); ?>
</span>
<?php else: ?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
<?php endif; ?>
</span>
<?php $check = get_field( 'description' );
if ($check): ?>
<span class="description"><?php the_field( 'description' ); ?></span>
<?php else: ?>
<span class="excerpt"><?php the_excerpt(); ?></span>
<?php endif; ?>
</section>
</article>
<?php endforeach; endif; ?>
</section>
<section class="all-results">
<?php while ($query -> have_posts()) : $query -> the_post(); $count++; ?>
<article class="<?php echo 'article-'.$count; ?>" itemtype="http://schema.org/Article">
<meta itemprop="wordCount" content="<?php echo word_count(); ?>">
<figure class="thumbnail">
<div class="image-wrapper">
<a href="<?php the_permalink() ?>">
<img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
</a>
</div>
</figure>
<section>
<h2 class="headline">
<?php the_title(); ?>
</h2>
<span class="byline">
<?php $check = get_field( 'contributor' );
if ($check):?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
<?php the_field( 'contributor' ); ?>
</span>
<?php else: ?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
<?php endif; ?>
</span>
<?php $check = get_field( 'description' );
if ($check): ?>
<span class="description"><?php the_field( 'description' ); ?></span>
<?php else: ?>
<span class="excerpt"><?php the_excerpt(); ?></span>
<?php endif; ?>
</section>
</article>
<?php endwhile; ?>
</section>
<div class="pagination-bottom">
<?php if( get_next_posts_link() ) :
previous_posts_link( 'Older Posts' );
endif; ?>
<?php if( get_next_posts_link() ) :
next_posts_link( 'Newer Posts', 0 );
endif; ?>
</div>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; wp_reset_postdata(); ?>
</main>
</div>
I have a blog.php with the following code:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h1>
<section>
<h3><?php the_category(); ?></h3>
<h5><?php the_date(); ?></h5>
<h5><?php posts_nav_link(); ?></h5>
</section>
<p class="articlemeta center"></p>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php the_content(); ?>
<?php edit_post_link( 'bearbeiten' ); ?>
<?php posts_nav_link(); ?>
<?php endwhile; else: ?>
<h2>
Sorry noch keine Texte vorhanden
</h2>
<section class="entry-utility">
<?php $tags_list = get_the_tag_list( '', ', ' ); if ( $tags_list ):?>
<span class="tag-links">
<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
</span>
<span class="meta-sep">|</span>
<?php endif; ?>
</section>
<?php endif; ?>
My issue is now, that the dont compute a navigation. There is also no navigation when i paste the code at any other destination of the code. I hope anyone could help me to fix my issue.
Many thanks in advance.
I noticed you call <?php posts_nav_link(); ?> twice. I am not sure if this is an error because I don't have much to work with here.
I would take a look at the Wordpress Codex for guidance. An example they provide that might help is:
<div style="text-align:center;">
<?php posts_nav_link( ' · ', 'previous page', 'next page' ); ?>
</div>
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; ?>
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