How can I get a serial number of current post in category?
For ex. I have a category Cars with 4 posts in it. When I open some post I want to see navigation like this: Post 3 of 4 [<<] [>>]
Most straightforward way is querying the posts in the category, like this:
// WP_Query arguments
$args = array (
'category_name' => 'cars',
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
Then you can get the number of posts with
// $query->found_posts gives the number of posts the query has found
// with the parameters you set
echo $query->found_posts;
And you can count up the post you display:
$count = 0;
foreach ( $query->posts as $count_post ) {
$count++;
// assuming you are inside The Loop
if ( get_the_ID() == $count_post->ID ) {
break;
}
}
// now you can get the "serial number" of the post
echo $count;
This might not be the most "WP way" of doing it, but it should work. :)
Related
I want to list all pages as well as selected pages in A-Z listing in WordPress. I know there are a number of plugins available, but I want this without a plugin.
Update
sorry if question is not clear, i want A-Z listing like attached image
i have solved this issue,
just need to put if condition, here is the code
$arr[0] = array(2=>2983);
$arr[1] = array(2=>2981);
$arr[2] = array('A'=>20);
$arr[3] = array('A'=>25);
print "<pre>";
print_r($arr);
$newArry = array();
foreach ($arr as $a) {
foreach ($a as $key => $value) {
if (array_key_exists($key, $newArry)) {
//$newArry[$key] = array($value);
array_push($newArry[$key], $value);
} else {
$newArry[$key] = array($value);
}
echo "<br/> Key ".$key ." => Value ".$value;
//print_r($b);
}
}
print_r($newArry);
You can use a WP_Query, as page is simply a post type.
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$pages = $query->posts;
See WP_Query documentation.
How can i get the no of posts inside a term of a custom taxonomy ? (including the posts that are attached to a child term).
For example i have:
term (2 posts)
-child term (2 posts)
--child child term (1 post)
Right now i'm, doing it like this:
$categories = get_terms($taxonomy,$args);
foreach ($categories as $categ) {
print $categ->name.' / '.$categ->count;
}
But for "term" i get only 2 posts when i really need to show 5( 2 from "term" and 3 from it's children).
Thanks
There is an easier way to do this: do a standard WP_Query with taxonomy parameters:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'slug',
'terms' => 'your-parent-term-name',
),
),
);
$query = new WP_Query( $args );
// Get the count
$count = $query->post_count;
If you don't know the name(s) of terms within a taxonomy, you can do a query and pass their IDs as an array to WP_Query. See this post on WPSE for more info.
This is something I run into few times, and I changed the code from PatchRanger on bountify to make it work with taxonomies (so please note this solution is based on his work):
function wp_get_term_postcount($term) {
$count = (int) $term->count;
$tax_terms = get_terms($term->taxonomy, array('child_of' => $term->term_id));
foreach ($tax_terms as $tax_term) {
$count += wp_get_term_postcount_recursive($tax_term->term_id);
}
return $count;
}
function wp_get_term_postcount_recursive($term_id, $excludes = array()) {
$count = 0;
foreach ($tax_terms as $tax_term) {
$tax_term_terms = get_terms($tax_term->name, array(
'child_of' => $tax_term->term_id,
'exclude' => $excludes,
));
$count += $tax_term->count;
$excludes[] = $tax_term->term_id;
$count += wp_get_term_postcount_recursive($tax_term->term_id, $excludes);
}
return $count;
}
The recursive function is there to prevent double counting of childs. You can add those two functions inside functions.php.
Then update your code to use it:
$categories = get_terms($taxonomy, $args);
foreach($categories as $categ) {
print $categ->name.' / 'wp_get_term_postcount($categ);
}
I've got two custom post types, for example 'Cars' and 'Bikes'. I've used Wordpress's default category to categorise the posts from the two post types. Let's say for example the categories are 'Red', 'Blue' and 'Black'.
What I'm trying to achieve here is that when I go to the category page for 'Red', I want to see the 'Cars' and the 'Bikes' that are categorised under 'Red'. I'm using the category.php and this is the query that I'm trying to run:
$car_args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'cars',
'post_status' => 'publish',
'cat' => $cat
);
// The Query
$car_query = new WP_Query( $car_args );
// The Loop
if ( $car_query ->have_posts() ) {
echo "<h3>Cars</h3>";
while ( $car_query->have_posts() ) {
$car_query->the_post();
echo get_post_type() . '' . get_the_title() . '<br />';
}
} else {
// no posts found
}
$bikes_args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'bikes',
'post_status' => 'publish',
'cat' => $cat
);
// The Query
$bikes_query = new WP_Query( $bikes_args );
// The Loop
if ( $bikes_query ->have_posts() ) {
echo "<h3>Bikes</h3>";
while ( $bikes_query->have_posts() ) {
$bikes_query->the_post();
echo get_post_type() . '' . get_the_title() . '<br />';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
The $cat in the query gets the category id of 'Red' category. Both these queries are correctly restricting the posts by the 'Red' category but posts from both the 'Cars' post type and 'Bikes' post type are showing up even though I've tried to restrict by post type. I've read in a few articles that Wordpress ignores the post type args on the category page. Is this true and if it is, is there a workaround for this?
What you are trying to do is possible with one query only, and only with the main query without any custom queries.
First of all, lets first add your custom post types to your category page. By default, custom pist types are excluded from category pages. So we need to add this manually to the main query arguments via pre_get_posts. Add the following to your functions.php: (CAVEAT: Untested and also requires PHP 5.3+)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_category() ) {
$q->set( 'post_type', array( 'post', 'cars', 'bikes' ) ); // Change this according to your post types
$q->set( 'nopaging', true ); // This will get all posts, same as posts_per_page=-1
}
});
You should now have all posts from a clicked category is your set post types on your category pages.
Next, wee need to sort out your loops. Delete all your custom queries in category.php and replace it with the default loop. As you would want two block ordered by post type, we will use rewind_posts() so we can run the loop twice
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if ( $post->post_type == 'cars' ) { //Change accordingly to only show cars in this loop
// Your loop
}
}
rewind_posts();
while ( have_posts() ) {
the_post();
if ( $post->post_type == 'bikes' ) { // Change accordingly to only show bikes
// Your loop
}
}
}
This should now display your posts in two block sorted by post type
I am accessing archive-products.php on woocommerce to display my products (like the normal process in woocommerce).
On the page of archive-products.php I have added the sidebar with all the product categories that my shop has (with or without products). I have used the following code to do so:
$taxonomy = 'product_cat';
$orderby = 'ID';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '<h2>' . _x('Our Products', 'mfarma') . '</h2>';
$hide_empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'order' => 'ASC',
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $hide_empty
);
?>
<ul>
<?php wp_list_categories($args); ?>
</ul>
Now the left side of the page has the above sidebar and the right one has the products. In each product category I have added a small description with an html format that I want to show when the user has clicked the category. According to woocommerce when you go to a specific category (in my case, http://localhost/product-category/mycategory) it is still the archive-products.php.
I am trying to get the term_id from the link clicked, but the loop (and the global $post) points me to the first product of the list instead of the category that I need. So if a category has zero products, I can't get the term ID. How do I get that term ID from archive-products.php?
Found the answer for something else but it also applies to my question.
add_action('woocommerce_archive_description', 'woocommerce_category_description', 2);
function woocommerce_category_description() {
if (is_product_category()) {
global $wp_query;
$cat = $wp_query->get_queried_object();
echo "CAT IS:".print_r($cat,true); // the category needed.
}
}
You can do it simplier:
Print current category:
single_cat_title(); // this prints your current category
Get current category string:
single_cat_title('', false); // this returns your current category
echo single_cat_title('', false); // for print current category
This is what i print out.
$args = array(
'category' => 'Animation',
'numberposts' => 8
);
$posts_array = get_posts( $args );
echo json_encode($posts_array);
this is the result: (there are 8 identical JSON lists like this, just showing 1 for convenience.)
{"ID":554,"post_author":"1","post_date":"2012-12-28 19:17:43","post_date_gmt":"2012-12-28 19:17:43","post_content":"The race to space is on. As nations compete, we follow the progress of a single chimpanzee that's been recruited into the Space Program. His results might prove influential for the better of all mankind. ...will he be up for the challenge","post_title":"Alpha","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"alpha","to_ping":"","pinged":"","post_modified":"2012-12-28 19:17:43","post_modified_gmt":"2012-12-28 19:17:43","post_content_filtered":"","post_parent":0,"guid":"http://localhost/Wordpress%203.4.2/?p=554","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"
but i just want to send the id and the post_content. i keep on getting null values and cant figure out why.
Just filter by the fields you need: (id and the post_content or title and permalink)
$args = array(
'category' => 'Animation',
'numberposts' => 8
);
$posts_array = get_posts($args);
$send_array = array();
foreach ($posts_array as $key => $value)
{
$send_array[$key]["ID"] = $value->ID;
$send_array[$key]["post_content"] = $value->post_content;
}
echo json_encode($send_array);
exit;