I created a custom post type with category taxonomy support.
On my archive template i have a dropdown with all categories to filter the archive.
Every category is linked with get_term_link(). The function works but it goes to the homepage. I want to link to the same page.
<?php
$args = array(
'type' => 'download',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => FALSE
);
$categories = get_categories( $args );
?>
<?php if ( count( $categories ) ) : ?>
<div class="btn-group margin-bottom-40">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
Categorie
<i class="fa fa-angle-down"></i>
</button>
<ul class="dropdown-menu" role="menu">
<?php foreach ( $categories as $category ) : ?>
<li <?php if ($category->cat_ID == get_query_var( 'cat' )) : ?>class="active"<?php endif; ?>><a href="<?php echo get_term_link( $category ); ?>"><?php echo $category->name ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
The links are now:
?cat=1
?cat=2
?cat=3
But it should be:
?page_id=5&cat=1
?page_id=5&cat=2
?page_id=5&cat=3
How can i fix this?
]]>https://codex.www.ads-software.com/Function_Reference/get_term_link
When i call function with OBJECT parameter , the function working fine but not working with “term ID / term slug” parameter.
$term
(object/int/string) (required) The term object / term ID / term slug whose link will be retrieved.
Default: None
Basically I have a custom taxonomy where each term is listed on a custom page, I then am using the get_term_link()
function to link to a page which displays all the posts within that term.
The URL that is returned from that function is https://www.frictionmultimedia.com/?taxonomy=RepresentedArtist&term=fin-davies but when I click on the link it goes to “Page not found”.
I have a page called taxonomy-representedartist.php which is where I was expecting the link to go to but obviously it doesn’t.
What have I done to cause the link not to go anywhere? is the page named wrong or something?
Any help is much appreciated,
Thanks,
Matt
I have created a “Portfolio” custom post in which I wish to insert posts under different categories (or “type” as I’ve called it). It’s a photography website and I currently have just two categories for testing purposes: “landscape” and “cityscape”. I want to put links to each post manually (i.e. outside of any loop) in a menu or on a page. I understand that I need to use the get_term_link() function but if I put the below in my menu then the page loads without any errors but only loads up until the third list item (where I use the get_term_link function). The forth list item does not display and, more strangely, if I click on the ‘home’ link the blog page does not load (page just reloads as is). If I remove the get_term_link item from the menu, the page loads completely and the ‘home’ takes me to the blog page as I would expect.
I have no idea why this is happening since I think the code is right — any help/suggestions would be very much appreciated!!
<div id="menu">
<ul id="menu">
<li><a>">Blog</a></li>
<li><a>">Gallery</a></li>
<li><a>">Landscapes</a></li>
<li><a href="?page_id=13">Guide</a></li>
</div><!-- #menu -->
---------------------------------
//In function.php file...
add_action('init', 'portfolio_register');
function portfolio_register() {
$labels = array(
'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio Item', 'post type singular name'),
'add_new' => _x('Add New', 'portfolio item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail'),
'has_archive' => true,
);
register_post_type( 'portfolio' , $args );}
register_taxonomy("Type", array("portfolio"), array("hierarchical" => true, "label" => "Type", "singular_label" => "Type", "rewrite" => true));
]]>Full error when trying to echo get_term_link() w/in a home page template:
“Catchable fatal error: Object of class WP_Error could not be converted to string”
Usage:
$term = get_term_by( 'id', $cat_id, $tax);
echo get_term_link( $term->slug, $term->taxonomy );
The term object is returning just fine; if I echo $term->slug or $term->taxonomy they return the correct value… The issue seems to be within get_term_link() itself…
Don’t see anything in the codex entry that helps (the term exists, obviously):
https://codex.www.ads-software.com/Function_Reference/get_term_link
<?php
foreach($all_tearm as $term){
if($term->taxonomy == 'restaurant_choices'){
echo '<a href="' .get_term_link($term->slug, 'restaurant_choices') .'">'.$term->name.'</a>, ';
}
}
?>
So the above will output:
a,b,c,d,
But I need:
a,b,c,d
Anyway, I wanted to put a link to that page on my front page. I found that if I use the function get_term_link() it will give me a link.
But this only works if the permalinks are on their default setting. It doesn’t go to the page if the permalinks are set to post name, for example.
Can this be fixed?
(this isn’t the first time I’ve had trouble getting wordpress to go to a template file when the permalinks are set to something besides default)
]]>PHP Notice: Trying to get property of non-object in /wp-includes/taxonomy.php on line 2891
$taxonomy = $term->taxonomy;
// line 2891
PHP Notice: Undefined property: stdClass::$taxonomy in /wp-includes/taxonomy.php on line 2901
elseif ( $t->query_var )
// line 2901
The reason for this is that the $term object doesn’t contain the expected property, because the alias in the plugin’s sql query is “_taxonomy”:
SELECT ... tt.taxonomy AS _taxonomy
(sitemap-core.php line 2126)
Then inside get_term_link() the code branches to line 2904 and produces an incomplete link because $taxonomy is empty
$termlink = "?taxonomy=$taxonomy&term=$slug";
// line 2904
get_term_link($term, $term->_taxonomy)
WP however expects get_term_link($term, $term->taxonomy)
, which means that inside get_term_link the code doesn’t branch out as intended and returns an incomplete link.
Just wondering if this is intentional or a bug?
]]>In functions.php, I have attempted to use the following functions:
if(!function_exists('duration')){
function duration(){
global $post;
$terms = get_terms('duration_span');
echo '<ul class="catlist">';
foreach ($terms as $term) {
echo '<li><a href="'.get_term_link($term->slug, 'duration_span').'">'.get_post_meta($post->ID, 'duration', true).'</a></li>';
}
echo '</ul> || ';
}
}
The function above gives me a list of ALL possible ‘duration_span’ categories WHETHER OR NOT THE POST IS WITHIN THOSE CATEGORIES. The specific duration does display as the link text.
`if(!function_exists(‘duration’)){
function duration(){
global $post;
echo ‘ || <ul class=”catlist”>’;
echo get_the_term_list( $post->ID, ‘duration_span’, ‘
‘ );
echo ”;
}
}
The function above displays only the applicable ‘duration_span’ category, but does not seem to allow me any way to change the link text to the content of the ‘duration’ custom field.
I’m not sure how to proceed from here. Suggestions would be most welcome!
]]>