I have listed all the post categories in my WordPress slider. But there's a problem; the category names are links.
if(has_category()) {
$cats_list = get_the_category_list(', ');
} else {
$cats_list = 'No categories';
}
And it's outputted by this:
<?php echo $cats_list; ?>
Is there any way to remove the links around it? I've read the WP docs but they don't give me any info about the links.
Solved it by creating this:
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
That still needs some tweaks, though.
A response from this question could help you:
$categories = array()
foreach((get_the_category()) as $category) {
$categories[] = $category->cat_name;
}
echo implode( ', ', $categories );
You could also change your echo line to:
<?php echo strip_tags($cats_list); ?>
Hope it helps!
Related
I know how to display Wordpress Current Post Title. It's like this:
<?php echo do_shortcode('[auto_gallery search="' . get_the_title($post_id) . '"]'); ?>
I also know how to display Tag without the link. It's like this:
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
?>
My question is, how do I display the Current Tags inside my Shortcode?
Because the method below doesn't work.
<?php echo do_shortcode('[auto_gallery search="' . get_the_tags($post_id) . '"]'); ?>
Any idea?
Please also teach me how to display Current Category inside my Shortcode.
Thanks for your help, I really appreciate it.
You can use this way to display tags inside a Shortcode:
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo do_shortcode('[auto_gallery search="' . $tag->name . '"]');
}
}
?>
I hope this helps.
<?php echo do_shortcode('[auto_gallery search="' . implode(" ",get_the_tags($post_id)) . '"]'); ?>
get_the_tags() function return the array of tags and you need to pass the string in the shortcode so simply array to string conversion.
Hails!
I am new to WP codex. Here's an example of code I have come up with to retrieve tags for a specific post:
<?php
global $post;
$post_tags = get_the_tags($post->ID);
if ($post_tags) {
foreach($post_tags as $tag){
echo '<div class="tag">' . $tag->name . '</div>';
}
}
?>
The question is - how to check is a tag is also shared with at least one other post (to show a tag only if there are at least two posts with the same tag)? Basically what I need is this:
if ($post_tags **AND $post_tags_count > 1**) { do the rest of the code
I know it should be simple but was unable to find how.
PS: $post_tags_count - just an example to show that I have to count posts sharing the same tag.
I believe I could add a loop with $count = 0; $count++; But maybe there is a better solution provided by WP? Thanks folks!
Okay. I have figured that out myself. Hopefully it will be useful for someone:
<?php
global $post;
$post_tags = get_the_tags($post->ID);
if ($post_tags) {
foreach($post_tags as $tag){
$tag_count = $tag->count;
if($tag_count > 1){
echo '<div class="tag">' . $tag->name . '</div>';
}
}
}
?>
im using
<?php
foreach((get_the_category()) as $category) {
$cats .= $category->cat_name . ', ';
}
echo rtrim($cats, ', ');
?>
to display the category names of a post. How would i go about out removing a specific category from this list
Untested but something like this should work:
<?php
foreach((get_the_category()) as $category)
{
if($category->cat_name == "category name")
{
}
else
{
$cats .= $category->cat_name . ', ';
}
}
echo rtrim($cats, ', ');
?>
I'm am working on a Wordpress custom template and I'm filtering post by their classes.
Since Wordpress displays a LOT of unnecessary classes when I put <?php post_class(); ?> in a page, ( here is what it gives me class="post-54 post type-post status-publish format-standard hentry category-3d category-web" )
I'm trying to simplify that by echoing only the categories that relate to the post.
EDIT: This is what calls the post in my page <?php query_posts( 'showposts=99' ); ?>
Then this piece of code
<li class="<?php if ( in_category('category-3d')) { echo "3d"; }
if ( in_category('category-animation')) { echo "animation"; }
if ( in_category('category-motion')) { echo "motion"; }
if ( in_category('category-shortfilm')) { echo "shortfilm"; }?>"></li>
give me this <li class="motion"></li> if my post is in the "motion" category.
The problem is that if my post is in several categories, only the first is echoed... How can I tell WP to echo ALL the names of the categories my post is affected to, while adding a space between them?
Since I am a beginner in php syntax, I'm still learning how to get such a simple thing working in a clean and effective way (i.e. without writing 16 lines of code !)...
Can someone help me on this one?
SECOND EDIT:
Ok I'm still trying to figure out why, but today, my piece of code (above) is working and echoes all my listed classes, but without space between them... So to make my classes work, I'm adding a space at the end of the echo part like this { echo "3d "; }. But I feel like it's a dirty way to make things work...
How to add a space between each class tag in the proper way? I'm aiming for something like this foreach $categories as $cat { echo $cat . " "; } but where $categories and $cat would refer to each "if" statement.
I'm not familiar with WP but if you get your categories with some function like get_categories() then you can use implode to make a string from all categories.
<?php $categories = get_categories(); ?>
<?php $categories = implode(' ', $categories); ?>
<li class="<?php echo $categories; ?>">...</li>
Could you provide what is returning get_categories()?
EDIT:
function get_class_attr() {
$classes = get_post_class();
$classes = substr($classes, 7, -1);
$arr = array();
$arr = explode(' ', $classes);
$classes = 'class="';
foreach($arr as $class_name) {
if(strpos($class_name, 'category-') !== false) {
$classes .= substr($class_name, 9).' ';
}
}
$classes = substr_replace($classes, '"', -1, 1);
echo $classes;
}
get_class_attr(); // output: class="3d web"
Also note that 3d is not a valid class name, you can use something like movie3d or film3d.
Alternative way is to create your class value a little bit earlier and then echo it:
<?php
$classes = array();
if ( in_category('category-3d')) { $classes[] = "3d"; }
if ( in_category('category-animation')) { $classes[] = "animation"; }
if ( in_category('category-motion')) { $classes[] = "motion"; }
if ( in_category('category-shortfilm')) { $classes[] = "shortfilm";}
if(count($classes)) {
$classes = implode(' ', $classes);
} else {
$classes = '';
}
?>
<li class="<?php $classes ?>"></li>
As of the code you've posted, it should add all the categories if the product is in all the four specified ones. You could add the space between the quotes after each echo, e.g.: echo ' motion'.
I would use another approach, adding really all categories, without the need to check them by name:
<li class="<?php echo implode( ' ', get_categories() ); ?>"> … </li>
See: http://codex.wordpress.org/Function_Reference/get_categories on how to filter the output of the function get_categories().
I would suggest instead you use this function from the Wordpress API - something you should read a lot of.
Something like this should work:
<?php $categories = get_categories(); ?>
<li class="<?php foreach ($categories as $cat) {
echo $cat . " ";
}?>"> -- Your li text --
</li>
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
if($child_cats) :
echo '{ ';
foreach($child_cats as $cat) {
echo $sep . $cat->cat_name;
$sep = ', ';
}
echo ' }';
endif;
?>
The above code outputs a number of categorys in this format:
A Cut Above,A20Labs,AMCH,
how would I add ' ' around each of the elements for output like this?
'A Cut Above','A20Labs','AMCH',
2nd question, how would I code it so that that the output goes into this array code like this?
<?php $type_array = array('A Cut Above','A20Labs','AMCH',)?>
Thanks so much!
Azeem
For your first question, change echo $sep . $cat->cat_name; to echo $sep . '\''.$cat->cat_name.'\'';
This will change it to output the name with single quotes around them.
To return an array instead, try this:
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
$type_array = array();
if($child_cats) :
foreach($child_cats as $cat) {
$type_array[] = $cat->cat_name;
}
endif;
?>
This will place the names into a new array instead of echoing them.
You can get the array you're wanting with a lot less work:
<?php
$child_cats = get_categories(array(
'child_of' => $parent_cat,
'fields' => 'names'
));
?>