Wordpress PHP - Add the_permalink inside a variable then echo - php

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().

Related

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);

display particular image associated with tag

I'm new with wordpress and I tried to look for an answer but everything I found and tried didn't work. So let's start from the beginning I added this code to functions.php in my child theme:
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
Now I can add tags to images in admin section.
The problem is that I want to display those images on post and category page next to the tags assigned to the post at the bottom where they appear in default. I added the category to the main navigation so after I click on it a page with all the post excerpts in the category are displayed. And at the bottom are shown tags associated with given post.
An example cause I'm not sure I explained it so that it's easy to understand.
I have 3 posts: project1, project2 and project3 which are in category Projects. Every one of those three projects has assigned one or more of these tags: company1, company2, company3. For every company tag there is an image with the same company tag assigned to an image (logo of the company). And I want to display not only the tag name but also the image associated with the tag.
Is there some way to do this?
Thanks in advance.
I used this code snippet which I found here:
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg"
alt="' . $tag->name . '" />';
}
}
?>
So the final product is this:
<?php
$posttags = get_the_tags();
$templPath = get_stylesheet_directory_uri() .'/images/';
if ($posttags) {
foreach($posttags as $tag) {
$html = '<div class="projectLinkWrap">';
$html .= '<a href="'. get_the_permalink() .'#projectpartner"/>';
$html .= '<div class="thumbLogoWrapper">';
$html .= '<img src="'.$templPath . $tag->name.'.png"
alt="' . $tag->name . '" /></div>';
$html .= '<span class="tagName">'. $tag->name .'</span></a></div>';
echo $html;
}
}
?>
Hope it helps someone one day.

wordpress php targeting multiple ids to change colour

Okay, so i've got a request for this update which I thought might be simple enough but is turing out a bit tricky than I though.
basically the site has these sub ids for each property 'Vest' and 'Verve' they want each one to be a different colour.
The css that's link to them is <span class="proptery-type-grid"> so obviously I'd add in a second css for each colour, but the id's in the php are being called like this: <span class="proptery-type-grid"<?php echo inspiry_get_property_types( $post->ID );?></span>
so what would be the best way (i'm thinking like a if or else statement) to go about targeting the individual ids to change the colours of them?
Thanks
Maybe you can use a ternary statement? http://us2.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Psuedocode:
echo ($post->ID('1') ? '<span class="proptery-type-grid"<?php echo inspiry_get_property_types( $post->ID );?></span>' : '<?php echo inspiry_get_property_types( $post->ID );?>');
I've solved it with
<span class="proptery-type-grid"><?php $post_terms = get_the_terms( $post->ID, 'property-type' );
foreach( $post_terms as $term) {
if( $term->name == 'Verve') {
echo '<span class="proptery-verve">Verve</span>';
} elseif( $term->name == 'Zest') {
echo '<span class="proptery-zest">Zest</span>';
} else {
echo '<span class="proptery-type-grid type-none"></span>';
}
}?></span>

Wordpress Query on a Page using Allow PHP in Posts and Pages Plugin

It does what I want it to do, displays a certain category of posts in a column on a page. Right now its just the title, but I want to link that title and the permalink section isnt working, or rather the href.
[php]
// The Query
$the_query = new WP_Query( 'cat=3' );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul style="list-style:none;">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . '<a href="the_permalink();">' . get_the_title() . '[/a]'. '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
[/php]
It links to subdomain.domain.com/site/the_permalink(); instead pulling the permalink for that post and linking to it.
the_permalink(); return to echo your link.
You need to return only string. For this you can use get_the_permalink($post->ID);
Because your permalink function is inside of echo function.
When you enter the a-tag you start with HTML, if you close it you change to BBCode.
Fatih is right, you need to use get_permalink function. Since you're inside the loop, you don't need to specify the parameter ($post->ID).
Also, you need to switch back to PHP (that is your main problem) as you did with get_the_title function. There are multiple syntax issues with your code (brackets!).
The line should go like this:
echo '<li>' . get_the_title() . '</li>';
Learn the difference between echo and return in (not only) PHP functions!

Drupal: Views - Print taxonomy term description in header

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.

Categories