Drupal: Views - Print taxonomy term description in header - php

I am using the following code to print the taxonomy term in the views page header.
<?php
$view = views_get_current_view();
$term_name = array_pop($view->args);
$term_name = str_replace('-', ' ', $term_name);
$possible_terms = taxonomy_get_term_by_name($term_name);
$term = $possible_terms[0];
print '<div class="term-desc">';
print filter_xss_admin($term->description);
print '</div>';
?>
The issue I'm having is that it works with all of the terms that have more than one word, but on the terms with only one word for the term name, it won't print the description.

Try
$term = array_pop($possible_terms)
instead of
$possible_terms[0].
You can also try doing a
foreach($possible_terms as $key=>$term){
$desc = $term->description
}
If that doesn't help do a
var_dump($possible_terms);
to see your data structure.

Related

PHP: Only show first word of a custom taxonomy

This is my code for displaying a taxonomy (WordPress):
<?php
$locations = get_the_terms($post->ID , 'location');
foreach ($locations as $location) {
echo '<div class="term-location">';
echo $location->name . ' ';
echo '</div>';
}
?>
How can I modify the code to only display the first word of the taxonomy? I've tried it using explode but I can't get it to work (since my php knowledge is limited).
Thanks in advance!
You can indeed use explode so you were on the right path!
Explode returns an array of strings. So you first use explode, and then select the first item.
$locations = get_the_terms($post->ID , 'location');
foreach ($locations as $location) {
echo '<div class="term-location">';
echo explode(' ', $location->name)[0] . ' ';
echo '</div>';
}
You can get more info on explode here, and an example on getting the first word here.

Wordpress PHP - Add the_permalink inside a variable then echo

I've got this code below that gets a specific category (from a taxonomy) then displays its sub-categories.
I'm trying to figure out how to add a href link to it, linking to the permalink in order to go to the archive page for that particular sub-category. So far I have this:
<?php
$terms = get_terms( 'job_listing_category', 'parent=59' );
$count = count($terms);
$link_address = the_permalink();
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<a href='".$link_address."'><p>" . $term->name . "</p></a>";
}
}
?>
This line: echo "<a href='".$link_address."'><p>" . $term->name . "</p></a>"; seems to be correct, however I need to reference the $link_address somewhere I assume...
I thought I could get the permalink of the sub-category by adding the line: $link_address = the_permalink();
However, this doesn't seem to work and I'm not sure why..
Any help greatly appreciated :)
You can get term link with get_term_link() and you can pass in the term object. You could do:
foreach ( $terms as $term ) {
echo "<a href='" . get_term_link($term) . "'>" . $term->name . "</a>";
}
If you want to save the permalink in a variable, you can use get_the_permalink() instead of the_permalink().

Pass PHP Variable into a shortcode

I'm trying to do something that seems really simple but I can't figure it out.
In my category template, I have this shortcode:
<?php echo do_shortcode( '[jobs categories="manufacturing"]' ); ?>
I also have this to show the title of the category:
<?php single_cat_title(); ?>
I would like to put them both together so I can pass the category into the shortcode so it fetches the correct job category listings. So I created this:
<?php $category = single_cat_title(); ?>
<?php echo do_shortcode('[jobs categories=' . $category . ']'); ?>
but it doesn't seem to work properly - Am I doing something wrong? Is this even possible?
Many thanks!!
If you look at the documentation for the single_cat_title() function, you'll see that it can either display or return the value, and it accepts two parameters $prefix and $display with default values of '' and true respectively.
What this means, is that just using single_cat_title(); will print Category Title to the document.
If you want to use it as a variable (without printing it to the document), you'll need to set the second parameter to false:
$category = single_cat_title( '', false );
This will define the $category variable for you, without printing anything, and you can then pass it to your shortcode (also note, that typically you'll want quotes around your attribute values in shortcodes):
echo do_shortcode( '[jobs categories="' . $category . '"]' );
You can make it a bit more succinct as well:
<?php
$category = single_cat_title( '', false );
echo do_shortcode( "[jobs categories='$category']" );
?>

Display advanced custom field (ACF) value from custom post type taxonomy - wordpress

In Wordpress I created a custom post type called "Sports" with a taxonomy called "sport_locations". Using the advanced custom field (ACF) plugin I created fields to show on taxonomy terms. I got everything to work, however I'm having trouble outputting the image I uploaded.
In ACF I have the option for the return value of the image to be: an object, url, or ID. Right now I have it set to object.
Below is the code I have so far. I'm writing this code inside of the single-sports.php . I created a foreach loop and only spitting out the terms associated to that specific sport.
When I do the var_dump I keep getting bool(false). Sorry for the extra commenting out in the code I was trying a bunch of different things and figured I'd leave it in my code as reference
post type = sports
taxonomy = sport_location
acf field = location_image (return value = object)
<?php
$terms = get_the_terms( $post->ID , 'sport_locations' );
//$LocImg = $wp_query->queried_object;
// echo '<pre>';
// echo var_dump($LocImg);
// echo '</pre>';
foreach ( $terms as $term ) {
$term_thumb = get_field('location_image', 'sport_locations'.$term->term_id);
echo '<pre>';
echo var_dump($term_thumb);
echo '</pre>';
echo '<div class="sport-single">';
echo'<img src="' .$term_thumb['url']. '" class="img-responsive">';
//echo '<img src="' .$locationImage[0]. '" class="placeholder" />';
//echo '<img src="' .get_src_picture(get_field("location_image", "sport_locations".$LocImg->term_id), "category"). '" class="img-responsive" />';
//echo '<img src="' .get_field( "location_image", $LocImg->taxonomy . "_" . $LocImg->term_id ). '" />';
echo '<p>'.$term->name .'</p>';
echo '</div>';
}
?>
There is supposed to be an underscore between the term name and the ID. So...
$term_thumb = get_field('location_image', 'sport_locations'.$term->term_id);
...should be...
$term_thumb = get_field('location_image', 'sport_locations_'.$term->term_id);
Alternatively, you can pass the term object...
$term_thumb = get_field('location_image', $term);

Create dynamic ids for wordpress gallerie shortcode from custom fields

this is my first post, always nice to read other questions. I'm looking for a solution to insert ids into a Wordpress Gallery Shortcode from Custom Fields i created with the ACF-Plugins.
The shortcode is looking like this:
<?php echo do_shortcode('[gallery type="carousel" ids="47,48,49,etc..."]'); ?>
I whould like to replace the specific ID's (numbers) with the value which is provided by the Custom Field. For example:
<?php echo get_field('galleryimage_1'); ?>
<?php echo get_field('galleryimage_2'); ?>
This would give me the correct first and second ID, I just don't have a clue how to include it into the shortcode.
Regards
If you are using just the standard ACF custom fields (and not the Gallery nor Repeater ACF add-ons), I'd suggest:
<?php $limit = 10; //number of your galleryimage_n fields
$imgArr = array();
for($ctr = 1; $ctr <= $limit; $ctr++){
$value = get_field('galleryimage_' . $ctr);
if($value){
$imgArr[] = $value;
}
}
$imgIds = implode(",", $imgArr);
echo do_shortcode('[gallery type="carousel" ids="'. $imgIds .'"]'); ?>
Looks good, way better then my solution:
echo do_shortcode('[gallery type="carousel" ids="' . get_field('galleriebild_1') . ', "]');

Categories