Returning number of posts in Wordpress - wordpress

I need a function to calculate the number of posts in a Wordpress blog that is aware if you are looking at a category, a given tag or the whole blog.
I'm keen to avoid rewriting for every different circumstance and want to make sure I get off on a reliable path. Relatively new to Wordpress any help appreciated.
Thanks

If you want to retrieve a count of all the posts in a blog, use wp_count_posts(). To get post counts from a specific category, do a count() on a call to get_posts() with the category ID specified as a parameter.
Example:
<?php
$posts = get_posts('category=1');
$count = count($posts);
echo $count;
?>
Unfortunately WordPress' wp_count_posts() function won't count a category's posts. It'll only count different post types, i.e posts, pages, drafts, and in 3.0, custom post types.

Provides a template function: WordPress › Count Posts « WordPress Plugins. Could borrow the code from it and integrate it into your theme's functions.php file.

Related

Wordpress | Design an entire category (Not category page)

I'm creating my first wordpress theme, and I've looked around on google, and the wordpress codex for an answer to my question, but I can't seem to find exactly what I'm looking for, or couldn't get it working.
What I'm trying to do, or trying to figure out, is how I can make it so a certain category has a certain design.
So if I wanted to make an index.php for any music videos in "www.domain.com/music/trash/drake-song.mp4.html"
the trash category, it'd have its own design, but songs in
"www.domain.com/music/good-music/coldplay-viva-la-vida.mp4.html"
the good-music category, I want it to look pretty much completely different. I've tried using something similar inside my header.php to this;
<?php
if( is_tag( 'good-music' ) ):
$my_classes = array( 'good-class', 'good-class-two' );
else:
$my_classes = array( 'not-good-class' );
endif;_
?>
but it seemed to simply change the category page.
"www.domain.com/categories/good-music"
Anyone know what I could be doing wrong? I know basic html/css/php/javascript, new to creating a WordPress theme, and can't seem to get this working..
Also:
Using XAMPP to host locally, using Friendly URL's, properly configured
In order to generate category-specific markup for a single post layout, you can put code like the below in your single.php file after the call of get_header().
Please note that this checks for your category based upon category slug. So if your category slug (the url version of your category) does not match the first arguments in the in_array() function call below, then you should change the argument to match the slug for your category.
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<?php
$categories = get_the_category();
$catSlugs = array();
foreach ($categories as $category){
$catSlugs[] = $category->slug;
}
if (in_array('good-music',$catSlugs)){
$post_cat = 'good-music';
} else {
$post_cat = 'not-good-music';
}
get_template_part( 'template-parts/post/content', $post_cat );
?>
<?php
endwhile; // End of the loop.
?>
It is important that this code only appear where there is a query to loop against.
Specifically, the file single.php in your theme should be the default file for displaying a single post, regardless of category. When you navigate to the url of a single post, this layout should be triggered.
As part of that triggering, a wordpress query of just that post is returned to be looped through.
Then, the code from while ( have_posts() ) : the_post(); until endwhile; will run one time, because there is a single post to be processed.
If there were more than one post, such as on a category page or on your default post listing page, then the code inside of that while loop would run as many times as there are posts in the query for that layout.
If you were to place the code in the header, it won't work because the header is prepared independently of the loop that runs on this page.
You could run a custom WP_query() in the header, but that is rarely a good way to handle site content.
In this situation it would not be appropriate, because you are customizing content of existing posts, and only differentiating based upon category.
So, just use the standard layouts files with custom layout parts.
I stripped out the divs in the loop below, because you may or may not be using bootstrap.
After this code is placed on your page, you would create files called content-good-music.php and content-not-good-music.php in YOUR_THEME_DIR/template-parts/post/ directory. The key is that you would add whatever your category slug is to the end of your
These files will contain the unique markup for these kinds of posts. You can also use a similar logic in your post listing loop to give the listed posts for each category their own unique php files.
Here's some get_template_part() documentation.
https://developer.wordpress.org/reference/functions/get_template_part/

Wordpress Category.php not showing all posts in that cateogry

I am having an issue with my category showing all post from that category. My code is here: Category Loop
I have it set up to show my posts in 3 columns. You can see it in action here. There are like 48 posts that should show up, but for some reason I only get 10.
Any suggestions?
10 is the default value of the query variable posts_per_page. You use have_posts() and the internal Wordpress mechanisms to retrieve posts, hence you are limited by this number. You can change it like this:
query_posts( 'posts_per_page=50' );
See this page: http://codex.wordpress.org/Function_Reference/query_posts

