I'm trying to create, in addition to the state and country arrays, a list for cities.
I have seen everywhere but I can not quite understand how and if it is possible to extract it.
I read of this: get_city(); but it leads to the billing data already entered by the user. I need a possible list of the cities already present.
I tried to follow the logic with which I extracted the rest
$countries = WC()->countries;
$countries_array = WC()->countries->get_allowed_countries();
$country_states_array = WC()->countries->get_allowed_country_states();
foreach($countries_array as $k => $v)
{
echo '<option value="'.$k.'">'.$v.'</option>';
}
echo '<div class="space[15]"><hr /></div>';
foreach ($country_states_array as $item)
{
echo "<div style='border: 1px solid blue;'>";
foreach ($item as $v)
{
echo " ".$v;
}
echo "</div>";
}
echo '<div class="space[15]"><hr /></div>';
//NOT WORKING.... ok....
$country_city = WC()->countries->get_allowed_city(); //does not exist
foreach ($country_city as $c)
{
echo "<div style='border: 1px solid red;'>";
echo " ".$c;
echo "</div>";
}
but it does not work.
does this list of cities exist or am I imagining it?
Related
I'm trying to create 3 rows of buttons to select. Think sorting boxes. AT&T has it on this page. Where you see iPhone, Samsung and then sorting. I'm trying to do the same thing. AT&T's example page
The first row will be sub categories of parent categories. That part I was successful in completing. However, I'm struggling to call to all the parent attributes.
<?php
global $product;
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
$product_attributes = array_keys($product_attribute);
$count = 0;
echo '<a class="archive-attributes" href="#">';
foreach ($product_attributes as $product_attribute){
$attribute_name = ucfirst( str_replace('pa_', '',
$product_attributes) );
echo $attribute_name . '';
if($count != count($product_attribute)) echo '';
else echo '';
}
echo '</a>';
}
?>
I only need the parent for this row. When one of these is clicked the children of that attribute will display below it. What am I doing wrong?
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);
}
I am using a script found at http://wpdevsnippets.com/add-category-tag-taxonomy-picture/. However, I cannot get the writer to answer a usage question, so here it is:
How do I use this script with a custom taxonomy on a page.php or archive.php template?
add_action('admin_head', 'wpds_admin_head');
add_action('edit_term', 'wpds_save_tax_pic');
add_action('create_term', 'wpds_save_tax_pic');
function wpds_admin_head() {
$taxonomies = get_taxonomies();
//$taxonomies = array('category'); // uncomment and specify particular taxonomies you want to add image feature.
if (is_array($taxonomies)) {
foreach ($taxonomies as $z_taxonomy) {
add_action($z_taxonomy . '_add_form_fields', 'wpds_tax_field');
add_action($z_taxonomy . '_edit_form_fields', 'wpds_tax_field');
}
}
}
// add image field in add form
function wpds_tax_field($taxonomy) {
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox');
if(empty($taxonomy)) {
echo '<div class="form-field">
<label for="wpds_tax_pic">Picture</label>
<input type="text" name="wpds_tax_pic" id="wpds_tax_pic" value="" />
</div>';
}
else{
$wpds_tax_pic_url = get_option('wpds_tax_pic' . $taxonomy->term_id);
echo '<tr class="form-field">
<th scope="row" valign="top"><label for="wpds_tax_pic">Picture</label></th>
<td><input type="text" name="wpds_tax_pic" id="wpds_tax_pic" value="' . $wpds_tax_pic_url . '" /><br />';
if(!empty($wpds_tax_pic_url))
echo '<img src="'.$wpds_tax_pic_url.'" style="max-width:200px;border: 1px solid #ccc;padding: 5px;box-shadow: 5px 5px 10px #ccc;margin-top: 10px;" >';
echo '</td></tr>';
}
echo '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#wpds_tax_pic").click(function() {
tb_show("", "media-upload.php?type=image&TB_iframe=true");
return false;
});
window.send_to_editor = function(html) {
jQuery("#wpds_tax_pic").val( jQuery("img",html).attr("src") );
tb_remove();
}
});
</script>';
}
// save our taxonomy image while edit or save term
function wpds_save_tax_pic($term_id) {
if (isset($_POST['wpds_tax_pic']))
update_option('wpds_tax_pic' . $term_id, $_POST['wpds_tax_pic']);
}
// output taxonomy image url for the given term_id (NULL by default)
function wpds_tax_pic_url($term_id = NULL) {
if ($term_id)
return get_option('wpds_tax_pic' . $term_id);
elseif (is_category())
return get_option('wpds_tax_pic' . get_query_var('cat')) ;
elseif (is_tax()) {
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
return get_option('wpds_tax_pic' . $current_term->term_id);
}
}
And here is the function that is used to call this:
wpds_tax_pic_url();
or
wpds_tax_pic_url($category->cat_ID);
How do I use this script with a custom taxonomy on a page.php or archive.php template?
On archive.php, you can use the wpds_tax_pic_url(); without any parameters it should display the URL of the attached image.
if(wpds_tax_pic_url()){
echo '<img src="'.wpds_tax_pic_url().'" />';
}else{
// other image or nothing.
}
On page.php or post.php, something following would work
$terms = wp_get_post_terms($post_id, 'custom_taxonomy');
foreach ($terms as $t) {
if(wpds_tax_pic_url($t->term_id))
echo '<img src="'.wpds_tax_pic_url($t->term_id).'" />';
}
I've created an array from three variables I get through a foreach loop.
//loop through each post
foreach($loop as $p){
//get the meta and taxonomy data
$term_list = get_the_term_list($p, "mountains",true);
$name = trim($term_list, "1");
$wtr_long = get_post_meta($p,"wtr_longitude",true);
$wtr_lat = get_post_meta($p,"wtr_latitude",true);
//Add to Array
$map_array[] = array ($name => $wtr_lat . "|" . $wtr_long);
}
Now this array should then deliver the data that will populate my Google Map, for that I'm using below code.
foreach( $map_array as $a){
foreach ($a as $key => $value) {
$pieces = explode("|", $value);
$trimmed_key = trim($key, "1");
$name = trim($trimmed_key);
?>
{latitude: <?php echo $pieces[0]; ?>,
longitude: <?php echo $pieces[1]; ?>,
html: <?php echo $name; ?>},
<?php }} ?>
This works almost fine (although it's probably not the cleanest code, tips on this would be appreciated too). The issue I'm having is that in the last iteration of the foreach inside the other foreach, I need to add a ], resulting in:
html: <?php echo $name; ?>}],
I can escape a single foreach, but doing it on a foreach inside a foreach is driving me insane. Any help would be greatly appreciated.
I'm not a php develeopor but you may want to build the collection of strings you want to echo first. After the collection is complete you can add the ']' the the last element. Then echo each element in your collection of strings.
atbyrd, your approach gave me a couple new issues, but did in the end lead to a working solution. Thank you for that.
Below the code that fixed the issue, hope it'll be helpful to someone else in the future.
//Add to Array
$map_string .= '{latitude: "' . $wtr_lat . '", longitude: "' . $wtr_long . '", html: "' . $name .'"},~!~';
//$map_string .= '{latitude: "' . $wtr_lat . '", longitude: "' . $wtr_long . '", html: "name"},~!~';
}
//Remove last three "~!~" characters from the string not to have one too many arrays when exploding
$clean_map_string = substr($map_string, 0, -3);
//Now I can explore on ~!~ to get a new array with the correct strings
$map_array = explode('~!~', $clean_map_string);
$i = 0;
$length = count($map_array)-1;
//Inserts all the markers
foreach($map_array as $value){
if( $i != $length ){
echo $value;
}
else {
echo str_replace("},", "}],", $value);
}
$i++;
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