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.
Related
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!
I have a wordpress plugin (broadstreet) which is a company directory and has list of companies. But I want to display in different style to match the rest of the website. Now all the company details are stored in the plugin. Is it possible for me to read the data stored in the plugin and display in a page and publish the same using wordpress? If so how?
Any help is greatly appreciated. Thanks
Yes you can, In this case, what I do is create a custom template page. For that you can go to theme you are refering and make a copy of page.php file and rename it. let's say it is directory-page.php.
Next go to admini panel and create a new menu item and set it is template to derectory-page.
Now you can open it and customise as you expect it.
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php //get_template_part( 'content', 'page' ); ?>
<?php// comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
If you want to remove the blog content and call your directry displaying method.
simply, comment out
<?php //get_template_part( 'content', 'page' ); ?>
<?php// comments_template( '', true ); ?>
<?php echo call_directory_display();?>
Now you have to implement call_directory_display() method in your plugin to show content in derectory-page.
If you have any questions ask in comments.
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'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();
?>
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!