Wordpress Portfolio changes using PHP - php

I have added project-types to my website portfolio https://littleseabear.com/portfolio but it lists all possible tags, and not the tags directly linked to the portfolio.
For example, Cast should only be film, screenplay and work in progress, while When the Boys Come Home should be Radio and Produced.
Here is my code:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_terms( 'jetpack-portfolio-type' );
if( is_archive() || is_home() || is_front_page() ){
?>
<div class="post-wrapper">
<?php
infinity_news_post_thumbnail();
}
?>
<div class="post-thumbnail">
<?php
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>, ';
}
?>
<div class="article-details <?php if( is_single() ){ echo 'single-article-details'; } ?>">
Also... is there a way to get rid of the commas? Thanks

get_terms retrieves all available tags, not only the ones associated to the current post. Instead, modify the get_terms line at the top like so:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_the_terms( get_the_ID(), $taxonomy );
This will collect terms of the $taxonomy associated to the current post.
Edit:
I missed the question about the commas. To totally get rid of those, you can just remove them from the output:
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a> ';
}
I removed the , right behind the closing </a>, so it will now only be separated by whitespaces. If you want to have a special separator only between items, you can achieve this like so:
$separator = ''; // initialize empty
foreach ( $tax_terms as $tax_term ) {
echo $separator.'<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>';
if (empty($separator)) {
// Choose a custom separator HERE:
$separator = ', ';
}
}

Related

Comma separate custom taxonomies (no links)

Here's just a simple little question I can't seem to figure out. I have one custom taxonomy which has multiple options. But I want to show the taxonomies without links. So I use this code:
<li>
<?php
$terms_as_text = get_the_term_list($post->ID, 'opties');
if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) , '';
?>
</li>
This display's only the selected custom taxonomies of opties. Because this taxonomy has multiple options I would like to comma separate them. But it won't let me.
Normally you would use:
<?php
echo get_the_term_list( $post->ID, 'opties', '<ul><li>', '</li><li>', '</li></ul>' ); ?>
The first part = before.
The middle part = separate.
The last part = after.
But this makes links of the custom taxonomy terms, and I don't want that to happen.
But because of the strip_tags($terms_as_text) I can't comma separate them.
How can I get them to separate with a comma?
You can try the following:
global $post;
$opties = wp_get_post_terms($post->ID, 'opties', array("fields" => "names"));
if (count($opties) > 0)
{
echo implode(', ', $opties);
}
try this
$list = get_the_term_list( $post->ID, 'opties');
$terms ="";
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $list, $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
$terms .= $match[3].","; // Get Text Only //nolinks
}
}
echo rtrim($terms,","); // display and remove extra ,
Try this:
if( is_singular( 'custom_post_type' ) ) {
get_the_term_list( get_the_ID(), 'custom_post_type_name', '', ', ' );
}
elseif( is_singular( 'post' ) ) {
get_the_category_list( ', ', '', get_the_ID() ) . '';
}
The above will return categories or custom post taxonomies with comma separated and a link to the response category or taxonomy.
The simple way to get the category or term without link is to use strip_tags() function as follow:
<?php echo strip_tags(get_the_term_list( get_the_ID(), 'category_name' ));?>

Retrieve single tag among all tags

