Wordpress Page for custom taxonomy - php

I'm a newbie in WordPress and I've just created a new custom-taxonomy called categories under a custom post type called arts. Is there a way I can create a custom page to display the 'categories's custom type? Such that a user can navigate to arts/categories and see all the terms under the categories taxonomy.

Create a file in your theme folder named taxonomy-categories.php and include your loop inside it...
Sample loop here:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a></h2>
<?php the_conetent(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
This loop will display all your posts assigned to custom taxonomy named "categories"
For Detailed Information Check this: http://codex.wordpress.org/Template_Hierarchy

save a file named taxonomy-categories.php and paste the following code in that file.
<?php
$args_pdf = array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms('categories', $args_pdf);
foreach($terms as $term) {
echo '<h1>' . $term->name . '</h1>';
}
?>

This loop will display all your posts assigned to custom taxonomy named "categories"
For Detailed Information Check this: http://hinhcuoidep.com.vn/ but i need load taxonomy category in home page but it dont work in main index template

Related

how to loop through posts in both front page and blog page?

i want to display some posts in home page according to post type but i can't access posts and post loop returns " Home " post only ,so what is the proper way to make this happen ?
my reading settings
Front page displays : A static page
Front page :home
Posts page :blogs
Keep your reading settings the way they are.
You can create a theme file called front-page.php and use this to control your home page.
Within your front-page.php you can use get_posts to retrieve posts and then loop through them
Example:
<?php get_header(); ?>
<?php
$args = array(
'numberposts' => 10, // number of posts to return
'post_type' => 'your-post-type' // change this to the post type you want to retrieve
);
$posts = get_posts( $args );
if ( $posts ) :
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<article <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>

How to write a template to list all posts with a custom taxonomy term

I have used the CPT (Custom Post Types) plugin to create a custom post type called Resources, and a custom taxonomy called Resource Categories. Within that taxonomy I have two categories/terms: blogs and books.
I have already figured out which file to edit: taxonomy.php
But I can't figure out how to write the template. Here is what I have so far:
<?php get_header(); ?>
<section class="content">
<div class="page-title pad group"><h2><?php single_term_title(); ?></h2></div>
<div class="pad group">
<?php if ((category_description() != '') && !is_paged()) : ?>
<div class="notebox">
<?php echo category_description(); ?>
</div>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<div class="post-list group">
<?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ): the_post(); ?>
<?php get_template_part('content'); ?>
<?php if($i % 2 == 0) { echo '</div><div class="post-row">'; } $i++; endwhile; echo '</div>'; ?>
</div><!--/.post-list-->
<?php get_template_part('inc/pagination'); ?>
<?php endif; ?>
</div><!--/.pad-->
</section><!--/.content-->
<?php get_sidebar(); ?>
<?php get_footer();
What Works:
The page is showing up when I go to resource-categories/books. Good!
The title of the category "Books" and the category description show up. Good!
What Doesn't Work:
There is no list of items from the category. It's just blank. I originally copied the code from the archive.php, which works just fine on Search pages, Author Lists, I think Category Archives, too...
What's wrong? I'm a php n000000b so I'm having a hard time translating tutorials/answers on the internet to solve this problem.
Use your custom WP_QUERY
$args = array(
'post_type' => 'resources',
'tax_query' => array(
'taxonomy' => 'resource-categories',
'field' => 'id',
'terms' => get_queried_object()->term_id,
);
);
$query = new WP_QUERY( $args );
while ( $query->have_posts() ):
$query->the_post()
..
endwhile;
Your issue is not your code, that is correct and should work out of the box.
Check the following:
Make sure that the public parameter for both your custom post type and custom taxonomy is set to true in register_post_type() and register_taxonomy()
Resave/reflush your permalinks by simply visting the permalinks setting page in back end.
This should solve your issue. If it does not, then you have serious conflict or bad filter in a plugin or in your theme.
Just a note, NEVER EVER run a custom query because something in the main query needs to be changed or is not working in the main query. Fix the issue properly and correctly, and do not hide it ;-)

WordPress Posts from category won't display

