Hello. I have a scenario that seems complicated but actually I want to connect a button to the excerpt function of wordpress. I can control this with functions.php, but that’s not my purpose. That’s why I understand that php should be involved more than wordpress.
First of all, I have a function in functions.php that checks the number of characters as follows
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return 100;
This is linked to the content.php file and the content.php file looks like this
doesnt that function return an object? Wouldnt you need to be checking some component of the object, not the object itself? Or just use the same function you use later on:
which presumably returns the text?
EDIT: Oh, i see. the_excerpt automatically echoes after filtering. That’s… a choice…
function that returns text <?php the_excerpt(); ?>
But the button is in a different div and that button needs to be tied to this function. That’s what the code in the last section above is trying to do. However, the button cannot be connected to functions in any way, so it works independently. There is an error somewhere, but I can’t see it.
so… i’m not a wordpress expert. I find it excessively bloated. But from a quick glance at the reference manual for the_excerpt, you should be able to check the result of apply_filters( 'the_excerpt', get_the_excerpt() ); for its length.
Yes. That’s not a problem, I can do that. But the problem is that I’m running this button in a different div. If I use WordPress own functions, everything works as I want. But my goal is to move this button outside of WordPress excerpt div. That’s why I don’t control the function with functions.php. I moved the button to a different div and it works independently, but I still want it to connect to wordpress excerpt function.
I’m afraid you’re beyond my expertise then, as you don’t actually seem to be stating a problem.
From a non-Wordpress, pure PHP standpoint:
The code you have written appears correct, as long as whatever you put in $excerpt is actually the text you want to compare. An object does not have a strlen, and will throw an error if the object’s class does not implement a __toString() method.
If I run this or a similar function in wordpress functions in functions.php then everything is as I want it to be. The button is automatically hidden or shown. But I don’t prefer this approach for some reasons. The excerpt function in the theme is managed from another php file and only from there. (Content.php)
Here I created a different div and button in the content.php file and I want to connect this button to the excerpt function. The important point is that the button moves independently when it goes outside the previous excerpt div element. But my goal is to move it to a different div anyway.
Even though this is in another div it still needs to be linked with the wordpress except function. (According to my project)
Yes. What I want to compare is just the text and the number of characters. So __toString() is not suitable for me?
var_dump($excerpt);
var_dump($excerpt_length);
The validation is actually successful, I get an output. But the button is not hidden even though it should be hidden according to the number of characters. It needs to get the character count from functions.php. Do I have to call the function directly from functions.php to connect with this number of characters? include('functions.php'); with the method
var_dump($excerpt); This is actually just for debugging. I increased the excerpt limit to 150, 200 etc. And nothing happened. Logically, the “read more” button above should now be tied to the character limit and the excerpt function. But there must be something being overlooked somewhere.
<?php
$excerpt = get_the_excerpt();
$excerpt_length = strlen($excerpt);
$excerpt_limit = 100; // Match the limit set in functions.php
?>
After this the button in a different div should now calculate the character count and be hidden. But apparently that is not the case. The button is not triggered, it is not hidden in short texts, it remains fixed. As I said, there is absolutely no other button, I’m sure of that. I hope I managed to explain my purpose better.
If I change the limit in functions.php the number of characters changes. But changing the character limit in content.php doesn’t help. (But it was already written to be paired with functions.php)
Okay maybe to a wordpress expert this means something.
I’m looking at this code:
absolutely none of that cares about what you did in another file.
In THIS file, the code does this:
$excerpt = get_the_excerpt(); //Get a string (hopefully)
$excerpt_length = strlen($excerpt); //Find the length of that string, whatever it was.
$excerpt_limit = 100; // Set a static number.
?>
<div class="news-main">
<?php the_excerpt(); ?> //Let Wordpress spit out whatever wordpress is going to spit out.
</div>
<?php if ($excerpt_length >= $excerpt_limit) : ?>
//Check one number against the other number in this file. If the first is bigger or equal to the second, echo a link.
I managed to partially solve the problem. But this is a complex and specific method. It is not suitable for every theme and project and needs to be developed. I accidentally noticed that the code was working in a contradictory way. For WordPress, it takes into account the character limit in functions.php, but the character limit in content.php has to be much higher than that. For example, if the character limit in functions.php is 150, the character limit in content.php should be 999. If this ratio is not set correctly, the button will not be displayed even in long texts. When set correctly, the button does not appear in short texts and from this point of view, it can be said that the code works.
But this ratio is unreasonable and its setting is a matter of chance, so the code should still be considered faulty. A normal and correct code cannot depend on the luck of numbers like the golden ratio. And it may always work well for me, but it may not work right for someone else. It probably conflicts with some functions in the theme that control excerpt functions… Or wp’s excerpt length function in functions.php conflicts with php’s substr function. It may also be necessary to remove the previous function for it to work correctly. Actually the correct scenario would be to manage it entirely in content.php. This is why the method is flawed or not a solution for every theme.
and you agreed… was the number ACTUALLY less than 100, or did you just ignore me? Cause your post there would tell me the number was way above 100, and you just ignored me and did you own thing.