I'm trying to replace a string within a Wordpress title. I need replace two specific keywords in the title. I don't wish to change the title for the whole page only for this specific instance. I can't seem to get this code to work and I'm not sure how to add addition strings to replace. Thank you for the help - I'm a PHP novice.
<?php
$wptitle = the_title();
$wptitle = str_replace('Download', '', $wptitle);?>
<?php echo $wptitle; ?>
This works:
<?php
$wptitle = get_the_title();
$wptitle = str_replace(array('REPLACESTRING1', 'REPLACESTRING2'), '', $wptitle);?>
<?php echo $wptitle; ?>
Related
I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);
currently I use tags in my wordpress article titles and place them at the beginning.
https://alerte-prolo.fr/les-alertes-alimentaires/
https://alerte-prolo.fr/alimentaire/acheter-ses-fruits-et-legumes-moins-chers/
I want to keep the tags in the category pages, but in the articles (single.php) I want to delete the tags.
currently I use this to lift the first word of the title.
<h2>
$originalTitle = the_title('','',false);
echo substr($originalTitle,strpos($originalTitle, ' '));
</h2>
t works with [Astuce], [gratuit] ect
but when I have tag [bon plan] it would be necessary to be able to delete the first two words.
I need help for this.
thanks
I think this working for you:
<?php
$originalTitle = "[bon plan] Some Title"; // In your case: $originalTitle = the_title('','',false);
$newTitle = explode( "] " , $originalTitle, 2);
echo $newTitle[1];
?>
If I understood You correctly:
$originalTitle = "[wtf] [this one to] Some Title";
$newOriginalTitle = preg_replace("/\[[^)]+\]/","",$originalTitle);
echo $newOriginalTitle;
/\[ - defining start
\] - defining end
how to print the content in wordpress by passing the string php.I have written the following code but it print all the content including image.I want it to print only particular text.
<?php
$content = get_the_content('Read more');
print $content;
?>
This depends on whether or not you're in the Loop - if you are then your code should work - see more here: https://codex.wordpress.org/The_Loop
However, if you are outside the loop and want to pass in a post ID as a paramter, you can refer to this post as everything is explained very well in there: https://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Use the_excerpt for only showing small content with read more
<?php $short_desc= get_the_excerpt();
echo substr($short_desc,0,200);?>
I need to get separated the image and text of the article, the problem is that they are in one field of the database, so how can i make that in the view i can put the image separated from the text? because i can't put the image and the text togheter, is a mess
<? foreach ($list as $item) : ?>
<?
$year = date('Y', strtotime($item->created));
$month = date('m', strtotime($item->created));
$day = date('d', strtotime($item->created));
$shortDescription = cutString($item->introtext, 100);
?>
<div class="footerItem">
<?= $day; ?>-<?= $month; ?>-<?= $year; ?>
<p><?= $item->title; ?></p>
<p>
<?= $shortDescription; ?>
</p>
</div>
<?php endforeach; ?>
so i need to put the image like this $item->image - this is what i want, but in joomla latestnews_mod, there is no field with image, and i don't know if i need to write my own, or edit this module, or i need to use other module for this ? And also i cannot get the field $item->category that i need, there is only the CAT_ID, and how can i get the category field??
I've lost touch with Joomla a bit, so it's entirely possible this is not the easiest way, but could you use regex to pull out the image from the intro-text if it's not specifically given?
You'd need a pattern something like:
<img\b[^>]+?src\s*=\s*['"]?([^\s'"?#>]+)
That's taken from this question: Regex to find the first image in an image tag in an HTML document
EDIT: Example using preg_match:
<?php
// This is the Regex pattern from above.
$pattern = '/<img\b[^>]+?src\s*=\s*[\'"]?([^\s\'"?#>]+)/';
// Perform the search, matches will be stored in $matches
preg_match($pattern, $item->introtext, $matches );
echo "Image src is: " . $matches[1] . "\n";
?>
Do note that this is relatively untested, may not work and if it does, may miss cases with bad markup!
I'm needing to replace white spacing with a plus sign "+" for the code displayed below.
I'm in the process of modifying some code which generates the label and url for products displayed in my catalog. The problem I face is that my current code doesn't do the replacement. Can someone please modify the code, replacing a spacing for a plus sign "+".
<h5><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h5>
and will return a url something like this:
http://www.efficienttrade.co.nz/catalogsearch/result/?order=relevance&dir=desc&q=potassium nitrate
However, when getName() function is used, names which have a space don't work for the generated search query. So I need to replace the space with a "+" to make the search query url work.
Thanks
As far as i understand your problem, you need to replace spaces by hypens in your product name. This can be achieved by replacing the following code in your href
...<?php echo $this->stripTags($_product->getName(), null, true); ?>...
with
...<?php echo str_replace(' ', '-', $this->stripTags($_product->getName(), null, true)); ?>...
How about the following to make you code slightly nicer (although PHP/HTML soup is never a lot of fun). The first line of PHP is the one that replaces spaces with hyphen
<?php
/*Get product name, stripped of HTML and spaces*/
$productName = str_replace(' ', '-', strip_tags($_product->getName(), null, true));
/*Assign variables rather than using same function multiple times.*/
$productAttribute = $_helper->productAttribute($_product, $_product->getName(), 'name');
/*Concatenate the URL here for easier code fixing later.*/
$url = 'http://www.efficienttrade.co.nz/catalogsearch/result/order=relevance&dir=desc&q=' . $productName;
?>
<h5>
<?php echo $productAttribute ?>
</h5>