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>';
}
}
}
?>
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.
I want to add linked tags to a post but in the middle of the page. We are using it for a portfolio post and want to list some basic details in the middle of everything else. You can see the empty tags area on this page: http://ucdev.inventivewebdesign.com/portfolio_page/lifechurch-phase-i-kidslife/
I have tried adding and to the page but it doesn't let me use php in the post.
I then tried to add a plugin that lets me use PHP snippets. It works partially:
echo 'Tags: ';
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
It shows Tags: on the page but not the list of tags. I have also tried these:
global $post;
foreach(get_the_tags($post->ID) as $tag)
{
echo '<li>' . $tag->name . '</li>';
}
<?php $tags = get_the_tags();
if( $tags ) foreach( $tags as $tag ) { ?>
<?php echo $tag->name; ?>
<?php break; }
Any help would be appreciated.
Hi M I found you also use Wordpress to make the site, so here is my idea for you.
use add_shortcode('short_code_name','short_code_function') at the end of functions.php file, then add the shortcode [short_code_name] to any textblock anywhere in your post/page. The following are the steps:
1 open functions.php file under wp-content/themes/theme-name/functions.php;
2 add a function about displaying the tags on the post page:
add_shortcode('test_tags','test_tags_function');
function test_tags_function(){
echo 'Tags: ';
$posttags = get_the_tags();
if ($posttags) {
echo "<ul style=\"list-style-type:none\">";
foreach($posttags as $tag) {
echo '<li>';
echo $tag->name . '</li>';
}
echo "<ul>";
}
}
image:
3 add [test_tags](example) to a textblock, like:
4 if you want the black dot in front of your tags, then delete this part of code:
style=\"list-style-type:none\"
is this what you like "list of tags"?
At the end of each page and every post, I would like to output the tags as a list in a shortcode.
Unfortunately, I do not know much about PHP, but someone who can understand will definitely be able to correct my mistake using the code below.
Thank you in advance!
<?php // functions.php | get tags
function addTag( $classes = '' ) {
if( is_page() ) {
$tags = get_the_tags(); // tags
if(!empty($tags))
{
foreach( $tags as $tag ) {
$tagOutput[] = '<li>' . $tag->name . '</li>';
}
}
}
return $tags;
}
add_shortcode('tags', 'addTag');
The method needs to return a string to be able to print any markup.
"Shortcode functions should return the text that is to be used to replace the shortcode." https://codex.wordpress.org/Function_Reference/add_shortcode
function getTagList($classes = '') {
global $post;
$tags = get_the_tags($post->ID);
$tagOutput = [];
if (!empty($tags)) {
array_push($tagOutput, '<ul class="tag-list '.$classes.'">');
foreach($tags as $tag) {
array_push($tagOutput, '<li>'.$tag->name.'</li>');
}
array_push($tagOutput, '</ul>');
}
return implode('', $tagOutput);
}
add_shortcode('tagsList', 'getTagList');
Edit: Removed check for is_page since get_the_tags will simply return empty if there aren't any
Be aware that pages don't have tags unless you've changed something.
That said, this should work. It also adds the <ul> around the tags, but you can change that, and I'm sure you see where. I've used is_singular, but you'll probably get away without that condition at all, unless you do add [tags] in a custom post type and don't want the output there.
I assume you want to add more modification, otherwise webdevdani's suggestion regarding using the_tags is probably simpler.
// get tags
function addTag( $classes = '' ) {
if( is_singular("post") || is_singular("page") ) {
$tags = get_the_tags();
if(is_array($tags) && !empty($tags)) {
$tagOutput = array("<ul>");
foreach( $tags as $tag ) {
$tagOutput[] = '<li>' . $tag->name . '</li>';
}
$tagOutput[] = "</ul>";
return implode("", $tagOutput);
}
}
return "";
}
add_shortcode('tags', 'addTag');
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!
Here is the code , it's getting me only the last field I posted , whereas I want to display ALL the fields !
if (!empty($node)) :
//$fieldImage = field_get_items('node', $node, 'field_image_slider');
print var_dump($node);
foreach ($node->field_image_slider['und'] as $item) {
$val=$item['uri'];
print '<div class="item"><figure><img src="' .file_create_url($val). '" /></figure><h3 class="orange">Hi</h3></div>';
}
endif;
Till now I've spent 2 days searching for a solution , but I did'nt found ..
Any help would be extremely appreciated !
Try this...
if (arg(0) == 'node') {
$node = node_load(arg(1));
}
if (!empty($node)) :
foreach ($node->field_image_slider['und'] as $item) {
$val=$item['uri'];
print '<div class="item"><figure><img src="' .file_create_url($val). '" /></figure><h3 class="orange">Hi</h3></div>';
}
endif;
print_r($node->field_image_slider);
die('dead');
Let's start from this. Please tell me do you see anything except dead on white screen?
Use entity_metadata_wrapper:
if (!empty($node)) {
$wrapper = entity_metadata_wrapper('node', $node);
$images = $wrapper-> field_image_slider->value();
print(var_dump($images));
...
}
You should get a nice array that is easy to use, empty if there are no images.