Get Category in Wordpress Post - php

OK I have a standard post here:
http://www.ticketyoda.co.uk/concerts/rihanna-tickets/rihanna-tickets-manchester-2013-06-12-united-kingdom/970
I am trying to make each post unique and contain the category information for both the city (Manchester) and the Artist (Rihanna).
At the moment the box underneath the venue contains the information taken from the category (see breadcrumb). So I have 1 out of 2. What I need now is for an extra box below to show the category information for the Artist.
I use :
<?php $category = get_the_category(); echo $category[0]->category_description; ?>
to get the first category.
You can see the rihanna description under the posts here:
http://www.ticketyoda.co.uk/concerts/rihanna-tickets/
Any help appreciated.

I think the reason you are only getting one category is due to your echo statement. It only calls for 1 value $category[0], in order to get a second category try [1]. Out of curiosity is the second category a child of the first?

Related

How to display ID of each category in Woocommerce shop page with "Show Categories" selected

So I have my shop page set to list all categories (not products). I've been using hooks to alter the display, hide/show elements, etc.
How can I get the ID of each category in the list within that category's display? WC is obviously pulling data through the WC loop, as it displays the thumbnail and title of each category, gets the url for the category, etc. How can I grab the ID and display it, say, next to the title for example?
I need to know how to get the ID so that I can use that to display the value of an ACF field for each category in the list display. I assume the id will look something like product_cat_<id-num> or something.
Please help. Deadline is drawing near. When I Google anything related to displaying the categories on the shop page, it just gives me a bunch of links like the linked article above, which have nothing about how to extract category info from this loop...
The issue was that I was not passing $category to my function.
This is how you would show a custom field below a title on shop page where categories are shown:
add_action( 'woocommerce_shop_loop_subcategory_title', 'end_shop_loop_title_wrap', 15 );
// pass $category to function - this is what i was missing
function end_shop_loop_title_wrap($category) {
if( is_shop() ) {
if ( $category ) {
$term_id = 'product_cat_' . $category->term_id;
echo the_field( 'custom_field_name', $term_id );
}
}
}
I found the answer to my issue buried in Google on a Stack Exchange question that I couldn't find again if i tried (which is why i bookmarked it).
Thanks to Alex Uleberg for the answer that I found.
Why is there not more robust documentation/info/explanations for Woocommerce?

Wordpress - add first post title to category title, also on subpages

I wonder that apparently noone has asked this before and after searching, checking and "cowboy coding" for hours I´m nearly ready to give up...
The (WP-)Problem: When you have a category with lots of posts and many category subpages, you normally have the category name in <title> and (on subpages) also a unique identifier appended (like "Page 2 of 99"). For SEO purposes and readability it would be nicer to add something more "descriptive". My Idea: Why not adding first post title to category <title> on every (sub-)page? Just like
<title> category 1 My funny first post</title>
<title> category 1 (subpage 1) My other first post on the second site</title>
The Question: How can I get the category title and every first post title from subpages to interconnect and to get unique results in the end?
Ideally this should happen in functions.php or custom plugin.
It is very simple.You have just to find the template of category post and add the following code with category title:
<?php echo get_cat_name( $cat_id ) ?>
Here $cat_id is your category id which you want the name.
You have to get the parent category id and then get only one post of that category and set order by asc/desc post id (as you want latest or oldest).In this way You always find the parent category first post data and you pick whatever you want.In your case post title will be useful.

Incorrect Category Name showing on WP single post

I am editing a WP site built years ago for my client by another developer, who we aren't in touch with.
On the single post pages, there is a heading that displays the category name. For the posts of one of the categories, it is showing the name of the parent category, not the subcategory , for some reason. There are two categories with the same parents ("News" and "Events") – "Events" displays correctly, "News" does not.
The code to display the category name looks like this:
<h2 class="section_header">
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
</h2>
Here is an example.
As you can see in the address bar, this post is categorized under "news", yet it is displaying the parent category ("happenings") in the heading.
As a comparison, you can look at this post which is in the category "events", whose parent is also "happenings", yet it displays the correct category title.
When checking off which categories to file a post under, it is unnecessary to check off the parent. Un-checking the parent category from the post makes just the subcategory name display.

How do I automatically put categories and their descriptions on a WP Page?

I am using a blog to keep track of citations I may want to use for a thesis. Each citation is a Post, authors are categories, which are parents to subcategories that are named for the titles of books or articles by that author. In the description of the book/article subcategories, I put the publishing information, e.g. “Penguin: London, 2007”.
Let’s assume that a particular author category is called “Mill, John Stuart” (for whom there are four citations), the book subcategory “On Liberty” (for which there are two citations), and the description “Bantam Classic: London, 1993”. Now, I would like to add a Page that shows all the sources that I have gathered, automatically outputting HTML code like this:
<p class="lit-author">Mill, John Stuart (4)</p>
<p class="lit-work"><i>On Liberty<i>. Bantam Classic: London, 1993. (2)</p>
This might just as well be done with <ul>s, I’m really only interested in being able to format the output using CSS and having in on a Page. (It’s pretty much just the contents of a Categories widget plus the category description, but on a Page.)
How would I do this, and where would I have to put the relevant code so that it would be displayed on a specific Page?
(WP 3.2.1, Graphene 1.4.1)
You can create a copy of category.php and name it category-slug.php where "slug" is the category slug set in the wordpress admin. This way, you can customize the page for each specific category. WordPress will display posts for that category when you visit the page.
http://codex.wordpress.org/Category_Templates
Now, in reference to the title of your question "How do I automatically put categories and their descriptions on a WP Page?" To display a category description on a page, you can use the following code:
<?php
//store the category id by the slug for any page
$cat_id = get_category_by_slug('slug')->term_id
//displays the category name
echo get_cat_name( $cat_id );
//displays the category description
echo category_description( $cat_id );
?>

Wordpress: Don't show the first inserted post

So, I have some subcategories, and in each category I insert some posts, when I show the posts for each subcategory I don't want to show the FIRST inserted post in that subcategory, only the posts that are not the FIRST ONE.How can I do this with query_posts..or how?
Best Regards
//SOLVED I will use a custom field
See offset parameter in function reference
query_posts('posts_per_page=5&offset=1');

Categories