I'm developing a website with Wordpress. What i need is make a page which shows only post with a specific category. What shall I do?
For instance, page Javascript, only displays post with the cateogry Javascript.
I don't know how good you are with PHP, but you need to create a Wordpress template where it queries the posts from your category.
You can read about the archive page here where it displays all your posts:
http://wp.tutsplus.com/tutorials/create-a-wordpress-archives-template-for-your-theme/
Just before this:
<?php while ( have_posts() ) : the_post(); ?>
Add this to query it to a specific category:
<?php query_posts('category_name=News'); ?>
Where 'News' is your category name!
<?php query_posts('cat=1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title();?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
You can read more about query_post here http://codex.wordpress.org/Function_Reference/query_posts
Related
I have tried to implement category and year(from post publish date) filter. To do this I have used the following URL structure:
http://www.horsenskunstmuseum.dk/2016/?cat=5
http://www.horsenskunstmuseum.dk/2015/?cat=5
http://www.horsenskunstmuseum.dk/2014/?cat=5
Here the years are from post date and 5 is the category id.
So all the above links works but when I click on 2017 to go to
http://www.horsenskunstmuseum.dk/2017/?cat=5
It does not show any post. But there are two posts in 2017 with category id 5.
If I open the blog page http://www.horsenskunstmuseum.dk/test/ the post from 2017 shows up.
May I know what might be the reason for the posts to not appear in
http://www.horsenskunstmuseum.dk/2017/?cat=5
Code:
category-tidligere-udstillinger.php
<?php
get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php
while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<?php $image_size = ( get_field('image_disable_force_ratio') ) ? 'blog-thumb-normal' : 'blog-thumb'; ?>
<?php the_post_thumbnail( $image_size ); ?>
<?php else : ?>
<?php echo '<img src="'.get_template_directory_uri().'/images/no-thumbnail.jpg" alt="No thumbnail">'; ?>
<?php endif; ?>
<?php
endwhile;
the_posts_navigation();
endif; ?>
<?php get_sidebar('exhibition'); ?>
<?php get_footer(); ?>
Also if I check just the yearly archives:
http://www.horsenskunstmuseum.dk/2012/
http://www.horsenskunstmuseum.dk/2016/
works but
http://www.horsenskunstmuseum.dk/2017
does not show any post.
The problem was occurring because there was a media file with the name '2017.jpg'. I renamed the media file and the archives loaded properly. Strange issue though.
I didn't know media file names can create a conflict with archive name.
If any one has any idea on this feel free to comment.
I used the following code in my page template:
<?php
while(have_posts()):the_post();
the_content();
?>
But nothing is displayed. The loop is not working. I'm sure that, there is sufficient information as content in my template page.
You should use if condition to check if post exists else skip the loop. Make sure to ON the error log and check the exact error.
<?php wp_reset_query(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php
/**
* Template Name: My Template
*/
the_post();
the_content();
?>
If you save the following code as a page template and call the page template into a page by choosing it, then the page will show the page content without any hassle.
Note: it's THE minimal bit of code.
use the following code
Try using wp_reset_query()
<?php
wp_reset_query();
while(have_posts()):the_post();
the_content();
endwhile;
?>
In the Twenty Fourteen theme and many others authored by experts, single.php contains the code:
while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
endwhile;
Is this any different or better than
the_post();
get_template_part( 'content', 'page' );
Is there a scenario where WordPress may expect single.php to be able to display more or less than exactly one post or is there another reason authors choose to use a while loop?
the while ( have_posts() ) : the_post(); is there by default so you can put conditional logic like in the example code below and won't get a 404 just incase there's no post or the permalink changes.
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Hope it helps.
I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/
I've made my front page a static page with wordpress. I would still like to be able to link to a listing of blog posts. How is this done?
create a new page template file called page-home.php
best way to do this is copy and paste the code from index.php into your new page...
inside this page create a new template call..
<?php
/**
Template Name: home
*/
?>
inside this page paste your index.php code..
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_content('<p>Read the rest of this page ยป</p>'); ?>
<?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php endwhile; endif; ?>
this will display your home page as usual, then you have to create a new call to the wordpress database to display your latest posts from which ever categories you want or show all posts from all categories..
<?php
//The Query
query_posts('posts_per_page=5&cat=YOUR_CATEGORY_HERE');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
this will then loop through the posts and display your latest posts..
have a check through the wordpress query posts codex