Extract certain section of posts as an excerpt in WordPress - wordpress

I have a WordPress website, in which I have created multiple posts and bound them to a certain category.
Each posts has 6 paragraphs.
Here is what I am trying to achieve:
I have created a child wordpress theme file, in which I am retrieving all the posts from a particular category.
I want to display the 3rd paragraph from each posts ( upto 40 words ) as an excerpt.
Anyone can help me to achieve this?
Any help will be appreciated...
Thank You
Edited:
Contents of one of the posts:
<div id="grey">
<div id="title">
<h1 id="divtest">Title</h1>
</div>
<div id="divContentText1"><span class="button5_hover"></span><strong>Back To Test Page</strong></div>
</div>
<div class="wrap" id="divPgInfo">
<div id="divInnerImgSliderWrap">[meteor_slideshow slideshow="test"]</div>
<div id="divImgTxtWrap">
<h4 class="innerSlideHd">Test Title</h4>
<p class="innerSlideText">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div class="divClr"></div>
</div>
<div class="divClr"></div>
Now I want to show the contents from the sentence "It is a long established...."
Any help will be appreciated....
Really Thank you...

In You Post Editor screen at wordpress backed
Write your 3rd paragraph in "excerpt" text area. (Enable it from top of edit screen page)
For display this content in fronend use the_excerpt function.

Inspired by this question and by the fact that Wordpress' excerpt is often not flexible enough for user needs, I jotted down a function allowing to use any HTML tag inside the post content as this arbitrary post excerpt.
Mind that the function can get some work to improve flexibility, for example you could substitute the excerpt with this custom excerpt altogether, or perhaps implement some other parameter to allow a more in-depth searching (something like marking the excerpt paragraph with an id="excerpt").
I went for a specific solution to your problem, however, and elaborated a bit more to allow some flexibility. You can put this code in your functions.php:
/*
* Defines an arbitrary excerpt by using any post content HTML element
*
* The function accepts an optional array of arguments.
* Optional $args contents:
*
* * id - The id of the post we want the excerpt of. Defaults to the current post.
* * tag - The HTML tag we want to use as an excerpt.
* * idx - The index of that tag as calculated considering the post_content as the root.
*
* #param array|string $args See above description.
*/
function my_get_the_excerpt($args = array()) {
$defaults = array(
'id' => null,
'tag' => 'p',
'idx' => 0
);
$args = wp_parse_args($args, $defaults);
$args = (object) $args;
$post = get_post($args->id);
if ( post_password_required($post) ) {
return __( 'There is no excerpt because this is a protected post.' );
}
$post_content = '<html>' . apply_filters('the_content', $post->post_content) . '</html>';
$post_html = new DOMDocument();
$post_html->loadHTML($post_content);
$paragraphs = $post_html->getElementsByTagName($args->tag);
return $post_html->saveHTML($paragraphs->item($index));
}
After doing that, in your case you could solve your problem by simply using this function in your template, instead of the_excerpt. Like so:
// This gets the third paragraph of your post.
echo my_get_the_excerpt( array( 'idx' => 2 ) )

Put a specific ID on the tag of the paragraph you want to use as an excerpt and when you want to use the excerpt just parse the content of the article and extract only the part in your with the id you've determined.
check this : Get content within a html tag using php and replace it after processing

Related

Wordpress & child themes - strategies to find out which .php to override?