I need help with my wordpress site. While displaying "tags" on a product page,
<?php echo $product->get_tags(', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '</span>' ); ?>
is used to show them.
However, I need product page to show only 1 tag for a product. This code makes all tags to appear on the page. Which code should I use or how should I edit this?
in example:
if a product has the tags: summer, winter, fall, spring
I want only "fall" to be seen
<?php
$tags = get_terms( 'product_tag' );
$first = true;
foreach ($tags as $tag) {
if ( $first ){
$term_link = get_term_link( $tag );
if ( is_wp_error( $term_link ) ) {continue;}
echo '' . $tag->name . '';
$first = false;
}
}
?>
This will only print the first tag in a woocommerce product page. Its tested and working.
If you want to display any selected tag, then you need to define which tag you want to be visible. Maybe using a custom field. Lets say you have a custom field name 'selected_tag' using ACF plugin and it will print the exact tag name that you want to visible in the product page.
<?php
$selected_tag = get_field('selected_tag');
$tags = get_terms( 'product_tag' );
foreach ($tags as $tag) {
if ( $tag->name == $selected_tag ){
$term_link = get_term_link( $tag );
if ( is_wp_error( $term_link ) ) {continue;}
echo '' . $tag->name . '';
}
}
?>

Wordpress Product-Filter with CPT & Custom Taxonomy

I'm building a Site with Wordpress link to site It's a Site with Custom Post Types (product) and Custom Post Taxonomies (product_category).
On the Category page I have to filter the products by Subcategories.
I found the filterable.js with a tutorial.
My Problem: If I click on any subcategory (Filter-Item) the browser (Chrome and Filezilla) doesn't work anymore.
If I click on the filter the browser says "processing request"
after a long loading time the browser gives me an error.
Here's a screenshot of the Browser error:
http://farbstrakt.com/screenshot.png
I think the problem is about how I change the URL.
I'm searching days for any solution but nothing.
I can't find the error
here's my taxonomy-product_category.php
<div class="container middler" id="demo">
<ul class="filter-wrapper clearfix" id="portfolio-filter">
<?php
$args = array(
'show_count' => false,
'child_of' => get_queried_object()->term_id,
'taxonomy' => 'product_category',
'title_li' =>'',
);
$terms = get_categories($args);
$count = count($terms);
if ( $count > 0 ){
echo '<li class="cl-effect-2"><div><span data-hover="Alle">Alle</span></div></li>';
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<li class="cl-effect-2">
<div>
<a href="#'.$termname.'">
<span data-hover="'.$term->name.'">'.$term->name.'</span>
</a>
</div>
</li>';
}
}
?>
</ul>
<?php
$loop = new WP_Query(array('post_type' => 'product', 'posts_per_page' => -1));
$count =0;
?>
<div id="portfolio-wrapper">
<ul class="grid effect-2 item-wrapper clearfix filter-all" id="grid">
<?php if ( $loop ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'product_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php $infos = get_post_custom_values('_url'); ?>
<li class="<?php echo strtolower($tax); ?> all ">
<div>
<figure>
<?php the_post_thumbnail( array(400, 160) ); ?>
<figcaption><?php the_title(); ?></figcpation>
</figure>
</div>
</li>
<?php endwhile; else: ?>
<p class="no-products">NO products
</p>
</ul>
<?php endif; ?>
and thats the short snippet from the code above where i think there must be the error.
<?php
$terms = get_the_terms( $post->ID, 'product_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php $infos = get_post_custom_values('_url'); ?>
<li class="<?php echo strtolower($tax); ?> all ">
Would be grateful if you could help me. I have searched a lot and didn't found any solution. I'm a PHP-Newbie. Of course I could use another filter plugin but I want to figure out where's the problem
Thanks
The issue is appending a url with #all means that the browser will look for a element with a id of all . It cant find it on your page, because no element exists with this id.Hence the browser crashes after a few links have been clicked on.
just for reference, a element with a id looks like this:
<div id="someelement" class="not the same thing as id" >content</div>
<div id="anotherelement" class="someelement" >content</div>
In this case, if you append the url of the page with this element on it with #someelement the browser will focus on the first element. This does not look for elements with the class 'someelement'.
If you want to pass query args, you need to use the ?products=all&etc format. But what i think you want to do is actually redirect to a page displaying only the selected term? use /term instead in your href.
Also you are getting a 404 on product_category and any of the terms you had on the page. Part of the issue is you are using $term->name instead of $term->slug which may be different. Did you reset the permalinks when you created the taxonomy?
Also check the settings as described here:
https://codex.wordpress.org/Function_Reference/register_taxonomy
By default WP will create a loop on taxonomy-term.php and archive pages, the loop you run is a second db request and loop so thats extra time added to your server response time. Use the pre_get_posts hook
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

loop to display taxonomy terms and description

So I am attempting to build the first page in my navigation heirarchy to choose product categories.
I have a custom taxonomy called collections. I would like to create a page that loops through all terms in the collections taxonomy and displays the Term +link and description.
I created a file called taxonomy-collections.php and put the following code in. But it is not doing the trick.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'collections' );
echo '<ul>';
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
echo term_description($post->ID,$term);
}
echo '</ul>';
?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
This is almost straight from the codex so I'm guessing I am missing something.
currently the code has each term displayed as a list, but I do want to change it to a grid format if someone could help with that too.
So each term and description will be wrapped in a div and right aligned with the next.
Thanks
Like this?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'collections' );
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<div>' . $term->name . '';
echo term_description($post->ID,$term).'</div>';
}
?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Wordpress: disable links for get_the_category_list function?

Hey my theme uses this function to display the categories a post has, but it also creates links which I would like to get rid of. I prefer php rather than a javascript solution.
Here is the code:
<p class="postmeta">
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'gridster' ) );
if ( $categories_list && gridster_categorized_blog() ) :
?>
<?php /*printf( __( '%1$s', 'gridster' ), $categories_list );*/ echo $categories_list; ?>
<?php endif; // End if categories ?>
<?php endif; // End if 'post' == get_post_type() ?>
</p>
Link to the codex reference
How can I void those links?
or get rid of links all together from DOM (but this might create more work as the actual text is between the <a> tags
HTML
<p class="postmeta">
<a target="_blank" href="http://whatever.com/category/default/" title="View all posts in Default" rel="category tag">Default</a>
</p>
Thanks!!
If you just want a text-string returned and use the native get_the_categories() function, you could simply wrap it in PHP's strip_tags() function to remove all HTML that is returned:
echo strip_tags( get_the_category_list(__( ', ', 'gridster' ));
You can try modifying this to fit your needs:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
(From http://premium.wpmudev.org/blog/how-to-get-a-wordpress-category-name-without-the-link/ )
One option is to copy the original function and adapt to your needs. Modified to remove the <a> tags but not tested, compare to the original, adapt if needed, and add it to the theme's functions.php:
function category_list_so_22771587( $separator = '' ) {
global $wp_rewrite;
$categories = get_the_category( $post_id );
$thelist = '';
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= $category->name;
break;
case 'single':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name;
break;
case '':
default:
$thelist .= $category->name;
}
++$i;
}
return $thelist;
}
And modify your template to:
$categories_list = category_list_so_22771587( __( ', ', 'gridster' ) );
You can retrieve categories, extract the 'name' column of each items, and implode each values with a comma:
<?= implode(', ', (array_column(get_the_category(), 'name')))?>
Or you can retrieve categories list with link, and remove a tags :
<?= strip_tags(get_the_category_list(', ')) ; ?>
Do you just want to hide the links? If so use css:
.postmeta a {display: none}
Edit, you can also "disable" those links in css:
.postmeta a {
pointer-events: none;
cursor: default;
}

Categories