Displaying a single term of a custom taxonomy in Wordpress - wordpress

How am I able to show a single term of a custom taxonomy in Wordpress?
I have a custom taxonomy, which will always have only one term in it, and I want to display it in a custom loop on my home page. I have tried all possible ways, with one (blargh, can't remember which one) I managed to display the term but as a link, which I don't want. I need it as regular text, since I want to display it in my own href, which will link to the place where I want to.
Any ideas anyone? All and any help very much appreciated. This issue is slowing down my development of a custom theme. :(

Get the single term object using get_term($term_ID, $taxonomy), or alternatively get_term_by('slug', $term_slug, $taxonomy).
The term object will have, among other properties;
$term->name; // the name of the term!
$term->slug;
$term->term_id;
$term->count; // number of objects using the term

Related

Pull Custom Taxonomies ACF Fields to frondend

Hey there awesome Stackoverflow peeps,
I have assigned colors to a custom taxonomy using ACF and I am trying to pull the colors of that tag to the post that I assign them to but I am not sure how to go about it. Can someone share the secret sauce?
Regards
It sounds like you're looking for the ACF get_field function. Documentation on that here: https://www.advancedcustomfields.com/resources/get_field/.
The second parameter is what you'll need to pay attention to. Sounds from the question like this field is on a term of a custom taxonomy? In which case the easiest way is to pass the whole term object there like:
get_field( 'color', $term );
You can use get_field to return the value or the_field to echo it.

Page Template for every custom post type

I'm just new in wordpress, I just wanted to ask if it's a bad practice if I'll create page template for every custom post types?
I mean if I have CPT [custom post type] with different content, like my first CPT has images, my 2nd cpt has image and text, my last CPT, has slider, text and description.
Because I wanted to create page template for every type of CPT's I have. Is it a bad practice? Or are there efficient and effective ways to do such things?
Your answers are highly appreciated. Thanks!
It's better not to do that, not only because it's a server load, but also because you'll get crazy if you have an issue. Instead, use conditionals in the same page. You can read about it at WP Codex: Conditional Tags : Taxonomies and then pay attention to the is_tax and has_term tags.
This way, you can use is_tag or has_term depending on your approach, like this
if( has_term( 'jazz', 'genre' ) ) {
// do something
}
You can use taxonomy to deal with such problem. Use taxonomy to filter contents according to your requirements. Use register_taxonomy() to register a taxonomy for CPT.

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

Drupal - Disable listing of nodes on taxonomy term page?

Is it possible to disable the normal taxonomy listing of nodes on taxonomy term pages?
The reason I need this is I want to use a view's override of taxonomy pages BUT the default views override stops a breadcrumb module working properly. So, I want to make a term view but as a block and show it on certain pages with PHP.
Thanks
Another way of doing this is using the Display Suite and Taxonomy Display module. Install them, then go to admin/structure/taxonomy/[mytaxonomy]/display.
Under "Use custom display settings for the following view modes" select "Taxonomy term page".
Then, in the "Taxonomy term page" view mode, under Term page display, select "Associated content display": HIDDEN.
Done! :)
This module claims to do just what you are seeking, but it did not seem to work despite checking the correct taxonomy to disable:
http://drupal.org/project/disable_term_node_listings
But putting the following in your theme's template.php will suppress those node listings:
function MY_THEME_preprocess_page(&$variables) {
if(arg(0) == "taxonomy" && arg(1) == "term") {
$variables['page']['content']['system_main']['nodes'] = null;
}
}
It's sort of a dirty way to do it, and you'll have to hide the pager using CSS, but it works.
This probably isn't the cleanest way but I've made a page-taxonomy.tpl.php and removed this:<?php print $content; ?> So far it seems this solution will work for my site, but I'd still like to know the proper way to do it.
If all you want to do is override the taxonomy term pages with a View, but NOT use the default view, you could create a custom module implementing hook_menu() or you could also take a look at the Taxonomy Redirect module.
From the Taxonomy Redirect page:
This module allows the administrator to change the destination of Taxonomy Term links.

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

Resources