RSS feed excerpt doesn't display the excerpt - php

I am trying to display the excerpt in a custom feed page, using the_excerpt_rss() function, I already set the summary show mode in Settings->Reading but it returns me the first paragraph of content, is there any another function to call just the excerpt, I already try with the_excerpt() and get_the_excerpt()?
Here is my code:
<description><![CDATA[<?php html_entity_decode(preg_replace( "/\r|\n/", "",the_excerpt_rss())); ?>]]></description>

I had found a solution. So I customized the function add_feed_content($content):
function add_feed_content($content) {
global $post;
$excerpt = $post->post_excerpt;
if(is_feed()) {
if (strlen($excerpt) > 0){
$content = '';
$content .= $excerpt;
return $content;
} else {
return $content;
}
}
}
add_filter('the_excerpt_rss', 'add_feed_content');

Related

Generate meta page title exactly as on h1 tag

Site based on Joomla. I have many pages where h1 header is mentioned as product detail and displayed based on product details through PHP. There are 2 files: default.php and view.html.php.
default.php :
<h1>Used <?php echo $this->CatName; ?> <?php echo $this->prodDet->prod_name;?> Toy for Sale </h1>
This correctly display the h1 tag. I want to generate meta title of the page and use this h1 output as generated in view.html.php. This line defines the title of the page :
$this->document->setTitle($title);
And this line defines header h1 :
"{$this->item->heading}";
Complete code :
protected function _prepareDocument()
{
$app = JFactory::getApplication();
$menus = $app->getMenu();
$title = null;
// Because the application sets a default page title,
// We need to get it from the menu item itself
$menu = $menus->getActive();
if ($menu)
{
$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
}
else
{
$this->params->def('page_heading', JText::_('COM_USEDCAR_DEFAULT_PAGE_TITLE'));
}
$title = $this->params->get('page_title', '');
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$title = "{$this->item->heading}";
$this->document->setTitle($title);
if ($this->params->get('menu-meta_description'))
{
$this->document->setDescription($this->params->get('menu-meta_description'));
}
if ($this->params->get('menu-meta_keywords'))
{
$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
}
if ($this->params->get('robots'))
{
$this->document->setMetadata('robots', $this->params->get('robots'));
}
}
Output in title tag is heading. How to put this h1 tag output instead of $title?
Here's what the title portion of your code does:
// getting title from params
$title = $this->params->get('page_title', '');
// trying to get it right
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
// overwrite everything above with some value, making above code useless
$title = "{$this->item->heading}";
$this->document->setTitle($title);
I might be wrong but if I recall correctly, if a value doesn't exist it will return the variable name when cast into a string. Here "heading" might be empty.
You might want to change your code to something like this:
[...]
if(!title){
if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
$title = $this->item->heading;
} else {
$title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
}
}
$this->document->setTitle($title);
You might as well like to save the title to session and reuse it everywhere:
[...]
$this->document->setTitle($title);
// save title to session
$_SESSION['page_title'] = $title;
and update the previous loop:
// getting title from params
$title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');
if (empty($title)){
[...]
Full code would be something like that:
[...]
session_id() || session_start();
$title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');
if(!title){
if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
$title = $this->item->heading;
} else {
$title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
}
}
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$_SESSION['page_title'] = $title;
$this->document->setTitle($title);
[...]
You might as well just ditch everything and go like that if you'd like:
[...]
$title = $this->params->get('page_title', '');
if(!title){
if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading) {
$title = $this->item->heading;
} elseif(
property_exists($this, 'CatName') &&
property_exists($this, 'prodDet') &&
property_exists($$this->prodDet, 'prod_name') &&
$this->CatName &&
$this->prodDet->prod_name
){
$title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
} else {
$title = $app->get('sitename');
}
}
$this->document->setTitle($title);
[...]
Code is untested but it should put you on the right track :)
Why don't you just send your h1 content to your php-document as GET parameter and then just output the it using echo inside the title tag? Unless you avoid dinamic echoing, this could be a fine solution for outputting text as title.
I would abstract away the logic of constructing the title/header to some function and then use this function to construct the title in both places.
function constructTitle($catName, $prodName) {
return "Used {$catName} {$prodName} Toy for Sale";
}
...
[in default.php]
<h1><?php echo constructTitle($this->CatName, $this->prodDet->prod_name); ?></h1>
[in view.html.php]
$this->document->setTitle(constructTitle(..., ...));
This allows you to have a single point to format your title while using it in several places.
The function needs to, obviously, be place in such position so that it can be accessed in both places and you need to have some way to get category name and product name in view.html.php. Im not familiar enough with joomla to know these things.
Edit:
To clarify, there is no real way to "extract" the title from the default.php as it is dynamic. You would need to process the php file then maybe you could do some regex magic, but this is in no way the proper solution to the problem.
you can just send your h1 content to your php-document as GET parameter and then output the it using echo in the title tag? Unless you avoid dynamic echoing,it would work.

