Wordpress 'wp_get_archives()' as raw text? - wordpress

in a special case I need the output of wp_get_archives(); to be just raw text instead of a link. Does anyone know how to do that? I recently checked the reference but also the 'format'=>'custom' option renders a link.
Thanks in advance!
---- Edit (#vard) ----
<?php
$args2 = array(type'=>'monthly','limit'=>'','format'=>'html','before'=>'','after'=>'','show_post_count'=> false,'echo'=>1,'order'=>'DESC');
echo "<ul>";
wp_get_archives( $args );
echo "</ul>";
?>
and code from zurb.com:
add_filter( "get_archives_link", "customarchives_link");
function customarchives_link( $x ){
$url = preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)#i', $x, $matches);
return $matches[4] == $_SERVER['REQUEST_URI'] ? preg_replace('#<li#', '<li class="current_page_item"', $x) : $x;
}

Related

How to add a code before and after "the_content()" in wordpress

i want my output to be like this -
echo 'codeBefore';
the_content();
echo 'codeAfter';
Problem is, one of the plugin i use inserts its code before and after the content & output i get is something like this.
echo 'codeBefore';
<plugin code>
the_content();
<plugin code>
echo 'codeAfter';
How to get my code displayed in front before any other plugin codes ?
User below Code
function slideshow_fun( $content ) {
if ( is_single() ) {
$custom_content = '[plugin_shortcode]';
$custom_content .= $content;
return $custom_content;
} else {
return $content;
}
}
add_filter( 'the_content', 'slideshow_fun' );
More detail and Credit

Adding "Add Friend Button" anywhere in the buddypress site

I'm trying to place an "Add Friend" button outside of the members page, inside a loop, so the button appears next to the author's avatar in a post.
I tried adding ;
<?php if ( bp_is_active( 'friends' ) ) { ?>
<?php bp_add_friend_button( $user_ids['users'][$i]->id ) ?>
<?php } ?>
Inside the loop but it didn't return any results.
Then I placed the following action hook in functions.php, it displayed the button inside the loop but once clicked, all the buttons in the post list got clicked as well.
add_action( ‘the_content’, ‘bp_add_friend_button’, 5 );
So now I'm stuck. I thought, adding a template tag inside the template would work since it had worked for "Send Message" button.
try
<?php if ( function_exists( 'bp_add_friend_button' ) ) : ?>
<?php bp_add_friend_button() ?>
<?php endif; ?>
Here is an example that works for me, hope its helps.
$id_or_email = $result -> user_ID;
$avatar_default = get_avatar($id_or_email, $size, $default = '', $alt = '', $args = null);
$potential_friend_id = $id_or_email;
$friend_status = false;
$friend_button = bp_add_friend_button($potential_friend_id, $friend_status);
echo '<li>'. $avatar_default. $user_info->user_nicename . $friend_button . '</li> ';
There may be a plugin that solve this, but to followup on Rafa's solution.
This complete code worked for me.
In bp-custom.php ( this is a file you need to create)
function my_id_text_processor($friend_id)
{
//Return string id without 'uid-' attached
$search = 'uid-';
$replace_with = '';
return str_replace($search, $replace_with, $friend_id);
}
In the file you want the button to appear put this code.
< ?php $friend_id = my_id_text_processor(bp_get_group_invite_item_id()) ? >
$avatar_default = get_avatar($friend_id, $size, $default = '', $alt = '', $args = null);
$friend_status = false;
$friend_button = bp_add_friend_button($friend_id, $friend_status);
echo '<li>'. $avatar_default,bp_group_invite_user_link(),$friend_button . '</li> ';

wp_trim_words not working on anything but ordinary strings

I´m a bit out of my field here and I´m confused about this. Using wp_trim_field doesn't work for me except for regular strings.
This does not work, it returns the whole text:
<?php
$field = the_field('project_description');
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
This however does work:
<?php
$field = 'this text does get trimmed';
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
Echoing out the $field instead does echo out the text that I am trying to trim, but the trimming aint working. Any ideas as to why?
edit - I also tried this, same thing happens:
<?php
$length = 1;
$text = the_field('project_description');
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;
?>
use var_dump($field); wp_trim_words( $field,....) $field must be string type ...check if this is or not to test the datatype, if its not im sure you know what to do then.
Use typecast if its not.
You'll need to change the $field variable to this: $field = get_field('project_description');
the_field(); outputs the content, while get_field(); retrieves it. In order to pass it through a function, you'll need to retrieve it.
ACF documentation page that answers this question: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

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

Can WordPress WP_Query return custom field post meta?

For some reason, I cannot get WP_Query to return the custom field values of posts. I can get the post thumbnails using get_the_post_thumbnail($post->ID, array(50,50)), but I cannot get the custom field data using get_post_meta($post->ID, $key, true).
A stripped-down version of what I'm trying to do:
<?php
$keys = array('Show Date','Birth Year','Origin');
echo '<table>';
echo '<tr><th>Title</th>';
foreach( $keys as $key ) {
echo '<th>' . $key . '<th>';
}
echo '</tr>';
$myquery = new WP_Query( 'post_type=post' );
if( $myquery->have_posts() ) : while( $myquery->have_posts() ) : $myquery->the_post();
$title = get_the_title();
echo '<tr><td>' . $title . '</td>';
$values = array();
foreach( $keys as $key ) {
$values[] = get_post_meta($post->ID, $key, true);
}
foreach( $values as $value ) {
echo '<td>';
echo $value;
echo '</td>';
}
echo '</tr>';
endwhile; endif;
echo '</table>';
?>
Also available here:
http://pastebin.com/at8S2THs
Even with all non-essential code removed, I cannot make this work. I know you can use parameters like meta_key and meta_value in a query to narrow down the results, but I just want to display all values for the keys I specify, if they exist, for each post.
Any help would be greatly appreciated...
** SOLUTION FOUND **
Just needed to add global $post; after the start of the loop. Thanks to #Kimikaze on the WP support forum for providing the solution!
When I can't find the data I need in wordpress, I always find it helpful to print the Global $post object to screen, so I can see if my data is making it to the page.
Global $post;
echo "<pre>";
print_r($post);
echo "</pre>";
All the helper methods or "hooks" are usually just interacting with this (or another) global object. Check the output of this on $post (and maybe your $values array) for data you're looking for and if it's there, you can just reffer to it directly, like the post title for example
$post->title
Another thought, The third parameter of get_post_meta() makes it return a single string when set to true, what do you get when it's set to false?
I'm guessing the problem has to do with the values you're using as meta keys. To retrieve the data with get_post_meta() you'll want to pass in the actual meta_key values, which are probably 'show_date', 'birth_year', and 'origin' since the meta_key value doesn't handle uppercase letters or spaces. Try changing to those values where you're setting the $keys array:
$keys = array('show_date','birth_year','origin');
If that doesn't work, it might be worth checking in the database (in the wp_postmeta table) to confirm the actual meta_key values.

Resources