Starting with some basic Wordpress Bootstrap Themes I'd like to modify the width for Posts and Post links. This is more get-to-know-WP than heavy duty theme creation - I am mostly interested in how to apply minor customizations like this across different themes. I know each theme can kinda pick which PHP files it wants to provide, so I know what works in Theme A won't necessarily work in Theme B.
I've already done this once for one theme, but now the other theme doesn't have the same PHP file names.
For example, I know that to make the widget area narrower for posts, I want to override sidebar.php:
<!-- <div id="secondary" class="widget-area col-md-4" role="complementary"> -->
<div id="secondary" class="widget-area col-lg-2" role="complementary">
And modifying index.php makes the left-side wider:
<!-- <section id="primary" class="content-area col-md-8"> -->
<section id="primary" class="content-area col-lg-10">
<div class="tmarker">index.php</div> <!-- I added this! -->
This works fine when I am at the root of my site, as index.php is getting used.
However, when I navigate to a Post, I am back to:
<div id="primary" class="content-area col-md-8">
and my post area hasn't expanded. This makes sense - index.php isn't used here.
How do I know which PHP file is getting composed into that post detail view so that I can override it?
I've tried looking for 'index.php' in the page source, but I only saw the one I added myself on my child override.
I guess I could just look at all the PHPs in the editor and find all the class="content-area col-md-8" and trial and error them till I hit the right one. Not much fun. Are there better ways?
Could I at least dump all of those PHPs to files and grep for col-md-8? I'd need a text export for themes.
P.S. I know Wordpress child customizations are best done in style.css and functions.php, but unless I missed something, with Bootstrap, I think I really need to hit those class assignments for Bootstrap to do the work instead.
The question here addresses something similar.
You can store the template name into a global variable and echo it into the footer. This should tell you what template file is being loaded (index.php, page.php, etc).
The code below should be added to functions.php.
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
add_action( 'wp_footer', 'get_current_template' );
function get_current_template() {
if( !isset( $GLOBALS['current_theme_template'] ) )
return false;
echo $GLOBALS['current_theme_template'];
}
There's lots of tips and strategies, but one that is a "A quick-and-dirty way":
Most decent themes add a class to the body element that reflects which theme template is being used. If you inspect the body element, you should be able to see which template is being used (for example page-template-default, etc).
If your theme breaks down further than simple template levels, then you'll have to start examining the code in the given template(s) to figure out which code is running, etc.

Wordpress remove <p> tag those are auto generated

i am trying to remove <P> tag those wordpress are generating automatically.
I have google for that and get solution like remove filter
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
But by using this its removing all <p> tag those i have already used in page content.
For example :
When i am putting plain text in page content like
This is example.
Wordpress wrapping these plain text in <p> tag like <P>This is example.</p>
How can i stop this adding <p> tag rather than removing all <p> tag.
Try this one it will remove extra <p> for a particulate page
http://wordpress.org/plugins/ps-disable-auto-formatting/
after configuring this plugin you get an option like this, showing on example site
http://www.wordpress-plugin.net/remove-automatic-p-paragraph-in-wordpress/
Use strip_tags() function, this will help you
One of the good plugin in wordpress is :
Row Html
Features of this plugin is :
Disable wptexturize (the function that creates smart quotes and other
typographic characters).
Disable automatic paragraph creation.
Disable image smilies.
Disable convert_chars (the function that converts ampersands to HTML
entities and "fixes" some Unicode characters).

wordpress add tags as p all time my code

I use a slider called revoslider and use as other sliders shortcodes for show this elements
When I use the shortcode without using the visual editor to insert and save, the slider doesn't works because WordPress adds <p> tags into the javascript:
<p> <script type="text/javascript"></p>
<p> var tpj=jQuery;</p>
<p> tpj.noConflict();</p>
<p> var revapi1;</p>
<p> tpj(document).ready(function() {</p>
<p> if (tpj.fn.cssOriginal != undefined)
tpj.fn.css = tpj.fn.cssOriginal;</p>
<p> if(tpj('#rev_slider_1_1').revolution == undefined)
revslider_showDoubleJqueryError('#rev_slider_1_1');
else
revapi1 = tpj('#rev_slider_1_1').show().revolution(
{
delay:9000,
startwidth:960,
startheight:350,
hideThumbs:200,</p>
<p> thumbWidth:100,
thumbHeight:50,
thumbAmount:2,</p>
<p> navigationType:"bullet",
navigationArrows:"solo",
navigationStyle:"round",</p>
<p> touchenabled:"on",
onHoverStop:"on",</p>
<p> navigationHAlign:"center",
navigationVAlign:"bottom",
navigationHOffset:0,
navigationVOffset:20,</p>
<p> soloArrowLeftHalign:"left",
soloArrowLeftValign:"center",
soloArrowLeftHOffset:20,
soloArrowLeftVOffset:0,</p>
<p> soloArrowRightHalign:"right",
soloArrowRightValign:"center",
soloArrowRightHOffset:20,
soloArrowRightVOffset:0,</p>
<p> shadow:2,
fullWidth:"off",</p>
<p> stopLoop:"off",
stopAfterLoops:-1,
stopAtSlide:-1,</p>
<p> shuffle:"off",</p>
<p> hideSliderAtLimit:0,
hideCaptionAtLimit:0,
hideAllCaptionAtLilmit:0,
startWithSlide:0
});</p>
<p> }); //ready</p>
<p> </script></p>
Because of this the code never works and I don't understand why WordPress adds these <p> for each line, it's ridiculous
I tried add_filter for content it still doesn't work.
Have you seen this topic on wp?
http://wordpress.org/support/topic/shortcode-is-being-surrounded-by-p-tags
seems to be a problem with nested shortcodes .. If it is your Issue at all?
Do you have a link to the slider you are using?
I had the same problem once, and add_filter( 'the_content', 'wpautop') does not work on my theme. So what I did was the following:
In the revo slider admin area, select the slider that is not displaying well.
Look for the troubleshooting tab (Bottom right) then change the values
Jquery No Conflict Mode = ON
Put JS Includes To Body = FALSE
(Important part) Output Filters Protection = By Compressing Output
That way, the script will be just in one line therefore the auto paragraph filter will just add the p tag to a single line
When editing a slider, in the Troubleshooting section, you've got an option called Output Filters Protection. Set it to By Echo Output, and revslider's shortcode function will bypass filters, including the faulty one that adds this <p> tags... It does that by outputting (echo) its content directly instead of returning it to wordpress.
Have you tried surrounding your code with <div></div> (without using any class or id)? It prevents Wordpress from surrounding text with <p></p> tags. I've used it to prevent <p></p> tags appearing around images, as described in this topic over at Wordpress.org.
For those who might have had a similar issue, what worked for me was the following:
Since I concluded the issue was within my theme, I had spent some time looking for the issue and found the one line causing the issue:
add_filter( 'the_content', 'do_shortcode', 7 );
I just commented it out and it sorted out the problem.

