Find post categories or tags when editing a post - wordpress

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);

Related

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

how can i add comments to to a custom page

I am trying to display the comments of a post in a custom page in wordpress. I want to display all the comments including sub-comments. There is any way to display the comments of corresponding post?the_comments() shows all the comments in the database. Can I use get_the_commets($args)?
get_comments() would do the trick for you.
$comments = get_comments(array('post_id'=>YOUR_ID));
will return you an array of comments for particular post.
You can check get_comments page for more options

How to display wordpress post according to category in pages

I'm working on a site.The problem is i have to display the categorized posts in their respective pages. I'm using a custom theme that I'm working on. basically I'm making a site in WordPress. Links to other helpful tutorials on making will be awesome.
This is the code you need to add in your webpage. Replace 1 with the category id you want.
<?php
$posts = get_posts(array('category' => 1));
?>
You can refer this link for more detailed info.
Edit: More detailed info.
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
Now $category is you array of category. you can get the category name by
$category[$i]->cat_name So, you can search your category in array and get it's "id". and from first code you can get 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 :)

Returning number of posts in 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.

Resources