I use a plugin called "Custom Content Type Manage"
this allows me to create a new content type which essentially mimics posts (all done in the name of 'user friendly')
This aside, I wrote the following:
<h2><?php single_cat_title( '', true ); ?></h2>
<p>
<?php
$page_id = '5536';
$page_data = get_page($page_id);
print apply_filters('the_content', $page_data->post_content);
?>
</p>
<p>
<?php
$category = get_the_category();
$category_id = $category[0]->cat_ID;
print category_description( $category_id );
?>
</p>
<div class="category-list">
<?php wp_nav_menu(); ?>
</div>
<ul class="leaders-container">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<li class="leader-container">
<?php
$image = get_custom_field('leader_image:to_image_array');
$url = get_custom_field('website_url:raw');
print "<img class='leader-image' src='".$image['0']."'>";
?>
<h2>
<?php the_title(); ?>
</h2>
<?php
print "</h2>";
the_content();
if($url != "#") {
print "<a class='website-button' href='".$url."' target='_blank'>Visit Website</a>";
}
?>
</li>
<?php endwhile; endif;?>
What this was doing, was getting the info from the category, and listing all the posts assigned to the category.
In this case, a post was labeled as a "Leader" and a category was their "congregation", so it was listing all the leaders assigned to the congregations.
This is an example of what it should look like
http://www.ubmsonline.org/?leader=rabbi-binyamin-sheldrake
However, this only works as it is a direct link from the posts in question
The category on the otherhand, which was working, and did list as many leaders assigned to the category, has now stopped working.
http://www.ubmsonline.org/category/ubms-leaders/
As you can see though, its pulling everything across correctly, category description, category title etc, but its just now not showing the posts.
fixed!
It was to do with a setting that changed on the "Custom Content Type Manager" plugin im using. After alot of research, i stumbled upon the exact problem on the "issues" section of the plugin's FAQ's page.
https://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=594
Hopefully this might help others.
In wp-content/plugins/custom-content-type-manager/loader.php the filter mentioned below might be commented, so if you uncomment this add_filter this bug will fix.
// Enable archives for custom post types
add_filter('request', 'CCTM::request_filter');
Thanks again for every one's help.
Andrew

Retrieve posts by category name issue in WP Query

So I've created a custom page template for my "Topics" Page.
What I want to do is add in some PHP to the custom page template my Topics page uses, to retrieve permalinks for the 3 most recent posts from a chosen category.
E.g.
(From post category 1)
--> Permalink for post 1
--> Permalink for post 2
--> Permalink for post 3
My code so far is as follows:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<ul>
<?php
$category_posts = new WP_Query('cat=consumer-trust&showposts=3');
while ($category_posts ->have_posts()) : $category_posts->the_post();?>
<li>
<a class="yourclass" href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></li>
<?php endwhile; ?>
The problem, however, is that changing the cat in the WP_Query doesn't seem to make any difference. I've tried numbers and category names, and neither works.
Can anybody advise? This code will appear three times on the intended page, for three different categories.
use
query_posts( array ( 'category_name' => 'cat_name','meta_key'=>'_pr_date', 'orderby' => 'meta_value','order'=>'DESC') );
Thanks for all the help, I've managed to find an answer:
<?php global $post; // required
$args = array('category' => 18); // include category 18
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
// put here what you want to appear for each post like:
//the title:
the_title();
endforeach;
?>

Archives.php not working in wordpress

Its been a while since ive done wordpress templating and for some reason i cant get the archives.php to work. i have this:
<?php
/* Template Name: Archives */
get_header(); ?>
<div id='content'>
<?php get_sidebar('left'); ?>
<div id='column2-wide'>
<?php if (have_posts()): while (have_posts()): the_post(); ?>
<?php if ( in_category(16) ) { ?>
<h2><?php the_title(); ?></h2>
<div class="post">
<?php echo the_content(); ?>
</div>
<?php } ?>
<?php endwhile; endif; ?>
</div><!-- column2 -->
<?php get_footer(); ?>
Then created a page in the admin and chosen the Archives template to be used from the dropdown.
However the posts just dont seem to show. Am i missing something? The very same code works in the index.php file. It seems its just not working when im trying to display posts in a page.
It could well be im missing a file as I started developing the theme using a skeleton theme by Kennethreitz which can be found here:
https://github.com/kennethreitz/wordpress-theme-skeleton/
Any help would be appreciated.
Thanks for reading.
fl3x7
EDIT--> Also ive removed the category check so it should just list all posts but instead what it does is just echo the title of the current page if that helps
I'm assuming that "16" is the category ID? According to the WordPress docs for in_category, if you're querying by ID it should be passed as an integer.
$category
(mixed) (required) One or more categories specified by ID
(integer), name or slug (string), or an array of these
With your current code, the in_category check is failing every time since it is checking for an association with the category named "16." Try this instead:
if ( in_category(16) ) {
...
}
Thanks to the help provided by Hans. This is what I did:
query_posts( array ( 'category_name' => 'foo', 'posts_per_page' => 10 ) );
if (have_posts()): while (have_posts()): the_post(); ?>
<h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
<div class="post">
<?php echo the_excerpt(); ?>
</div>
<?php endwhile; endif; ?>

Categories