P text added to html text

I know there are a lot of topics on this, and I've already looked at all them and none of the solutions there apply to me.
I've put a shortcode to run a jscript for a responsive slider in the 'text' side of my page editor. Yet when I load the page, the source code has tons of paragraph tags after every line of the javascript. It even has a paragraph tag before it even asks for the content itself.
I've tried editing the functions.php file of my theme (Reason 2.0), but I'm not sure I could mark it up correctly, it's very php-heavy. I've also tried five of the plugins suggested here. None of them have any effect.
The code is horrendous, and looks like this:
<p> <!-- START REVOLUTION SLIDER --></p>
<div id="rev_slider_1_1_wrapper" class="rev_slider_wrapper" style="margin:0px auto;background-color:#E9E9E9;padding:0px;margin-top:0px;margin-bottom:0px;">
<div id="rev_slider_1_1" class="rev_slider" style="display:none;">
<ul>
<li data-transition="slidehorizontal" data-slotamount="5" data-masterspeed="300" data-link="http://www.secondhandculture.org/mad-men-and-attraction" >
<img src="http://www.secondhandculture.org/wp-admin/admin-ajax.php?action=revslider_show_image&img=uploads%2F2012%2F12%2Fmad-men-image.jpg&h=300&t=exact" ></p>
<div class="caption sft"<br />
data-x=”400″<br />
data-y=”20″<br />
data-speed=”300″<br />
data-start=”200″<br />
data-easing=”easeOutExpo”><img src="http://www.secondhandculture.org/wp-content/uploads/2012/12/Mad-men-text.png" alt="Mad Men"></div>
<div class="caption big_white sfr"<br />
data-x=”550″<br />
data-y=”140″<br />
data-speed=”300″<br />
data-start=”500″<br />
data-easing=”easeOutExpo”>How it makes us love <br><br />
what we know we should hate, <br><br />
and asks us why</div>
</li>
</ul>
<div class="tp-bannertimer tp-bottom"></div>
</p>
</div>
</div>
<p> <script type="text/javascript"></p>
<p> var tpj=jQuery;</p>
<p> tpj.noConflict();</p>
<p> tpj(document).ready(function() {</p>
<p> if (tpj.fn.cssOriginal != undefined)
tpj.fn.css = tpj.fn.cssOriginal;</p>
<p> var revapi1 = tpj('#rev_slider_1_1').show().revolution(
{
delay:9000,
startwidth:,
startheight:300,
hideThumbs:200,</p>
<p> thumbWidth:100,
thumbHeight:50,
thumbAmount:1,</p>
<p> navigationType:"none",
navigationArrows:"nexttobullets",
navigationStyle:"round",</p>
<p> touchenabled:"on",
onHoverStop:"on",</p>
<p> navOffsetHorizontal:0,
navOffsetVertical:20,</p>
<p> shadow:2,
fullWidth:"off",</p>
<p> stopLoop:"off",
stopAfterLoops:-1,
stopAtSlide:-1,</p>
<p> shuffle:"off"
});</p>
<p> }); //ready</p>
<p> </script></p>
<p> <!-- END REVOLUTION SLIDER --></p>
I had the same problem once, and add_filter( 'the_content', 'wpautop') does not work on my theme. So what I did was the following:
In the revo slider admin area, select the slider that is not displaying well.
Look for the troubleshooting tab (Bottom right) then change the values
Jquery No Conflict Mode = ON
Put JS Includes To Body = FALSE
(Important part) Output Filters Protection = By Compressing Output
That way, the script will be just in one line therefore the auto paragraph filter will just add the p tag to a single line
A common reason for this is that the theme author changed the priority of the output filters in the theme or even replaced the default ones with his/her own. So, when you insert the Revolution Slider shortcode, paragraph tags are inserter surrounding each line of output, breaking the Javascript code. The usual message in the Firefox error console is:
Error: SyntaxError: syntax error
Source Code:
</p>
This is a theme problem the theme author should fix. If you want to try to fix it yourself, look for a line like:
add_filter( 'the_content', 'wpautop' , 99);
and change the priority (the 99) of the filter to something like 9. That may help but also break something else. In some themes the code is not exactly that but you can look for a function adding or removing filters from the shortcodes output. You can see examples in the pages I linked below.
However, before doing that try this: recent versions of Revolution Slider have an option to avoid being filtered. When you create a slider, in the Troubleshooting box, there is an option named "Output Filters Protection". Enable it (I have used the "By Echo Output" option).
This is a known problem with some Wordpress themes. Also note, if the theme affected this plugin, it will surely affect other plugins.
A few comments about this problem are posted in http://pippinsplugins.com/never-remove-the-default-the_content-filters-in-themes/ and http://theandystratton.com/2011/shortcode-autoformatting-html-with-paragraphs-and-line-breaks
From the first link (Never Remove the Default the_content Filters in Themes):
There is a terrible, terrible practice among theme developers to remove some of the default filters that are applied to post and page content...
From the second link (Shortcode Autoformatting HTML with Paragraphs and Line Breaks):
... this mysterious issue of my shortcode output being mysteriously auto-formatted with paragraph tags and line-breaks... It’s globally removing two very important core content filters that WP has built-in... This makes this theme work perfectly and negatively affects ANY and ALL plugins that have shortcodes...
The final solution is the one commented in the second article:
Don’t use a theme that poor code in it.
Don´t use inline JS - use register_script() and wp_enqueue_script
function o99_load_my_scripts()
{
// Register for a plugin:
wp_register_script( 'custom-script', plugins_url( '/js/my-custom-script.js', __FILE__ ) );
// or
// Register for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/my-custom-script.js' );
// then, you can then enqueue the script:
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'o99_load_my_scripts' );

Wordpress multiple / exclusive posting pages

I want a site with a separate News and Blog page, ie only news posts are dispayed on news pages and non news posts on blog pages. Also archive lists, category lists, etc for each page must only display relevant posts. Seems like a common requirement, but using the WP documentation, I keep going around in circles!!! Is there a simple way to do this, without getting into multiple blogs, eg using categories.
Thanks
That's easy.
First, you will need to create custom page template. Refer to this page to see how to create it.
Second, on that page (you can copy from your page.php/index.php, the important part is:
if (have_posts()) : while (have_posts()) : the_post();
Find that piece and add this code just right above that code:
query_posts('cat=3&paged='.get_query_var( 'paged' ));
Things to note from above query_posts snippet is:
cat: this is the category ID you want to query. To easily see what ID is on a particular category, you can use ShowID for Post/Page/Category/Tag/Comment plugin.
paged: Paged will allow your custom page to handle next & prev navigations, which is handled by next_post_link() and prev_post_link(). As for get_query_var( 'paged' ) is function to get what page's page you currently see.
Hope that helped.
<shamelessplug>
I blogged it here (in Bahasa Indonesia, which you can easily translate using google translate).
</shamelessplug>

Resources