Wordpress grid/list view - wordpress

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

Related

Wordpress how to create custom post template

Wordpress how to create custom post template from parts of INDEX.php! On my Frontpage (Index.php) i have a great Post Grid. I would like to have this Grid also under each post (Under the Post Content between Comment field). I have copy some parts from the (Index.php) Code and inserted in the post.php but, the result was not satisfactorily.
I have searched everywhere but can not find a satisfying solution, which is compatible with my wordpress theme. If someone can help me, please let me know what code do you need. I would be more than happy to hear a solution!
Thanks in Advance
Not very clear what you mean, but I think you want to print some posts on your single.php file. To accomplished this, you'll need a WP_Query similar to:
<?php
// the query
$args = array(
'post_type' => 'post' // change as needed
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2> <!-- Copy same structure and data as the one on the index.php file -->
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
You will need to keep the same div structure and classes as the one on the index.php to make them look alike.
Good luck!

Page with posts of specific category

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!

Restrict a Wordpress Template to show only one category

I tried everything found in the web, but i'm getting errors or that's not what I am searching for...
I have to make a PAGE in wordpress, to show ONLY the posts of the category with a certain ID ( in my case id=8 )
i tryed to edit the loop-xxxx.php .. the template file... everything but I get always a problem
navigation system doesn't work. I mean... getting back to older posts won't work cause the output shows the last posts instead of older one.
The code I'm using in the loop or in the template file is:
<?php
query_posts('cat=8');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
i tried inserting it before the
<?php while ( have_posts() ) : the_post(); ?>
in loop.php
or before the call of loop inside the index.php
please help me :\
One solution is to use a custom WP_Query. In the custom page's TEMPLATE file, where ID is the id of the targeted category:
<?php $tmp_query = new WP_Query('cat=ID');
while ( $tmp_query->have_posts() ) : $tmp_query->the_post();
the_content();
endwhile;
wp_reset_postdata();
?>
Check this.
<?php query_posts($query_string . '&cat=8'); ?>
<?php if (have_posts()) : ?>
<optional> You can write here: "You are in category X". </optional>
<?php while (have_posts()) : the_post(); ?>
Good luck.

Page template, single page display

I've built a custom page.php template. Very simple, essentially:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php get_footer(); ?>
I've created a few pages, and if I visit their url, I just get the same page title.
I read up in the documentation, and it says to use the_title() and such only when in "the loop".
So presumably, I'm just being shown the first page in the "array".
Is there any way I get get the contents of a single page based on the url?
Edit: In fact, should I even need to do this? Refering to example templates, it looks like I'm doing everything right?
In my sidebar I was using a custom query.
This was called before trying to access the main page content, without resetting.
When doing a custom query you must reset after you've finished your loop like so:
$originalPost = $post;
$sidePosts = get_posts($queryArgs);
foreach($sidePosts as $post) {
setup_postdata($post);
// echo it out like a normal post.
}
$post = $originalPost;
or if you are using query_posts() (which you shouldn't in a sidebar):
wp_reset_query();
Which will take your post back to it's previous value.
For a custom page template please use the following to get everything correct
<?php
/* Template name: My custom template */
get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_title();
the_content()
endwhile;
get_sidebar();
get_footer();
?>

Display Wordpress Archives one category at a time?

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.

Resources