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;
?>
Related
I'm modifying a pre-built theme to display 3 'featured posts' before the main grid of all posts on the index.php home page. I thought that the best way to do this was to do another loop before the main loop that queries posts with the category 'featured'. I need it to display the title of the post as well as the post categories in front of a background image of the post thumbnail.
However, when I use the_category(); the background image is no longer clickable and it seems that the anchor tag duplicates and closes itself around every element in the loop. My code is as follows:
<?php
$query = array(
'posts_per_page' => 3,
'post_type' => 'post',
'category_name' => 'featured',
'orderby' => 'date',
'order' => 'DESC'
);
$featured_home = new WP_Query( $query );
if( $featured_home->have_posts() ) {
?>
<div class="container featured-home">
<?php while ( $featured_home->have_posts() ) : $featured_home->the_post();?>
<div class="featured-home-box">
<a href="<?php the_permalink(); ?>">
<div class="featured-home-img" <?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"', $image_src[0] );
}?>>
<div class="blog-info-content">
<span class="cat"><?php the_category(); ?></span>
<h3><?php the_title(); ?></h3>
</div>
</div>
</a>
</div>
<?php
endwhile;
?>
</div>
<?php
}
wp_reset_postdata();
?>
Everything works fine until I add the_category();.
Now when I inspect the boxes I see this:
<div class="featured-home-img" style="background-image: url(bag.jpg);">
<div class="blog-info-content">
<a href="my-favorite-bag/">
<span class="cat"></span>
</a>
<ul class="post-categories">
<li>
Culture
</li>
<li>
Featured
</li>
</ul>
<h3>My Favorite Bag</h3>
</div>
</div>
The anchor link with "my-favorite-bag" (the permalink) is duplicated over and over again. Also, the category is NOT enclosed in the span with the class of "cat", as I would expect.
Why does this only happen when I add the_category or get_the_category?
How do I show the categories for each post in this loop?
The easiest way to get the category would be by passing the get_the_category() function the current post id.
$post_id = get_the_ID(); // or use the post id if you already have it
$category_object = get_the_category($post_id);
$category_name = $category_object[0]->name;
The get_the_category() function returns an object that contains properties such as the category id, it's name, etc...
Also, note that when using multiple wordpress loops, you may have to call wp_reset_postdata() to reset to the original query.
You can read more here:
Wordpress Wp_Query
get_the_category()
You can get the category name in number of ways:
within the post loop, if your posts have only one category then use as :
$cats = get_the_category();
$cat_name = $cats[0]->name;
and if your post have more than 2 categories then you can look for get_the_category_list().
Reference Link: https://codex.wordpress.org/Function_Reference/get_the_category_list.
for getting the link for this category you can use
$category = get_the_category();
echo '<img src="'.$category[0]->cat_name.'" alt="'.$category[0]->cat_name.'" />';
While working with the category archive (this could be used before the loop to save the category name to the $cat_name variable):
$cat_name = get_category(get_query_var('cat'))->name;
Reference Link: http://codex.wordpress.org/Function_Reference/get_category
Try this one...
It automatically creates a <ul><li>category name</li></ul> structure and shows name with the permalink. This should work for all options.
<?php the_category() ?>
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 ;-)
I've got this command to show the sub posts from that category but I want to be able to just show 1 post by ID but still under the specific category.
Here's the code I have:
<?php query_posts( array('cat' => $category_id,'posts_per_page'=>'1')); ?>
<li>
<?php while (have_posts()) : the_post(); ?>
<div class="sponsor-thumb"><?php the_post_thumbnail( 'category-thumb' ) ?></div>
<?php the_title(); ?>
</li>
<?php endwhile;?>
Please help! :)
I'm a bit unsure on what you want.
If you want to show a single post and you have the id of that post, just add 'p' => 123 to the query_posts array.
<?php query_posts( array('cat' => $category_id,'posts_per_page'=>'1', 'p' => 123)); ?>
Replace 123 with the id of the post you want to retrieve.
I am trying to show the most recent posts titles in the sidebar based on what category the post resides in. This code works great for a specific page template, but if I put this in the single.php file, I can only pull post titles from one category.
Is there a way to show post titles based on the category of the post?
<!-- BEGIN SIDEBAR -->
<div class="col-md-4 column blogsidebar">
<aside id="recent-posts-4" class="widget widget_recent_entries">
<h1 class="widget-title">Recent Articles</h1><hr>
<?php $my_query = new WP_Query('category_name=Blog&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); echo '<br>'; ?>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a><br>
<?php echo word_count(get_the_excerpt(), '12'); ?>...<br>
<?php endwhile; ?><p></p>
</div>
<!-- END SIDEBAR -->
First get the category of the visible post, and then query with that.
$post_cat_ids = wp_get_object_terms( get_the_ID(), 'category', array('fields' => 'ids'));
Then on your query,
<?php
$my_query = new WP_Query( array(
'category__in' => $post_cat_ids,
'showposts' => 10
));
?>
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