I'm using this code in our WordPress theme template to show the previous post which appears to work: previous_post_link('%link', '%title', true);
However, the content of %title includes the entire title of each post, which is very long and messes up the theme formatting.
Is there a way to modify the output of %title to limit to 20 characters?
Try this:
function modify_title_previous_post_link($link) {
preg_match('~>\K[^<>]*(?=<)~', $link, $match);
$title=$match[0];
$title = substr($title,0,20);
$title.="...";
$link=preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1>$title</a>",$link);
return $link;
}
add_filter('previous_post_link','modify_title_previous_post_link');
I'm not that good with regex, so I used this answer for preg_match and this answer for preg_replace.
I'm not a fan of the preg_match method.
Is there a reason that you have to pass '%title' into previous_post_link? The Wordpress function just does a text replacement on it, it's not really a complex macro or anything.
I'd do something more like
$prev_title = $get_previous_post()->post_title;
if (strlen($prev_title) > 20)
$prev_title = substr($prev_title , 0, 20) . '...';
previous_post_link('%link', $prev_title, true);
(I'm new to PHP so I might have gotten some syntax wrong, but that's the basic idea)
Related
I am new to php and trying to trim the lines of text generated by the ('information') part of the code seen below (in my custom WP theme):
<div class="information"><?php echo get_post_meta(get_the_ID(),'information',true); ?></div>
Each post has a different lenght of it's information (basically like a excerpt, but pulled from a custom field in each post) text, which messes up my archive/blog page, as i use a 3 columns grid. I need the lenght of all posts seen on the archive page to be equally long, by restricting the "information" text to be no more than lets say 150 characters long.
I see the fenction in this piece of code for example, taken from a default WP file, but can't seem to wrap it around my own piece of code seen above, to make it work like i want it to:
<?php
et_divi_post_meta();
if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) ) {
truncate_post( 270 );
} else {
the_content();
}
?>
This is what i want/talk about:
Before:
POST 1
I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.
After:
POST 1
I am a wordpress post, with a long text, that should not be so long...
How do i do this?
Thanks!
You can use substr() php function to get part of the string.
Take a look at this example function.
function trimResult($string, $len = 150){
if(strlen($string)> $len){
$string = substr($string,0,$len) . "..."; // to let the user know that was truncated...
}
return $string;
}
ref: http://php.net/substr
Specific code example, will be a better solution to include the php function on a global file.
<?php
function trimResult($string, $len = 150){
if(strlen($string)> $len){
$string = substr($string,0,$len) . "..."; // to let the user know that was truncated...
}
return $string;
}
?>
<div class="information"><?php echo trimResult(get_post_meta(get_the_ID(),'information',true)); ?></div>
Hope it helps.
$post = "I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.";
$post = mb_strimwidth($post, 0, 70, "...");
echo $post;
result: "I am a wordpress post, with a long text, that should not be so long..."
Add ... if string is too long PHP
This is my "code":
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
Now when I want to use it in a post, all I get is:
[whoop]
As you can see I am very new and unused to it so maybe the answer is really simple, maybe I've just miss a thing in advance.
I both checked defining the function in functions.php and also in content.php
For shortcode function echo is required.
Please check with below code
function whoop_function(){
$responseData = "whoop whoop!";
echo $responseData;
return true;
}
add_shortcode('whoop', 'whoop_function' );
put your code in themes/function.php files and remove from content.php as function is duplicated so function already defined PHP error occurred.
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
and add your shortcode [whoop] in any page content section.
if u use do_shortcode('[whoop]'); then echo it like below.
<?php echo do_shortcode('[whoop]'); ?>
Your approach is correct. I think you are using it in a template. Just need to use it as mentioned below :
In files:
<?php echo do_shortcode('[whoop]'); ?>
In admin pages or posts:
[whoop]
Fetch the content like this:
$post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
$content = $post_content->post_content;
echo do_shortcode( $content );
You are fetching content without considering the short code. Update your code as above.
Hope it will work for you.
In Wordpress I currently get the search time like so:
<?php $kw = str_replace("-"," ", $_GET['s']);
however, I would like it so that if there is no search term, the site title is used for $kw instead.
How best to achieve this? I have played around with get_bloginfo(); but no success.
Thank you for any help.
You can apply a filter to the get_search_query function and intercept the incoming query. if the query is empty use the site title else proceed as planned. see the code below (to be added to your functions.php file in your theme)
<?php
function my_search_query($query) {
$blog_title = get_bloginfo();
$query = (empty($query) ? $blog_title : str_replace("-", " ",$query);
return $query;
}
apply_filters( "get_search_query", "my_search_query" );
?>
I'm trying to limit the title into 1 word only
I tried using this code but it doesn't return anything..
<?php
$title = get_the_title();
$names = explode(' ', $title);
echo $names[0];
?>
This is where I'm trying to implement the code If I change the get_the_title() into $names[0] but it doesn't return anything.
$postheader .= '<h3 class="entry-title">' . get_the_title() . '</h3>';
What is in the content of $title? The explode/split should work in that fashion, so there are a couple of things you can do to try to debug:
First, check the content of $title. You can do this as simply as outputting is and viewing source: make sure it has actual spaces, and not non-breaking spacing, or that the field does not start with a space.
Second, make sure your first code block is actually getting executed by echo'ing something you can check for in source, or any other method to ensure it is running. Good luck!
Here's what I'd like to do. This function which grabs the content of a custom Wordpress post field:
get_post_meta($post->ID, 'society_twitter', true);
Needs to run in place of the string 'yourTwitterUsername' below,
[twitter-widget username="yourTwitterUsername"]
Looking something like:
[twitter-widget username="get_post_meta($post->ID, 'society_twitter', true);"]
Is this possible? If so, what is the syntax? A little more detail: I'm trying to modify a Twitter widget to display the username listed in a custom post field in my database rather than just one string value. I've tried editing the plugin php itself but this seems like an easier solution.
Use string concatenation?
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
Edit: Updated based on the comments:
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
echo do_shortcode( $string);