Display Link to Category in Wordpress? - wordpress

I am trying to figure out how to display the category of an article, and a link to the category. Any help would be greatly appreciated.

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.

Note that: <?php the_category(', ') ?>will display the category as a link. which is good.... but if you want only the category URL (that is, the category link only), then you will have to use the <?php get_category_link($category_ID); ?> the $category_ID is required. once you fix that in, the category URL will be returned.
Consider the 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 -->
Category Name
Now you can see how we got the category ID and then using it to get the category Link. Hope this answers your question well enough?

You can use get_the_category()
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>

Related

Add title attibute to categories

In single.php I use <?php the_category(', '); ?>. This function lists categories attached to post, but title attribute (on hover) is missing. How can I add this? I've tried with adding a filter in functions.php and making a new function like <?php the_better_category('%cat% - my text'); ?>, but the result is miserable.
From the WordPress Codex:
<?php single_cat_title( '', true ); ?>
The first part (in single quotes) will output whatever custom text you put there before the category title and true means it will display (false is use in PHP).
Have you tried:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>

How to Display post from specific tag on top of category page in wordpress

I want to create a div, like an editor choice zone, that display the post tagged "editor-choice" on the top of each category page. Of course it will only display post from current category page. Thanks
If some one is interested, here is the code:
<?php
if (is_category( )) {
// this grabs the current category slug
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
// this is the query to sort the tag only with the CURRENT CATEGORY by slug
$args = 'tag=editor-choice&category_name='.$yourcat->slug.'&category_title='.single_cat_title( '', false );
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
wp_reset_query();
?>

WordPress - Display Custom Field on Post - Pull Post Title and URL

Okay, I have the code pulling the custom fields URL and title of the url. Now I can't seem to get it to show the second featured blog. Here is the working code.
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
Here is a screenshot showing my Custom Fields if it helps at all, and the results they are showing on the site. screenshot
Example 1: You are using the actual word 'TITLE' for the link use <?php the_title() ?> instead
Example 2: You are not building link at all. Your href attribute is empty. Cut echo $featured_blog1 and paste it to href attribute to end up like so:
Example 3: Same as 2
Also you can delete dose instructions or put them inside <?php ?> code so they are not visible to viewers.
Hope this helps.
If you need more info just ask. ;)

Wordpress query_post with variable category

I'm trying to put my blog content into bootstrap tabs, where each tab is a category so it is kind of a filter.
Everything is working smoothly but I can't separate dynamically the categories posts on the tabs.
Here is my first code, where I make the tabs getting the current categories: (all good here)
<ul id="catstab" class="nav nav-tabs nav-stacked cat_list">
<?php $categories = get_categories(); {
foreach($categories as $category){
echo "<li><a href='#" . $category->slug . "' data-toggle='tab'>" . $category->name . "</a></li>";
} // end foreach
}
?>
</ul>
Then I make the containers for each tab with a loop inside each one: (all good here too)
<div id="cattabscontent" class="tab-content">
<?php foreach (get_categories(array('hide_empty'=>false)) as $category)
{
echo '<div class="tab-pane fade" id="' . $category->slug . '">';
echo get_template_part("loop");
echo '</div>';
} ?>
</div>
Finally this is the query_post from my loop, and this is where I have the problem, what I need here is to give the category variable ($current_cat) the value of the current category, so it will get the correct posts on every tab.
<?php query_posts(array(
'category_name' => '$current_cat',
));
?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
. . .
I have thought to give the variable the value of the ID of his parent, but I think it is not possible using only PHP, and I have no idea of how to make it using AJAX or something like that.
Any idea?
Don't use single quotes around variable names, use double quotes. Single quotes will be interpreted literally not expanded to the value of the variable, so echo '$var'; will give you $var exactly. Your code should either use double quotes or no quotes when setting the category_name argument, assuming that $current_cat is equal to the name you want to query on.
double quotes
'category_name' => "$current_cat",
no quotes
'category_name' => $current_cat,
Try to use category__in parameter like this:
<?php query_posts(array(
'category__in' => '$current_cat_id',
));
?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
Parameter category__in display posts that have only this category and not children of that category. You will have to use category ID in this case.
Parameter you are using category_name on the other hand displays posts that have this category and any children of that category.

How to show subcategory name of a post in wordpress?

I have Main Category - Sub Category 1 - Sub Category 2 - Child Category .
If i click the child category , using the following script i can sort the post.
<?php if (have_posts()) : $i = 0; while (have_posts()) : $i++; the_post(); ?>
To show the category name i use ,
$category = get_the_category();$category->cat_name;
But i can't show the sub category and child category so please help me!.
This will print only sub category and not a parent
<?php
$cat_name = 'category';
$categories = get_the_terms( $post->ID, $cat_name );
foreach($categories as $category) {
if($category->parent){
echo $category->name;
}
}
?>
Check out settings for this in the wordpress codex here
http://codex.wordpress.org/Function_Reference/get_categories
Right now you are only calling for the main categories but not the children, this should help depending on what exactly you are trying to do
<?php while (have_posts()) : the_post();
$category = get_the_category();
//var_export( $category[0] ); // All info about this post's category
echo $category[0]->cat_name; // will print the category/sub-category name
?>
Make sure that in the post itself, the sub-category is checked but not the parent.
(sometimes it doesn't work if both are "checked")

Resources