Wordpress, echo list of slug terms with custom link - wordpress

i'm trying to get a list of terms (using CPT UI) with my custom link where the slug is the last part of the custom link.
Example:
Clients: Term1, Term2, Term3
where each term is a link like:
example.com/#term1
example.com/#term2
example.com/#term3
So, i have the same custom link structure but only the final slug changes:
$servizio = get_the_terms($post->ID, 'servizio');
$servizio = array_values($servizio);
for($cat_count=0; $cat_count<count($servizio); $cat_count++) {
echo $servizio[$cat_count]-> slug;
if ($cat_count<count($servizio)-1){
echo ', ';
}
}

Use instead foreach loop, it's a lot easier to iterate in terms.
$categories = get_the_terms(get_the_ID(), 'servizio');
$categories_output = [];
if ($categories) { // Prevent the error #Invalid argument supplied for foreach()# incase the post has no category
foreach ($categories as $category) {
$categories_output[] = '<a href='. get_bloginfo('url') . '/#' . $category->slug .'>'. $category->name .'</a>';
}
}
if ($categories_output) {
echo implode(', ', $categories_output);
}

Related

Get the terms of a custom taxonomy without explicit taxonomy IDs in Wordpress

I used the following code to try it but it doesnt work. I need all terms of the taxnonmy which are saved for this explicit post. But I only get "array".
get_the_terms( $post->ID, 'regisseur', 'Regisseur: <span class="flight">
Additional I want to except the ID 43 and 76 – this terms shouldnt be shown in the list.
Why it doesnt work?
If you need to get all the terms in a post, you should use wp_get_post_terms() instead of get_the_terms()
Try this (updated)
<?php
$terms = wp_get_post_terms($post->ID, 'regisseur');
if ($terms)
{
$output = '';
foreach ($terms as $term) {
//Here we exclude terms
if ($term->term_id == 43) continue;
if ($term->term_id == 76) continue;
$output .= '<span>' . $term->name . '</span>';
}
};
?>

WP_Query and Woocommerce

I have a function inside (*Wordpress Child Theme) functions.php which returns the attached WooCommerce product categories, using global $wp_query. The get_posts() function will only return the number of products for the first page of products. (*the post_per_page value - in this case 16).
I have tried to temporarily set the post_per_page to -1, by adding the code below right before my function call in archive-product.php template:
$wp_query->set('posts_per_page', 999);
$wp_query->query($wp_query->query_vars);
and then resetting the value after the function call inside archive-product.php template
$wp_query->set('posts_per_page', 16);
$wp_query->query($wp_query->query_vars);
This almost works, but messes up the pre_get_posts function (*which sorts the products), and also seems to cause issues with the listing of the product results if over 500 products?
Please any suggestions would be greatly appreciated. Thanks.
//build dynamic category select menu based on attached categories
function dynamic_category_select() {
global $wp_query;
$my_posts = $wp_query->get_posts();
$my_post_ids = wp_list_pluck($my_posts, 'ID');
$categories = wp_get_object_terms($my_post_ids, 'product_cat');
foreach($categories as $category) {
$options[] = '<option value="' . $category->slug . '">' . $category->name . '</option>';
}
$x = '<select id="category-options" name="category-options">';
$x .= '<option value="">Select Category Options</option>';
foreach($options as $option) {
$x .= $option;
}
$x .= '</select>';
return $x;
}
Try replacing your $my_posts = $wp_query->get_posts(); with the following:
// Get current query vars
$my_query_args = $wp_query->query_vars;
// Disable paging - return all results
$my_query_args['nopaging'] = true;
// Create a new query with the modified query vars
$my_query = new WP_Query($my_query_args);
$my_posts = $my_query->get_posts();

Drupal - Show child terms of current term, only if child items exist - code modification needed

Having fought with the problem of showing child terms for a given taxonomy term in a block, I finally stumbled on a piece of code that does exactly what I want here
As per the instructions I've added the following to my template.php
function themename_child_terms($vid = 1) {
if(arg(0) == 'taxonomy' && arg(1) == 'term') {
$children = taxonomy_get_children(arg(2), $vid);
if(!$children) {
$custom_parent = taxonomy_get_parents(arg(2));
$parent_tree = array();
foreach ($custom_parent as $custom_child => $key) {
$parent_tree = taxonomy_get_tree($vid, $key->tid);
}
$children = $parent_tree;
}
$output = '<ul>';
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
}
I've then created a block and added :
<?php // $vid is the vocabulary id.
print themename_child_terms($vid = 1);
?>
This shows the child terms of the current term perfectly. However, it shows ALL terms that exist under the parent term, even if there is no piece of content using that term.
e.g.
viewing the page with all items in Term 1, I get
child 1
child 2
child 3
correctly listed in the block. But, if there are no pieces of content tagged with 'child 3' for example, it's still showing up that term in the block. This isn't very useful as it links to an empty term page.
How would I modify the code to only show children that actually have items associated with them. So if there are no children tagged 'Child 3', then that term would not show up. Is it an easy modification?
Thank you kindly for any solutions.
Nick
Working with Drupal 6
Thanks to user jerdiggity for posting the following reply over on drupal.stackexchange here. Works perfectly.
Hmm... I'd try something like this:
Change this part of your code:
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
to something like this:
// Avoid unnecessary "Invalid foreach" errors showing up in the log:
if (!empty($children)) {
// If not empty, run the foreach loop:
foreach ($children as $term) {
// Then check to see if any nodes exist for that term id:
$number_of_nodes = taxonomy_term_count_nodes($term->tid);
// If there ARE nodes...
if ($number_of_nodes > 0) {
// ... then add them to the output:
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
}
}
Hope that helps... :)

How to display WordPress RSS feed your website?

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.
I want to show only 3 post on my website.
I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
I want to show the complete title of my wordpress blogpost but specific letters of description.
Also the description should end up with a word not some piece of non-dictionary word ending with "..."
How this can be done, i have heard that it can be done through RSS.
Can somebody help me?
To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".
First we will make a script to trim the description and to put "..." in last:-
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Now we will define the variables in which we store the values:-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)
for ($i=0; $i<3; $i++)
{
$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}
?>
Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-
FIRST RECENT POST:
<?php echo $item_title[0]; ?>
<?php echo substrwords($item_description[0],70); ?>
SECOND RECENT POST:
<?php echo $item_title[1]; ?>
<?php echo substrwords($item_description[1],70); ?>
THIRD RECENT POST:
<?php echo $item_title[2]; ?>
<?php echo substrwords($item_description[2],70); ?>
Hope this can solve your problem. By the way Nice question.
Click here for the original documentation on displaying RSS feeds with PHP.
Django Anonymous's substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.
Full Code:
blog.php
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.
Example:
social.php
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
Also, this code is plug-n-play friendly.
Why not use the Wordpress REST API to retrieve posts -
API URL is : https://public-api.wordpress.com/rest/v1/sites/$site/posts/
where $site is the site id of your wordpress blog
or else simply use this plugin -
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html

Highlighting Word(s) Searched on in WordPress Search

I am using built-in WordPress search.php routines, is it possible to highlight the word searched on, in context with the search results retrieved?
For example, if I typed in "products", any page that returned this matching word would be highlighted to the user.
Thanks.
Here is a function you can add to functions.php that will highlight the searched term in the results.
/* Search Highlighting ********************************************/
// This highlights search terms in both titles, excerpts and content
function search_excerpt_highlight() {
$excerpt = get_the_excerpt();
$keys = implode('|', explode(' ', get_search_query()));
$excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt);
echo '<p>' . $excerpt . '</p>';
}
function search_title_highlight() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);
echo $title;
}
To use this function it has to be added to your archive loop:
<?php if (is_search() ) {
search_excerpt_highlight(); } ?>
Here is a few WP Plug-in that highlights search words using jQuery:
http://wordpress.org/extend/plugins/highlight-search-terms/
http://urbangiraffe.com/plugins/search-unleashed/
or do it yourself:
http://www.livexp.net/wordpress/highlight-the-search-terms-in-wordpress-search-results.html

Resources