Displaying post images only - php

I am trying to put just the images that are in my Wordpress post on screen. I have the following code which removes them and posts just the text
<?php
$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
I'm basically trying to do the same thing but pull out and post just the images. These code(s) would appear on the same page so I'm not sure if it's possible to write one function to 'separate' the images and text and echo them n different divs.

Related

PHP: Explode array, include all tags (not just paragraph)

I have the following code that takes the content from the Wordpress post and injects an image between each count of a paragraph - this works well.
It does not take into account heading or image tags etc. How can I modify this so it does take these other tags into the count?
<?php
$content = apply_filters( 'the_content', get_the_content() );
$content_table = explode("<p>", $content);
$content_table[3] .= $spot1;
$content_table[6] .= $spot2;
$content_table[9] .= $spot3;
$content_table[12] .= $spot4;
$content_table[15] .= $spot5;
$content2 = implode($content_table, "<p>");
echo $content2;
?>

Seperating post text and images in different locations on the same page

Using the following code to remove images (and paste just the text)
<?php
$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Trying to bring in the images (in a new area of the same page) I a trying to use
preg_match('#(<img.*?>)#', $content, $results);
To no avail. And I am also wondering if there is a better way than to run a function twice, or If I can separate the text and image and call them into two different divs/locations
Get Only text Based content
function custom_strip_image($text) {
$text = preg_replace("/<img[^>]+\>/i", "", $text);
return $text;
}
echo custom_strip_image(get_the_content());
Make second function for get images list

removing block quote from post text - Wordpress

I have a Wordpress site and I want to remove the block quotes from the post and put just the regular text. (also want to remove any images that are in the text, I just want the regular text)
This code does the OPPOSITE of what I want - takes out the block quotes and posts that. I want it to post the other text and NOT the block quote.
<?php
// get the content
$block = get_the_content();
// check and retrieve blockquote
if(preg_match('~<blockquote>([\s\S]+?)</blockquote>~', $block, $matches))
// output blockquote
echo $matches[1];
?>
What you need is a content filter. Add the following to your functions.php file
add_filter( 'the_content', 'rm_quotes_and_images' );
function rm_quotes_and_images($content)
{
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = preg_replace("/<img[^>]+>/i", "", $content);
return $content;
}
Try this
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
return $content;
}
Just add this to your code:
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
echo $content;
The way wali hassan sayed is to add following code to your function.php:
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
return $content;
}
This overrides the default "the_content()" function so in you page template you only need to call:
the_content();

Explode twice doesn't index properly

I'm using the Simple HTML DOM Parser to retrieve a specific div from a website. I remove the part of the div that I don't want by using explode(). I then want to explode the kept part into a new array, but for some reason it doesn't get indexed as intended.
Why doesn't my last row with "echo $content[0];" print "Overall" while "echo $content[5];" does, when Overall is the first string? How do I fix this?
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur');
$content = $html->find('div[id=contentHiscores]', 0)->plaintext;
echo $content;
echo "<br><br><br><br>";
$content = explode("SkillRankLevelXP", $content);
$content = $content[1];
echo $content;
echo "<br><br><br><br>";
$content = explode(" ", $content);
echo $content[0];
?>
Between SkillRankLevelXP and Overall, there are 6 spaces, though the browser only shows it as 1. Use the "View Source" menu and you'll see what I mean.
You can use some RegEx to replace 2 or more spaces with just 1 space, and I think that will get you closer to what you want.
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur');
$content = $html->find('div[id=contentHiscores]', 0)->plaintext;
$content=preg_replace('/ {2,}/', ' ', trim($content));
$content = explode('SkillRankLevelXP ', $content);
$content = $content[1];
$content = explode(' ', $content);
print_r($content);
?>

Undoing a strip_tags in php

I am trying to customize a Word press template and having some trouble. The page is http://rexonmedia.com/?page_id=113 and under each section there are description, all the text in description is plain text(Actionscript, Visit site ...). I want the text to be formatted or have links like the source (http://rexonmedia.com/?portfolio=audiomaya) with hyperlink.
I came to find out that the culprit here is "strip_tags" tags. It's used on two different places. I tried couple of scenarios but didn't work. Please help
public function customFormat($content,$strip_tags = false,$shortcode=true){
$content = stripslashes($content);
if($shortcode)
$content = do_shortcode( shortcode_unautop( $content ) );
$content = preg_replace('#^<\/p>|^<br\s?\/?>|<p>$|<p>\s*( )?\s*<\/p>#', '', $content);
if($strip_tags)
$content = strip_tags($content,"<hades>,<tabend>");
return $content;
}
<?php
global $more; // Declare global $more (before the loop).
$more = 1;
$content = get_the_content('');
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$this->shortenContent( $content_limit , strip_tags( $content ) );
?>
Supply strip_tags with the allowable tags. The line in the first block of code would change to:
$content = strip_tags($content,"<strong><a>");
And the line in the second block of code would change to:
$this->shortenContent( $content_limit , strip_tags( $content, "<strong><a>" ) );
By the way, the function defines $strip_tags as false in the function definition if it is not supplied. Check that!
public function customFormat($content,$strip_tags = false,$shortcode=true){

Categories