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!
Related
i would like to have a sidebar appear in a single posts for a specific category. I not great at php so this is what i have been able to come up with. It does not work of course lol. Your help will be very much appreciated!
<?php if( in_category('9') ) : ?>
<div>
<h1>Other Products I Recommend</h1>
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('products2')) : ?>
[ do default stuff if no widgets ]
<?php endif; ?>
</div>
<?php endif; ?>`
There are two options for that:
There is a plugin that can help you with this. It's called jetpack. When it is installed and activated, when you add any widget to a sidebar there will be a visibility option, click that and choose which pages/categories/etc to show the widget it.
Code:
As for your code:
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('products2')) : ?>
that says if the dynamic_sidebar function doesn't exists or if it's not the products2 sidebar then do X. That function will exist as sidebars are set in that function, so nothing inside that if statement will run.
You will want something like:
<?php if( in_category('9') ) : ?>
<div>
<h1>Other Products I Recommend</h1>
<?php dynamic_sidebar('products2'); ?>
</div>
<?php endif; ?>
that should display the contents of what you have added to the products2 sidebar only when you are in a post that has a category id of 9.
How can I "tell" Wordpress to choose a different single post template if the user making the request happens to be a logged in subscriber?
This is what I want to do.
Our company distributes products.
The general public can check out the individual products (single posts) and have access to some information (public pricing, etc.).
The plan is to manually add our resellers as subscribers in WP admin, let them log in as subscribers in our website, and then they would be able to see a similar single post, but with some more information thrown in (reseller pricing, volume discounts, links to service manuals, stuff like that).
Is this easy to accomplish?
Thanks!
Are you comfortable with PHP? What you'd have to do is find the template directory and edit (or create) single.php and use get_template_part to display the correct single.php template:
<?php get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_user_logged_in() ) :
<?php get_template_part( 'single' ); ?>
<?php else: ?>
<?php get_template_part( 'single', 'guest' ); ?>
<?php endif; ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
P.S. I copied that single.php page from https://github.com/Automattic/_s/blob/master/single.php
I would handle this with the built-in WordPress function is_user_logged_in().
From a high level, I would actually create a code snippet in my single.php template like so:
if (is_user_logged_in()) {
include 'single_subscriber.php';
}
else {
include 'single_visitor.php';
}
Then you can actually keep single.php clean and simple. But put your actual templates for different users in different files.
Of course, if you have common functionality or logic it would be best to do all of those common things in single.php where possible, and perhaps tweak my example to only add more items using that built-in function.
currently I am making a custom theme for my client and I am not a expert in this. My question is how to make different style for post from same category. Currently in my theme
Starting a New query for first post
<?php query_posts('showposts=1&cat=videos&offset=0'); if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<div class="first-news">
<h2><a href="<?php the_permalink() ?><?php the_title(); ?></a></h2>
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('video-thumb');?<?php} ?>
<?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,8); ?>
</div>
<?php endwhile; else: endif; ?>
then again starting the same query for remaining 4 posts with another div and style
<?php query_posts('showposts=4&cat=videos&offset=1'); if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<div class="second-news">
<h3><a href="<?php the_permalink() ?><?php the_title(); ?></a></h3>
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('news-thumb'); ?><?php } ?>
<?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,8); ?>
</div>
<?php endwhile; else: endif; ?>
this working perfectly, Is this correct? I think there may be a good solution which will query post only once and get the required number of posts from same category with different style.
What I want is on below image.
You should use the category template from wordpress.
Before loading your page, wordpress looks for the presence of specific templates, example from the page linked above.
1. category-slug.php
2. category-ID.php
3. category.php
4. archive.php
5. index.php
In order to activate “post formats” in WordPress 3.1+, you will need to open your theme’s functions.php file and paste the following code:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
Note: aside, and gallery are not the only available post formats. The available list of post formats are:
aside – Typically styled blog format.
chat – A chat transcript.
gallery – A gallery of images.
link – A link to another site.
image – A single image.
quote – A quotation.
status – A short status update, usually limited to 140 characters. Similar to a Twitter status update.
video – A single video.
For the full list of post formats, refer to WordPress Codex.
Once you have added this code, you will see a new field in your post write panel in the right hand column where you see publish.
Upon writing the post, you can change the format and hit publish. This will allow you to display your post in a pre-styled format.
Edit your post loop.
Suppose in your case videos category post format is video
We are going to be utilizing the conditional tag: has_post_format()
if ( has_post_format( 'video' ) {
// Blog Category format
}
else
{
// Normal Formate
}
I hope this will help you. More Info...
I just installed Views module for Drupal 7 and am trying to find out how to customize it.
So far, I have done the following things.
Created a content type specified views and named it as 'videotest'.
Created a custom theme as 'views-view-list--videotest.tpl.php'
The page is working without a problem.
Here is the custom template code I used (this is the default template from Views):
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<?php print $list_type_prefix; ?>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
<?php print $list_type_suffix; ?>
<?php print $wrapper_suffix; ?>
How do I prevent Views to format $row for me? I like to format each values in a node myself using foreach.
I tried to set the view style as 'unformatted' with unformatted custom template, but it also style each value for me.
You should to use fields templates not 'Display', 'Style', or 'Row'.
Or you can use both fields in 'Row' template. Ex:
print $fields['you_field_value']->raw;
Name of 'you_field_value' show as part of link in fields list of view display.
Or to get the value of field:
print $fields['field_pretitle_front']->content;
print $fields['your_field_value']->raw;
did not work for me, however the following did in my case.
$row->field_YOUR_FIELD[0]['rendered']['#markup'];
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!