Custom Wordpress Excerpt by two paragraphs - php

I found the answers to limiting the excerpts to only 2 paragraphs or more. However, the code I found only apply to the actual excerpt, not to custom excerpt that I made. I made two excerpts, one for certain posts on front page and another for the posts page. I wanted to add that code to the custom excerpt that I made for the posts page. How do I do that.
Here's my code that I created:
// Create the Custom Excerpts callback
function wpden_excerpt($length_callback = '', $more_callback = '')
{
global $post;
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
// Custom Length
function wpden_mag_len($length) {
return 200;
}
function wpden_more_view($more){
global $post;
return '... <a class="view-article" href="' . get_permalink($post->ID) . '">' . __('', 'wpden') . '</a>';
}
And I called it in my template_post.php:
<?php wpden_excerpt('wpden_mag_len','wpden_more_view'); ?>
The code in question that I wanted to use for my custom wpden_excerpt is either this:
add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
function my_custom_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
from this site or from the other stack overflow question
$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
// Here we choose how many paragraphs do we want to cutthe excerpt at, This part thanks to Clément Malet
$wpse0001_excerpt = "<p>$wpse0001_excerpt</p>";
$wanted_number_of_paragraph = 2;
$tmp = explode ('</p>', $wpse0001_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$wpse0001_excerpt = implode('</p>', $tmp_to_add) . '</p>';
$wpse0001_excerpt = str_replace(']]>', ']]>', $wpse0001_excerpt);
$excerpt_end = ' ' . ' » ' . sprintf(__( 'Read more about: %s »', 'pietergoosen' ), get_the_title()) . '';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;
return $wpse0001_excerpt;
}
return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt');
The thing is that those codes in question apply to the the_excerpt and override the custom excerpt that I made. I tried to format the code like this:
function wpden_excerpt($text = '')
{
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('#<script[^>]*?>.*?</script>#si', '', $text);
$text = strip_tags($text, '<p>');
$excerpt_length = 80;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}
and called it in the template as
<?php wpden_excerpt('text'); ?>
But it's not working. What was wrong and how do I fix it?
UPDATE
I still need help!!! I tried many different combo of custom excerpt and limiting the excerpt to one paragraph and I wasn't able to do so... Please help!!

Thanks to my brother's help, I was able to edit the code to show two paragraphs for a custom excerpt:
// Create the Custom Excerpts callback
function wpden_excerpt()
{
global $post;
$output = get_the_content();
$wanted_number_of_paragraph = 2;
$tmp = explode ('</p>', $output);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$output = implode('</p>', $tmp_to_add) . '</p>';
echo $output;
}
and call it in my template file as
<?php wpden_excerpt(); ?>

Related

Wordpress Sorting Tag list Alphabets first then numbers

I'm trying to make my list of tags, organized with first letter heads, to show Alphabet (a, b, c, d, etc..) first then numbers and symbols (#,1,2,3 etc..)
here is my code:
<?php $list = '';
$tags = get_tags();
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach ($tags as $tag) {
$first_letter = strtoupper( $firstLetter );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
usort($letter, 'myComparison');
$list .= "\n\t" . '</ul><h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
$list .= "\n\t" . '</ul><ul id="archiveEach">';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$link = $tag->link;
$list .= "\n\t\t" . '<li>'.$name.'</li>';
}}}}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';
print_r( $list);
?>
I've tried to sort using different functionalities, using this
function myComparison($a, $b){
if(is_numeric($a) && !is_numeric($b))
return 1;
else if(!is_numeric($a) && is_numeric($b))
return -1;
else
return ($a < $b) ? -1 : 1;
}
then calling the function using usort($differnentelements, 'myComparison')
I cannot get it to work -- any suggestions would be really greatly appreciated. thanks!

How to add a "Read more" button to my blog?

I have an HTML template which I convert to a Wordpress theme, but i can't add a "Read more" button to the home page.
I use the following code in functions.php:
function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
if (strlen($_GET['p']) > 0) {
echo "<p>";
echo $content;
echo "</p>";
}
else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
$content = substr($content, 0, $espacio);
$content = $content;
echo "<p>";
echo $content;
echo "...";
echo "</p>";
}
else {
echo "<p>";
echo $content;
echo "</p>";
}
}
I also use the following code in index.php:
<div class="text_home"><?php the_content_limit(300); ?></div>
What can I do to solve my problem?
//keep this code in function.php
function get_excerpt($limit=10, $content) {
$content = explode(' ', $content,$limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content);
$content = $content.' ...';
} else {
$content = implode(" ",$content);
}
return $content;
}
where you want to call this function
$content = get_the_content();
$content = strip_tags($content);
echo get_excerpt(15, $content);

Shuffle text inside HTML elements