Shortcode output shown at the top of the page

I know this question is similar to other questions that have been posted. I have followed exactly what was suggested in answers to those questions but still can't figure out why the output is shown at the the top of the page.
function foo_shortcode($atts, $content = null) {
$datashortcode = '<div>'.(function_exists('rtb_kk') ? rtb_kk() : '').'</div>';
return $datashortcode;
}
add_shortcode('showfoo', 'foo_shortcode');
Any idea?
Without knowing how the rtb_kk() function works, I can only assume it uses echo to display content rather than using return. This is what causes the output of that function to appear at the top of the page.
To work around this issue, you can capture the output of the function with ob_start() and ob_get_clean():
function foo_shortcode($atts, $content = null) {
if (function_exists('rtb_kk')) {
// Start output buffering
ob_start();
// Run the function
rtb_kk();
// Capture buffer as a string
$output = ob_get_clean();
} else {
// Function doesn't exist so we return an empty string
$output = '';
}
return '<div>' . $output . '</div>';
}
add_shortcode('showfoo', 'foo_shortcode');
Alternative method
If you're able to use the bcn_display() instead of the rtb_kk() method you're using, then there is no need to rely on ob_get_clean().
function foo_shortcode($atts, $content = null) {
if (function_exists('bcn_display')) {
// Return the output as a string so we can control when it's displayed
$output = bcn_display( true );
} else {
// Function doesn't exist so we return an empty string
$output = '';
}
return '<div>' . $output . '</div>';
}
add_shortcode('showfoo', 'foo_shortcode');
This will solve your problem, just try
<script type="text/javascript">
function foo_shortcode($atts, $content = null) {
if(function_exists('rtb_kk')){
$rtb_kk = rtb_kk();
}else{
$rtb_kk = '';
}
$datashortcode = "<div>$rtb_kk</div>";
return $datashortcode;
}
add_shortcode('showfoo', 'foo_shortcode');
</script>

target page with php based on page slug

Lets say I have six pages in wordpress
example.com/apples
example.com/art
example.com/bananas
example.com/broccoli
example.com/cars
example.com/cats
I want to target pages which slugs begin with specific letter
if (page slug beginns with "a"){
echo 'content for pages with slug beginning with a';
}
else if (page slug beginns with "b"){
echo 'content for pages with slug beginning with b';
}
else if (page slug beginns with "c"){
echo 'content for pages with slug beginning with c';
}
How do I write this correctly
Referring to this answer here, I'd say it's safe to get the URL like this:
/** Get the queried object and sanitize it */
$current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
/** Get the page slug */
$slug = $current_page->post_name;
And then:
/** Get the first character */
$slugBeginsWith = substr($slug, 0, 1);
/** Apply your logic */
if($slugBeginsWith == 'a')
{
echo 'content for pages with slug beginning with a';
}
elseif($slugBeginsWith == 'b')
{
echo 'content for pages with slug beginning with b';
}
elseif($slugBeginsWith == 'c')
{
echo 'content for pages with slug beginning with c';
}
However you didn't mention what is your goal here. Maybe if you provide more information in your question we could help better!
you need to get the firstcharacter by using php substr function. place following code in functions.php file
add_filter('the_content', 'change_content_by_firstCharacter');
function change_content_by_firstCharacter( $content ) {
global $post;
$post_slug = $post->post_name;
$firstCharacter = substr($post_slug, 0, 1);
if ( $firstCharacter == 'a' ) {
$content = 'content for a goes here';
} else {
return $content;
}
}

PHP return size of Wordpress Articles

I'm trying to make Wordpress return the length of my articles in 3 categories: Small, Medium and Large.
I'm currently using this function to test it but I doesn't seem to work (I'm a total PHP noob!)
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
if ($content > 4000) {
return 'large';
} else if ($content > 2500) {
return 'medium';
} else {
return 'small';
}
}
Could anyone please help me? It would be even better if the function automatically added the post to the right category in Wordpress, but for now this was all I could.
Thanks in advance!
You can use the strlen function to get the length of a string:
function wcount()
{
ob_start();
the_content();
$content = ob_get_clean();
$length = strlen($content);
if ($length > 4000) return 'large';
if ($length > 2500) return 'medium';
return 'small';
}

Setting the limit of the_content characters

I'm using this code to set the limit of 300 characters to the_content.
How can i set when i want to use it and when i dont ?
<?php
add_filter("the_content", "plugin_myContentFilter");
function plugin_myContentFilter($content)
{
// Take the existing content and return a subset of it
return substr($content, 0, 300);
}
?>
<?php
function plugin_myContentFilter($content) {
if (!is_single()) {
return substr($content, 0, 300);
} else {
return $content;
}
}
add_filter("the_content", "plugin_myContentFilter");
?>
Will shorten the content to 300 characters unless on a single post page.

Categories