How can I output the count of a specific taxonomy? - wordpress

I am trying to output the count of product categories that have been created for the custom post type product_cat.
When I use:
<?php
$count_posts = wp_count_posts('product_cat');
echo $count_posts->publish;
?>
it outputs "0" although there have been 3 categories created. I have searched through all of he plugin files to see how the count is output on the edit product categories page in the Wordpress admin interface. I normally refrain from asking questions due to the fact that I am new and self taught so forgive me if my terminology is not 100% correct.

Use wp_count_terms:
<?php
$product_cat_count = wp_count_terms( 'product_cat' );
echo $product_cat_count;
?>

Related

Wordpress Custom Taxonomy Template

found some help on here earlier today and hoping to do so again about creating a custom template. :-)
I have a Wordpress custom post type set up in my functions.php
I have a custom taxonomy called "Classifications"
Under that taxonomy I have the terms/categories:
Old Research (parent)
--- sub cat 1
--- sub cat 2
I have created the file "taxonomy-Classifications-oldresearch.php" which successfully changes the layout for me. However, it does not hold the layout in the sub categories. I can alter it by also creating "taxonomy-Classifications-subcat1-oldresearch.php" but I will have a lot of sub categories that I want to use one template for. This way I don't have to create a new template file for every new category. Does anyone have a good solution for me?
Thank you!
There is a great post on the Wordpress stack exchange that addresses exactly this issue. The problem is that Wordpress uses a template hierarchy to load php templates based on the slugs of each term.
You can do some custom coding inside your taxonomy template to show proper sub categories as you want. For example, let suppose your current parent taxonomy is oldresearch then you will be taken to specific taxonomy template as WordPress does, in your case you will be taken to oldresearch template page. Inside that template you can get id of your current term and then can get children data from that category. In code it will look something like shown below:
Note: this below code is for reference you can use it according to your needs.
<?php
$term_id = 10;
$taxonomy_name = 'Classifications';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
In this way you can play with your sub-categories data. $term_id is just for reference purposes, you will be getting id of your parent taxonomy term and so will be getting sub categories from that term.
For further reading check Get Children terms Codex

WordPress default number of results returned

For some reason this query is only returning the 10 most recently published results, there should be at least 15 or so. So my question is, if I don't specify the number of results in my query, will WordPress default to 10 or something?
<?php getGallery('test', 'test2'); ?>
function getGallery($galleryLink, $altLink){
global $post;
// search for any pages with a custom field of 'test'
query_posts('meta_key='.$galleryLink.'&post_type=page');
if (have_posts()) while (have_posts()) : the_post();
$link = get_post_meta($post->ID, $galleryLink, true);
$alt = get_post_meta($post->ID, $altLink, true);
$permalink = get_permalink($id);?>
<a href='<?php echo $permalink ?>'><img src="<?php echo $link ?>" alt="<?php echo $alt ?>"/></a>
<?php endwhile;
wp_reset_query();
}
default number of returned posts in $wp_query is defined in Dashboard > Settings > Reading. There you have defined 10, but remember that other people can set diffrent number on their blogs.
i hope you know that if you want to define your own number of posts you should add posts_per_page=X to query :) if you want to always return all posts matched to query put -1 in place of X
You should be able to modify the default value of 10 to any number you specify with the posts_per_page parameter in the query, like so:
query_posts('meta_key='.$galleryLink.'&post_type=page&posts_per_page=20');
Sub in any value you like instead of 20. Better yet, use a variable and stay away from literals.
Yes. The standard Loop query shows the number of posts defined in your Reading Settings page in WordPress, which defaults to 10.
See the examples under "Pagination Parameters" in the WP_Query documentation if you want to alter this programatically, rather than relying on the setting. For example:
$query = new WP_Query( 'posts_per_page=-1' );
...will retrieve all posts.
If this is your default loop, just go to Settings --> Reading and change "Blog pages show at most" to your desired number. If you specify a number that is less than your total amount of posts you'll have to include some sort of navigation to allow the visitor to go previous posts.

How do you display a random post from your database of content in a wordpress 3.0 theme?

I am developing a webpage at http://www.knowledgenation.us and currently I have roughly 500 posts on the page. I believe that is far too many posts to expect someone to read through but I do believe that my page has return value. I want people to return to the webpage on a regular basis and always get something new from the site.
That being said, I would like to post three random posts from my database to the body of the web page that is the theme that I have. I would also like to know how to make that code modular so that I can reuse it for a new incarnation of this website that is going to pull in content from RSS feeds from two websites that friends of mine are developing.
That all being said, bottom line, how do you post random posts to a website, what would the code look like and please be kind in explaining because I am quite new to PHP programming and would not understand most of what the code is about. I just recently got a http://www.lynda.com account and am going to be learning all about PHP but for right now I understand little.
I thank you in advance for helping me with this.
When you query your posts, you can pass on query attributes such as a category, included/excluded post ids, limits and offsets, etc.. You can also define how your results will be ordered — by which field and into which direction (ASC/DESC).
The order_by parameter can be a regular field names like title or date, and also rand as in random to fetch random posts.
Here's an example to use outside the loop, fetching five random posts:
<ul>
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
And another example for a regular loop:
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
query_posts($args);
while (have_posts()) : the_post();
the_content('Read the full post »');
// And so forth…
endwhile;
?>
Hope you get the picture…

excluding posts in Wordpress

I wondered how I could exclude posts in Wordpress.
E.g. I have a string
$exclude_ids (= "4,5,6") or (="-4,-5,-6")
and I would like to prevent these posts from showing up. How would I do that?
I already tried:
query_posts('p=' . $exclude_ids);
but that didn't really work out and I didn't really find any information regarding this topic on google.
Cheers
Here's the relevant info from the docs:
'post__not_in' => array(6,2,8) -
exclusion, lets you specify the post
IDs NOT to retrieve
It's in the Codex: http://codex.wordpress.org/Function_Reference/query_posts
use the post__not_in, something like: query_posts(array('post__not_in'=>'1,2,3'))
The ideal solution would be to create a category, add those posts to it, then exclude the category. But if you really want to single out posts, it could be done as follows:
<?php if (have_posts()) :
while (have_posts()) : the_post();
if ($post->ID == '179' || $post->ID == '180' || $post->ID == '181') continue;?>
<?php the_content();?>
<?php endwhile;endif;?>
Just use that if statement in your Loop. The continue will skip over that iteration for any of the listed posts.
Source: http://www.sandboxdev.com/blog/wordpress/180/exclude-single-post/

WordPress custom query pagination

I have a WordPress site, where on the main page I list the content from more categories.
My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?
Best Regards,
If you're using the standard Wordpress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?
Edit 11/20: I use this in several different places on one page to show the latest post in a category:
<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex
I don't know how to paginate different categories on the same page. Must be possible. Maybe ask in the Wordpress forums.
This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you'll even need to rely on a plugin. :)
I'm going to assume that you're familiar with the query_posts() function, so let's go ahead and use this example as a base:
// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we'll want to use the [offset] parameter of query_posts():
// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I'm assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.
With something like that, it looks like you'll have to work a bit with Javascript to do the work for you, along with what I illustrated above.
I believe you could do something like this:
<?php
if(isset($_GET['paged'])){
$page = $_GET['paged']-1;
}else{
$page = 0;
}
$postsPerPage = 5;
$theOffset = $page*$postsPerPage;
?>
<?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>
Hope this will help you :)
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $page,
);
query_posts($args);?>
?>

Resources