I want to add a ribbon on my posts on the front page, which shows the posts category.
I can add it as text like this (I use the Imbalance theme by WPShower):
<?php imbalance2_posted_in(); ?>
But how can I just write the Category Name, without markup so I can use it in classes and such?
Thank you in advance.
You can get the category of every post with get_the_category(). Below is shown how to get the category of the current post.
global $post;
$category = get_the_category( $post->ID ); //OR SOME OTHER ID, DEPENDING ON WHAT YOU WANT
$category_name = $category->name; //GETS THE ORIGINAL NAME, INCLUDING WHITESPACES
$category_slug = $category->slug; //GETS THE SLUG, WHICH WILL BE BETTER TO USE IN CLASSNAMES
EDIT
<?php
global $post;
$category = get_the_category( $post->ID );
?>
<div class="box <?php echo($category->slug); ?>"></div>
You can use post_class() to generate a few class names including one for each category.
If you want to do it manually, you can get information on the categories using get_the_category() and put the class names together yourself.
Related
I am coding a theme that uses an archive on a page with a special template. Above the results of the archive (that also means outside of the loop) I want to display the values of some custom fields. (e.g. the value of the field “intro”. I tried to create an additional loop and have the values displayed, but there are no results. Does anybody see a mistake?
Thanks!
Raphael
Here is what I tried last:
<div class="newsearch_intro">
<?php global $post;
$args = array('numberposts'=>'1');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
the_field('intro');
endforeach;
?>
</div>
Try using the_field() / get_field() with the post ID as a second parameter:
<div class="newsearch_intro">
<?php
global $post;
the_field('intro', $post->ID);
?>
</div>
I don't know if this is possible but I'll try to explain.
I have a custom post type (creatives) and a taxonomy (image-sort). Every post in the CPT is a person (a creative). They can upload images and sort them by choosing which categories (the 'image-sort' taxonomy) they belong to.
There are multiple creatives, and they will post images to different categories. Some to all of them, some to just a few.
Every creative have their own page with a dynamic listing of which categories they have posted content to. My problem is though that if one creative have posted to 'cat-1' and 'cat-2' everybody get that listed out on their respective page. What I want is to only show 'cat-1' and 'cat-2' on the creative which has posted to those categories. If another creative has posted to 'cat-1' and 'cat-3' I only want those two to appear on his page.
Does this make sense?
<ul>
<?php
$terms = get_terms('image-sort');
if ( $terms && !is_wp_error($terms) ) :
foreach ( $terms as $term ) :
?>
<li><?php esc_html_e($term->name); ?</li>
<?php endforeach; endif; ?>
</ul>
Not sure if i understand you, but if i do, try replacing this line:
$terms = get_terms('image-sort');
with the following line:
$terms = wp_get_object_terms(get_the_ID(), 'image-sort');
EDIT
Try fetching the terms with the following piece of code:
<?php
// get all attachments of the current post
$attachments = get_children('post_parent=' . get_the_ID() . '&post_type=attachment&post_status=any&posts_per_page=-1');
// we're saving the terms here
$all_terms = array();
// looping through all attachments
foreach( $attachments as $attachment) {
$terms = wp_get_object_terms($attachment->ID, 'image-sort');
if ($terms) {
// looping through all attachment terms and adding them to the main array
foreach ( $terms as $term ) {
$all_terms[$term->term_id] = $term;
}
}
}
?>
I am using the "advanced custom fields" plugin and need to have it so that a custom field is pulled in for the category pages. I can get these to come in on pages, but the category pages are giving me a lot of trouble... 'video' is the name of the custom field I want to pull in.
This is the code I am currently using:
<?php echo get_field('video', 'clear-creek'.$wp_query->queried_object->term-4); ?>
or just a standard version like this which works on the regualar pages...
<?php the_field('video'); ?>
but it's not working... can someone please help steer me in the right direction?
Thanks!
If you are on a category's archive page, you would use this:
<?php echo get_field('video', 'category_'.get_query_var('cat')) ?>
If you are on a custom taxonomy instead, you would use this:
<?php $queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
echo get_field('video', $taxonomy . '_' . $term_id); ?>
This will dynamically get the taxonomy's slug and ID, and build out your get_field based on that information.
I'm trying to have our WP theme call in a specific sidebar when the user lands on a dedicated article/permalink page. The post category will determine which sidebar is pulled in.
Example: If the user arrives on a permalink/dedicated article page for a post categorized as "red", then the theme would look for the post category, acknowledged that the category is red, and then find and pull in the sidebar that I have defined as "red".
Just getting started with this project and would appreciate any thoughts on what I describe below.
This is what I think the code should look like to achieve that:
<?php
if (is_category()){
$current_cat = intval( get_query_var('cat') );
get_sidebar($current_cat); //for category red get sidebar-red.php
}
?>
One more piece to the puzzle:
I want to add in an addition argument so that if the post is uncategorized (i.e. we did not categorize the post for whatever reason) that it will pull in a default sidebar. I think that code should like something like this, but I don't know how to define "non-existent category". Basically I want to tell wordpress to look for the post category. If it finds that one does not exist, then I want it to pull in sidebar-default.php
<?php
if (is_category()){
$current_cat = intval( get_query_var('cat') );
get_sidebar($current_cat); }
elseif (is_category(**argument for nonexistent category**))
$current_cat = intval( get_query_var('cat') );
get_sidebar-default.php; }
?>
You should be using in_category not is.
For example:
<?php
if ( in_category('fruit') ) {
get_sidebar('1');
} elseif ( in_category('vegetables') ) {
get_sidebar('2');
} else {
// do nothing or something
// ...
}
?>
http://codex.wordpress.org/Function_Reference/in_category
<?php in_category( $category, $_post ) ?>
$category
(mixed) (required) One or more categories specified by ID
(integer), name or slug (string), or an array of these
Default: None
You can have the different sidebar classes defined in your CSS.
Give the category that you want to match up with the style the same name as that category's slug. In your template, define the sidebar div's class using the post's category-slug.
Here's a bit on how to get the slug:
<?php
if (is_category( )) {
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
echo 'the slug is '. $yourcat->slug;
}
?>
So your sidebar div would be something like:
<div class="<?php echo yourcat->slug; ?> sidebar"></div>
So you've given your sidebar two classes, one called sidebar and one to be determined by whatever the category is!
Hopefully this made sense.
You can even use Category Templates, so you create category-red.php and category-blue.php files
I am trying to pull excerpts and custom fields from specific posts, I have tried using post titles and post id but I am only succeeding in pulling every single post using this query. For example I have this code trying to pull just the title for the post with id 182
<?php $map = new WP_Query();
$map->query('post_id=182'); ?>
<?php while ($map->have_posts()) : $map->the_post(); ?
<?php the_title(); ?>
<?php endwhile; ?>
It pulls the title of every single post using this method and I can not figure out how I am going to have multiple loops like this each pulling content from just one specific post. Can someone please explain where I went wrong?
I've had luck with WP_query('p=182').
If you know the post ID then you can use get_post($post_id); like so
$post_id = 182;
$my_post = get_post($post_id);
$title = $my_post->post_title;
echo $title;
echo $my_post->post_content;
ckeckout codex