I´m trying to shuffle articles with the PHP-shuffle function and with the Simple HTML Parser.
For example, this is my content:
<p>This is my article.</p>
<h2>This is a subheading</h2>
<p>This is an paragraph with an image inside it <img src="http://image.jpg">
</p>
The output should be something like this:
<p>article is This my .</p>
<h2>This subheading is a</h2>
<p>is an with an image paragraph inside This it <img src="http://image.jpg"></p>
However, using the Simple HTML DOM parser, I find it difficult to prevent the images from being shuffeled because they sometimes are placed inside paragraphs.
This is my current script. I feel like it is way to complicated, and it sometimes doesn't output the correct result.
Hopefully someone can help me.
$tags = 'p, ul, ol, blockquote, h1, h2, h3, h4, h5, h6, h7';
$html = str_get_html( $html );
/**
* Loop through HTML and set output
*/
foreach( $html->find( $tags ) as $article ) {
$element = $article->outertext;
$array = $article;
$tag = $article->tag;
$innerHTML = '';
// Nested paragraphs
foreach ( $array->find('p') as $el ){
$word_array = preg_replace( "#[\s]+#", " ", $el->innertext );
$words = explode( " ", $word_array );
$w = '';
shuffle( $words );
foreach ( $words as $word ){
$w .= $word . ' ';
}
$innerHTML .= $el->innertext = $w;
}
// List items
foreach ( $array->find('li') as $el ){
$word_array = preg_replace( "#[\s]+#", " ", $el->innertext );
$words = explode( " ", $word_array );
$w = '';
shuffle( $words );
foreach ( $words as $word ){
$w .= $word . ' ';
}
$innerHTML .= $el->innertext = '<li>' . $w . '</li>';
}
// Images
foreach ( $array->find('img') as $el ) {
// Blur image
$src = stripslashes( str_replace( '"', '', $el->src ) );
$new_src = $this->create_blur_image( $src );
// Replace url with base64 encode
$src = $el->src = $new_src;
$innerHTML .= $el->outertext;
}
// Output
if ( $innerHTML ){
$element = $article->innertext = $innerHTML;
} else {
$word_array = preg_replace( "#[\s]+#", " ", $article->innertext );
$words = explode( " ", $word_array );
$w = '';
shuffle( $words );
foreach ( $words as $word ){
$w .= $word . ' ';
}
$element = $article->innertext = $w;
}
$output .= $article->outertext;
}
$html = $output;
return $html;
Use:
function shuffle($text){
$text_array = array_shuffle(explode(" ",$text));
$text_string = implode($text_array," ");
return $text_string;
}

Adding custom content to wordpress posts, with variables such as $title not showing

I have used this code to add custom content to all my wordpress posts.
function add_after_post_content($content) {
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$content .= '<strong>'. $title . '</strong> is a <strong>wallpaper</strong> posted in the ' . $cat_name . ' category.';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
The problem is that the post title and category are not showing so all I am getting is basically "is a wallpaper posted in the category".
How could I modify the code so that the post title and category are pulled to be added to the description?
Here is the code which works for some posts created using a specific plugin, but I would like it globalized to all posts on the site
//Create Post
$user_id = get_current_user_id();
$imagePoster->dirName = time();
$wp_upload_dir = wp_upload_dir();
if(!empty($_FILES["file"]["tmp_name"]))
{
$filename = $_FILES["file"]["tmp_name"];
$originalFilename = $_FILES["file"]["name"];
$zipMessage = $imagePoster->unzip($filename , $originalFilename);
$images = $imagePoster->iterateDir($wp_upload_dir['basedir'].'/bulkimages-'.$imagePoster->dirName.'/');
}
else
{
$filename = $_POST['manualfile'];
$images = $imagePoster->iterateDir($wp_upload_dir['basedir'].'/'.$filename.'/');
$zipMessage = '';
}
$postCount = 0;
$titleExploded = explode(",", $titleList);
$linkExploded = explode(",", $linkList);
$initialInterval = $statusSplit[1];
$interval = $statusSplit[1];
foreach($images as $image)
{
if(get_option('create-posts-from-images-useimagename') == true)
{
if(get_option('create-posts-from-images-delimiter') != '')
{
$path_parts = pathinfo($image->getFilename());
$title = str_replace(get_option('create-posts-from-images-delimiter')," ",$path_parts['filename'] );
}
else
{
$path_parts = pathinfo($image->getFilename());
$title = $path_parts['filename'];
}
}
else
{
$title = $imagePoster->loopTitles($titleExploded, $postCount );
}
$link = $imagePoster->loopTitles($linkExploded, $postCount );
$cat_name = get_cat_name( $category );
$content = '<strong>'. $title . '</strong> is a <strong>Wallpaper</strong> posted in the ' . $cat_name . ' category.<br /><br />';
It's been a while since I worked in wordpress but try var dumping out the following in the shortcode.
$GLOBALS['post']
You should be able to get what you want doing something like...
$GLOBALS['post']->post_name
to test this in your code above do the following.
function add_after_post_content($content) {
global $post;
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$post_categories = wp_get_post_categories( $post->ID );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
foreach($cats as $c){
$content .= $c['name'];
}
$content .= 'categories';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');

Modify function to include paragraphs

I have this excerpt function that cuts the content after the first paragraph (after the first </p> tag.
I need to modify this so I can cut the content after the second or third paragraph, that is the second or third </p> tag. My knowledge of php is not great, so any help will be appreciated.
Here is my function
if ( ! function_exists( 'wpse0001_custom_wp_trim_excerpt' ) ) :
function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
global $post;
$raw_excerpt = $wpse0001_excerpt;
if ( '' == $wpse0001_excerpt ) {
$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );
$wpse0001_excerpt = str_replace(']]>', ']]>', $wpse0001_excerpt);
$excerpt_end = ' ' . ' » ' . sprintf(__( 'Read more about: %s »', 'pietergoosen' ), get_the_title()) . '';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;
return $wpse0001_excerpt;
}
return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt');
EDIT
This is the section of code I need to modify, but not sure how
$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );
$wpse0001_excerpt = "<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p>";
$wanted_number_of_paragraph = 3;
$tmp = explode ('</p>', $wpse0001_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$wpse0001_excerpt = implode('</p>', $tmp_to_add) . '</p>';
echo $wpse0001_excerpt;
// Output : <p>1</p><p>2</p><p>3</p>

Categories