I am almost done with my humble attempt at a custom CMS using Wordpress. The only difficulty I have is making a page display the archive for only one category (and it's children). Anyone has an idea?
Thanks a lot!
Regis
You can create your own custom archive page using the class WP_Query. Specifically, something like:
<?php $query = new WP_Query('category_name=code'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<!-- display the category here -->
<?php endwhile; ?>
You can look at the default theme's archive.php to get a feel for what else is needed to display a particular category in a layout you are familiar with.
This is what I use to show a list of titles and permalinks for the category "mycategory"; this can go inside the main Wordpress loop:
<?php $my_query = new WP_Query('category_name=mycategory&showposts=-1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><?php the_title(); ?><?php endwhile; ?>
Change the "-1" to "1" to show only the one most recent post; to "10" to show the last 10 posts, etc.
Related
I want display 30 posts on category.php , but after 10 post loop is terminating where as i have more then 30 posts for particular category . I have tried on category.php with if(have_posts() )
Ohh i got answer .. It is simply SETTINGS -> READING -> Blog pages show at most
#Dibya
try this way to show the number of posts on category.php without affecting blog page
<?php
$query = new WP_Query('category_name=Category&posts_per_page=30');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
if (has_post_thumbnail()) {
?>
<?php the_post_thumbnail(); ?>
<?php
}
?>
<h2><?php the_title(); ?></h2>
<?php
the_excerpt(); // or the_content(); for full post content
endwhile;endif;
?>
This can also be resolved using posts_per_page = -1 parameter in wp_query. By using this , you don't need to bother about the backend settings. This will always works.
I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!
Does anyone know what the best way to implement a list/grid togglable view in wordpress would be?
To be more clear: the default view shows the posts in a list, I would like to have the option to show them in a grid.
I have so far created a loop which shows only the thumbnails, and included it in another template.
But I don't know how I would link to that view. Would I best off using an archive view?
Thanks.
One of the easiest solutions is to create a page template containing a grid view and add a link to this page in the list view. Very simple but just what you need.
I had the same issue with pagination. WordPress has built in function posts_nav_link that automatically prints links to previous and next pages if needed. The problem is that this function works only with $wp_query instance of WP_Query (this instance is used by default, for example to get page's content or the latest posts in home). So the solution for you:
<?php
$temp=$wp_query;
$wp_query=null;
$wp_query = new WP_Query('showposts=4');
while(have_posts() ) : the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile;
posts_nav_link();
$wp_query = $temp; ?>
I think that's quite obvious and there is no need of explanation :) should be working
Thanks dude. I kind of got it working for now with this:
<?php $latest = new WP_Query('showposts=4'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I think my mistake was trying to get it to use get_template_part( 'loop', 'grid' ); It would show the page, but no posts. Even if used get_template_part( 'loop', 'index' );
I'll need to figure out the pagination, but I'm putting it down for now to work on something else.
Thanks for your help so far! #Gediminas
I have a page with template No Sidebars I want to list 5 posts' titles on that page by author where the author's name = page's title
any idea how to do so without using any plugin?
I thought that query_posts function would do the trick but this important note kind of tells me that I cannot use query_posts
Here's a bit of code that will probably get you the Post Titles by author; in order to have this automatically feed off of the page title I'd have your title generation code... Hope this helps you get at least part of the way there...
<?php $my_query = new WP_Query('author_name=YourAuthor&showposts=5'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a><?php endwhile; ?>
Check out Function Reference/WP Query « WordPress Codex and an earlier answer (with the same code :) as poindexter's) at Wordpress display featured posts outside of loop
I have a permalink structure of /%catergory%/%postname%/.
When I go to blah.com/categoryname I want all posts in that specific category to be listed.
When I go to blah.com/categoryname/post-name I want just the specific post to be displayed.
I have made a category specific template (category-5.php) and have got as far as...
// Display all post titles in category loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
// Display specific post in category loop
<?php if ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="storycontent">
<?php the_content(); ?>
</div>
<?php endif; ?>
...but obviously I only want the first loop to display when the url is blah.com/categoryname, and the second loop to display when the url is blah.com/categoryname/post-name.
Any thoughts? thanks
You can't have two loops running on the same page as you do here.
I believe you need to separate out the two things you're trying to do. To have a unique look/feel for the category, create a category-1.php file. To create a unique look/feel for the posts within that category, create a separate 'single' template.
This WP support thread explains how to create the 'single' template: http://wordpress.org/support/topic/266638
There are also a few "post template" plugins that help accomplish the same thing, if you prefer to go that route, for instance:
http://wordpress.org/extend/plugins/post-template/
Full list here: http://wordpress.org/extend/plugins/search.php?q=post+templates
Good luck!