get_* functions are return functions. They don't actually print anything. Given your code example, all you should need is an echo statement:
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>">← Back to Portfolio</a>
ALTERNATIVE: The best way of doing this would be to keep everything in your loop for reuse in single or archive views, combined with the use of wp_get_post_categories. This is untested, but it should get you started with a general-use method of listing categories associated with a given post or set of posts:
<?php
if(have_posts()) : while(have_posts()) : the_post();
$cats = wp_get_post_categories($post->ID);
if($cats) : foreach($cats as $cat) : $category = get_category($cat);
?>
<a class="button" href="<?php echo get_category_link($category->cat_ID); ?>">← <?php echo $category->name ?></a>
<?php
endforeach;endif;
endwhile;endif;
?>
Answer from maiorano84 on Stack Overflowget_* functions are return functions. They don't actually print anything. Given your code example, all you should need is an echo statement:
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>">← Back to Portfolio</a>
ALTERNATIVE: The best way of doing this would be to keep everything in your loop for reuse in single or archive views, combined with the use of wp_get_post_categories. This is untested, but it should get you started with a general-use method of listing categories associated with a given post or set of posts:
<?php
if(have_posts()) : while(have_posts()) : the_post();
$cats = wp_get_post_categories($post->ID);
if($cats) : foreach($cats as $cat) : $category = get_category($cat);
?>
<a class="button" href="<?php echo get_category_link($category->cat_ID); ?>">← <?php echo $category->name ?></a>
<?php
endforeach;endif;
endwhile;endif;
?>
Take a look on the link get_the_category().
global $post;
$category = get_the_category($post->ID);
if($category){
echo '<a href="'.get_category_link($category->term_id).'">'.$category->cat_name.'</a>';
}
Videos
Sounds like you may want get_category_link - something like:
$categories = get_categories();
foreach ($categories as $cat) {
$category_link = get_category_link($cat->cat_ID);
echo '<a href="' . esc_url($category_link) . '" title="' . esc_attr($cat->name) . '">' . esc_html($cat->name) . '</a>';
}
should print out the links to the categories for you.
According to Function Reference/get category link
<?php get_category_link( $category_id ); ?>
Example:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
<!-- Print a link to this category -->
<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
One these three should do the job for you...
1. Function: the_category();
News su <?php the_category(', '); ?>
Displays as:
News su WordPress, Computers, Blogging
And if only a single category is assigned to a post, it shows up like this:
News su WordPress
2. Function: get_the_category_list();
<div id="pagine"><?php echo get_the_category_list(); ?></div>
Displays as:
<div id="pagine">
<ul class="post-categories">
<li>
<a href="http://example.com/category/wordpress/" title="View all posts in Business" rel="category tag">WordPress</a>
</li>
<li>
<a href="http://example.com/category/computers/" title="View all posts in Business" rel="category tag">Computers</a>
</li>
</ul>
</div>
And if only a single category is assigned to a post, the output would be like this:
<div id="pagine">
<ul class="post-categories">
<li>
<a href="http://example.com/category/wordpress/" title="View all posts in Business" rel="category tag">WordPress</a>
</li>
</ul>
</div>
3. Function: single_cat_title();
If you want to show just one category (category with the lowest ID will be shown) no matter how many categories are assigned to a post, use something like this:
<div id="pagine">
<ul>
<li>
<?php
$category = get_the_category();
echo '<a href="'.get_category_link($category[0]->cat_ID).'">News su ' . $category[0]->cat_name . '</a>';
?>
</li>
</ul>
</div>
The above code always shows one category, like this:
News su WordPress
So, given the codes (and what each does), suit them to your needs.
get_the_category returns an array of the stdClass object of all categories associated with the current post_id.
$category[0] will give you the first category in the array. To see the mapping do.
<?php print_r( $category ); ?>
If you want to do this on post page you can add something like the following to your single.php file of your theme.
<div class="meta">Posted in: <span><?php the_category(', ') ?> </span></div>
Here's some info that will be of use:
http://codex.wordpress.org/Template_Tags/wp_list_categories
Basically you can call: <?php wp_list_categories( $args ); ?> and this will output what you're looking for.
Thr $args parameter is an array of settings strings that lets you change the order, style, depth etc, on links returned.
Use:
get_category_link( $category_id );
See:
https://codex.wordpress.org/Function_Reference/get_category_link
In your specific case:
<?php
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
$exclude = get_the_ID();
$posts = get_posts('posts_per_page=4&category='. $category->term_id);
foreach($posts as $post) :
if( $exclude != get_the_ID() ) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post c-1"> Link to actual post</a>
<?php } endforeach; ?>
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" title="View all" class="btn border"><i class="i-right-double-arrow"></i> View all <?php echo $category->name; ?></a>
<?php endforeach; wp_reset_postdata(); ?>
A STRAIGHT FORWARD AND CLEAN CODE
I'm a newbie :)
I modified the codes given by Adam and removed the unnecessary parts not needed to answer the initial question.
It worked for me 100%.
Give it a try.
Please let me know if it worked for you too :)
<?php $categories = get_the_category();
foreach ($categories as $category) :
endforeach; ?>
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
LINK TO CURRENT POST CATEGORY >>
</a>
OR
<?php $categories = get_the_category();
foreach ($categories as $category) : ?>
The category is:
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
<?php echo $category->name; ?>
</a>
<?php endforeach; ?>
COMBINE SOME OTHER ELEMENTS AS YOU LIKE. PLAY AROUND BUDDY.
You can use get_queried_object(), which will return category object.
See documentation:
https://codex.wordpress.org/Function_Reference/get_queried_object
Put your $c term (in your loop) in a print_r/var_dump to see if the property you are trying to compare the values of is of the actual value that you are looking for (See below).
I've also added a ternary operator in the place of your if statement, it is just better practice. (Read below for more on ternary operators).
<menu id="nav">
<ul>
<?php $cat_id = get_cat_ID();
foreach( $categories as $c ):?>
<?php print_r($c); ?>
<li class="<?php echo $c->term_id == $cat_id ? 'active' : null ;} ?>">
<a href="<?php echo get_category_link( $c->term_id ); ?>" title="<?php echo $c->cat_name ;?>">
<?php echo $c->cat_name ;?>
</a>
</li>
<?php endforeach; ?>
</ul>
</menu>
Ternary Operator
In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.
if(1==1) echo 'true';
else echo 'false';
can be done in a ternary operator like so:
echo 1==1 ? 'true' : 'false';
Another example
if(1==1) $boolean = true;
else $boolean = false;
can be done in a ternary operator like so:
$boolean = 1==1 ? true : false;
Since PHP 5.3, the 'middle part' of the operator can be left out. Shorthand ternary operators can be used to match only false
if(1!=2) echo 'false';
The ternary operator:
echo 1!=2 ?: 'false';
Read more: http://php.net/manual/en/language.operators.comparison.php
There is a function called get_category_link() which might be helpful for you. This will be able to generate an appropriate link without having to hard-code it, except for the category name or ID.
Examples from the Wordpress Codex:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
<!-- Print a link to this category -->
<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
http://codex.wordpress.org/Function_Reference/get_category_link
Specific to your demand, try this:
<li><a href="<?php echo home_url() ?>/category/Gadgets"> Gadgets</a></li>
From http://wordpress.org/support/topic/getting-category-slug-from-posts-in-the-loop:
<li class="<?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
You should be able to use:
$cats = wp_get_post_categories($post->ID);
This will be an array of the categories associated with this post. Then you can loop through them and do whatever you need.
Found the answer for something else but it also applies to my question.
add_action('woocommerce_archive_description', 'woocommerce_category_description', 2);
function woocommerce_category_description() {
if (is_product_category()) {
global $wp_query;
$cat = $wp_query->get_queried_object();
echo "CAT IS:".print_r($cat,true); // the category needed.
}
}
You can do it simplier:
Print current category:
single_cat_title(); // this prints your current category
Get current category string:
single_cat_title('', false); // this returns your current category
echo single_cat_title('', false); // for print current category