Trouble with get_the_category() + Wordpress - php

Wordpress Version 3.4.2
I am having a hard time getting the current category name to print on single.php. (outside the loop) There seems to be some sort of caching going on.
I'm trying to do something like this:
if the_category() == 'this posts category' {
echo 'something';
else do this....etc
I've tried several things and always end up with a cached category name. No matter which category the post belongs to.
Here is the snippet that I've been using:
// outside loop
$category = get_the_category();
echo the_category($category[0]->cat_ID);
Alas, it will only print the cached category name, not the category the post is actually in. Thanks for your help. It's really appreciated.

Look at using wp_get_post_categories() instead. maybe something like wp_get_post_categories($post->ID, $args)?

Related

customizing Wordpress queries using pre_get_post

I've built the following snippet from other post examples that say doing query modification any other way is punishable by great scorn, but it's not working. I'm getting results that include non-published posts and pages which clearly shouldn't happen:
function post_conditions($where)
{
$where .= "AND post_content NOT LIKE '%::exclude tag::%'";
return $where;
}
add_filter('posts_where','post_conditions');
function mysearch($query)
{
$query->set('post_type','post');
$query->set('post_status','publish');
$query->set('posts_per_page',20);
$query->set('paged',get_query_var('paged'));
}
add_action('pre_get_posts','mysearch');
while( have_posts() ){
the_post();
echo get_the_excerpt();
the_tags();
}
if (get_query_var('paged'))
my_paged_function();
wp_reset_query();
The get variables look like so: ?s=mysearchterm&submit=+GO%21+
On my blog template, I'm using the verbotten query_posts() function to achieve the same effect and it works perfectly.
I don't know what's going wrong. Any ideas?
Since this code lives in your template, it's not firing in time to catch the pre_get_posts hook. By the time the template is chosen / running, wp_query is done setting up, and pre_get_posts is over.
You need to move this functionality into your functions.php files, and try and use some other means by which to determine if you want to change the query. There's lots of information available to you - including if it's an archive, a single page, a single post, the post id, and more - hopefully with that information, you can determine if you want to modify the query.

opencart - get parent category name/id on a sub-category page? New

Okay, I've been asked to repost this as a moderator deemed it to be a 'new' question to this thread: opencart - get parent category name/id on a sub-category page
I believe it to be a valid question related to that title so have posted it here in compliance. Any help would be very much appreciated:
Being a newbie to this concept I'm struggling to interpret the logic within the original thread [above] when applying it to my particular scenario; I'm trying to display the currently displayed category's parent category name. In category.php I've included the line $categories = explode('_', $this->request->get['path']); but am having difficulty in displaying the parent category name. I'm unclear as to the correct variable name to use in category.tpl to display this. Could someone please advise? Thanks.
Find (catalog/controller/product/category.php) around line 95
if ($category_info) {
Add inside
if($category_info['parent_id'] != 0){
$this->data['parent_cat_info'] = $this->model_catalog_category->getCategory($category_info['parent_id']);
}
now in template file (/catalog/view/theme/default/template/product/category.tpl) you can access the properties of array $parent_cat_info
e.g for category name echo $parent_cat_info['name']
In page catalog/controller/product/catalog.php
find the line
if ($category_info) {
Then add below code inside above line
$data['parent_cat_info'] = array();
if($category_info['parent_id'] != 0){
$data['parent_cat_info'] = $this->model_catalog_category->getCategory($category_info['parent_id']);
}
and in page catalog/view/theme/yourtheme/template/product/category.tpl
add where you like to diaplay
<?php //print_r($parent_cat_info);
if($parent_cat_info){
echo $parent_id=$parent_cat_info['category_id'];
echo $parent_name=$parent_cat_info['name'];
}
?>
There is a way easier way to get them.
If you install the export/import tool that exports the DB into an xls.
It has the cateogory id and name associated on the category tab inside the xls.
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=17
Its very simple and elegant, best method is to simply not convolute. :)
Hope this helps everyone.
Cheers
NickTailor
nicktailor.com

Adding a title attribute to category without affecting core

My current setup includes having the category of each post. But whenever I mouse over the category, it shows the default message of Show all posts in [category].
I have looked around and have seen some pretty similar questions that require multiple lines of php code. This surely cannot be that complicated. Is there a way I can perhaps tweak the following to get it to do what I want?
<?php the_category(' '); ?>
This is the code that returns the above. I am looking through the codex and found category_description() and get_the_category(). I am kind of new to arrays and am having problems figuring them out. I was kind of hoping that something like this would work:
<?php the_category('title=category_description()'); ?>
but it is not. I know that is a pretty hilarious way of solving this to most of you but I am completely lost. I just want the description of the category to be the title attribute. Is there a simple way that I can do this?
wordpress codex
using get_the_category(); you can save all of the categories into a variable.
Ex:
$categories = get_the_categories();
There is however an optional parameter that you can pass; the category ID.
Ex:
$categories = get_the_categories(THE_CATEGORY_ID);
Whether you present a category ID or not you must run $categories through a loop.
Ex:
foreach($categories as $category){
}
Only then, will you be able to customize your title attribute without modifying the wordpress code.
Ex:
foreach($categories as $category){
echo ''.$category->cat_name.'';
}

Wordpress loop export category list to php echo

Am a bit stuck with a wordpress loop, am wondering if anyone can help.
I need to run a Wordpress loop but only get the category names/id (Either is fine) from each post and have all of those variables as one php item I can echo later in the page.
Its for a category list filter system, but I only want to show categories which have posted displayed on that page.
The loop will be dynamic as well, so I cant just hard code exclude/include, I need to echo the value of all the numbers in together.
I hope that makes sense! Anyone who has any ideas would be really cool. Thanks!
I would use the get_the_category function like so...
<?php
// before you begin the wordpress loop
$category_array = array();
?>
<?php
// from *within* the wordpress loop
foreach((get_the_category()) as $category) {
if (!in_array($category->cat_name, $category_array)) {
$category_array[] = $category->cat_name;
}
}
?>
<?php
// after the wordpress loop is finished
echo implode(",", $category_array);
?>
This code basically creates a new (empty) array so that for each category in the current page, check if you've already added that category name to the array, then if not, go ahead and add it. Then when the loop is finished, echo out a comma separated string of category names. (You can of course, change the separator if you want a comma and space ", " or any other delimiter).
The Codex article has a lot more information on other things you can do with that function. Hope that helps.
Edit: Fixed the implementation because I forgot this was going to be used on a page where you are listing many posts using the loop. (You need to initialize your array from outside the wordpress loop, then echo your results after the loop has been finished).

Get Wordpress Category from Single Post

I'm finishing up a WP theme, and I'm on the single.php template. I'm having some issues because I need to access the parent category that a post is in in order to display certain images and XML content.
Here is an example of what I'm talking about. The following is the end url of a single post:
/andrew/leaf-art-2/
/andrew/ is the category, and leaf-art-2 is the single post. When I am on the single post, I am having trouble getting single_cat_title(); to return the category that the current post is in. I am using single_cat_title(); instead of the_category(); because it displays the string value of the category which I then use to place a picture of the artist (whose category this is) on their posts. I don't have any use for the url, I just need the string with the category name.
Any good ways of doing this? I have been searching the Wordpress Codex and lots of forums, and haven't found any answers yet.
The following was my original post.
I have set up a category called "artists" which when I run single_cat_title("", false); I can get the string value of the category and then use it to search for the appropriate artist image using XML.
This works fine on the category.php template page.
The problem is that when I'm actually inside of a single post that has the "artists" category, single_cat_title(); doesn't output any information to the page, thereby keeping me from accessing the XML data.
I need to, while in the "artists" > "sample" post, be able to get from WP the category.
P.S. the above category is one of many that is using this setup, which is why I can't hardcode it.
How about get_the_category?
You can then do
$category = get_the_category();
$firstCategory = $category[0]->cat_name;
For the lazy and the learning, to put it into your theme, Rfvgyhn's full code
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name; echo $firstCategory;?>
<div class="post_category">
<?php $category = get_the_category();
$allcategory = get_the_category();
foreach ($allcategory as $category) {
?>
<a class="btn"><?php echo $category->cat_name;; ?></a>
<?php
}
?>
</div>
Get category names with permalink in single post without loop
the_category(',', '', get_the_ID())
If you want only the first one:
get_the_category(get_the_ID())[0]

Categories