How to list all items in custom taxonomy with a permalink in WordPress

Not sure how to word this. I've set up a custom post type in WordPress called Products. I also have two taxonomies linked to Products called Brands and Categories. When you create a product you can set to which Brand and Category the product belongs. It works perfectly when using the type as permalink to list everything within a type
http://sitename.com/products
but I can't seem to get the taxonomies working in the same way, ie. list all the items within a taxonomy (like an archive, I suppose):
http://sitename.com/brands
http://sitename.com/categories
Is this possible, and if so, what could I possibly be doing wrong? I would try and set up some rewrite rules in functions.php, but I don't know what the query string looks like if you want to list items in a taxonomy, and if this is even possible. If it helps, I'm using the plugins More Types and Ultimate Taxonomy Manager to create the types and taxonomies.
There's not really a way to do this yet. WP is moving this direction and with 3.1 is setting up a top-level for custom post types, but still nothing for taxonomies. It's klunky but I added a page called page-products.php and then put my list in there.
$toplevelterms = get_terms($taxonomyname, 'hide_empty=0&hierarchical=0&parent=0');
$termchildren = array();
foreach ($toplevelterms as $toplevelterm) {
$termchildren[] = $toplevelterm->term_id;
}
foreach ($termchildren as $child) {
$term = get_term_by('id', $child, $taxonomyname);
$write .= '<li>';
$write .= '' . $term->name . '';
if ($term->description) $write .= apply_filters('the_content', $term->description);
$write .= '</li>';
}
if ($write) echo '<ul class="clearfix">' . $write . '</ul>';
FYI the correct link for that plugin (I'm the author) is now http://www.get10up.com/plugins/simple-custom-post-type-archives-wordpress/
And it should really be done using the built in archives feature in 3.1 - the plugin was built for 3.0.
There is a Trac ticket for this, but it's not clear what would the most obvious thing to show on this page:
a list of all posts attached to any term of that taxonomy, or
a list of all terms of this taxonomy?
A similar question on the WordPress Stack Exchange asked for option 2, and your question can be interpreted both ways: when you say all items within a taxonomy, do you mean all terms of this taxonomy, or all posts that are linked to a term of this taxonomy?
Currently the main query (the Loop) that you can control with the query vars will always return posts, not terms. As said in the ticket, option 2 would require the Loop to return terms for this query, possibly causing compatibility issues for themes that don't have a template for it. Until that is implemented, David's answer is probably the way to go.
Thanks for the answers! I've found a plugin that does exactly what I wanted - after manually coding everything :/
http://www.thinkoomph.com/plugins-modules/wordpress-custom-post-type-archives/
Use the WP htaccess Control Plugin
http://wordpress.org/extend/plugins/wp-htaccess-control/
In the options , you can remove the base of taxonomies you select.
Worked for me, hope It works for you as well :)

WordPress--post only latest 1 or 2 or 3 posts on homepage

How do I just display the latest post on home (not the whole archive as wp does by default)?
And how can I influence that it has/not date, category, read more, etc?
WP manual does not explain this clearly. Or does it? If yes, please tell me where?
Thank you guys! And girls!
The new Wordpress Themes have child themes, it is a new way to organize and simplify upgrading themes, but it can be a pain because they are not as easy to modify as older themes. Anyway, there is the old way:
You can use the settings in the Administration Page to set your homepage to display only one post (Settings / Reading / Blog pages show at most -> 1 post). But if you want just your home to display one post, and decide you want your archives to display the basic 10 posts, you can edit your theme template to use this function:
<?php query_posts('posts_per_page=1'); ?>
Function Reference/query posts
http://codex.wordpress.org/Function_Reference/query_posts
To be sure no date or category is displayed, you also have to edit the template and remove the corresponding php code (in the loop, you should look for these functions:
<?php the_category(); ?>
<?php the_date(); ?>
Function Reference/the category
http://codex.wordpress.org/Function_Reference/the_category
Function Reference/the date
http://codex.wordpress.org/Function_Reference/the_date
To use the new way, you have to create a child theme to your theme, and override the loop by creating your own loop.
Chld Themes
http://codex.wordpress.org/Child_Themes

Find post categories or tags when editing a post

I'm trying to figure out how to find a post category or tag when editing a post in wp-admin.
I need this because I want to show some modules in the editor only for a post of a certain category.
Is there a way to accomplish that result?
You can use the get_the_category() function to get the categories of the current post. From the codex:
global $post;
$categories = get_the_category($post->ID);
var_dump($categories);

Resources