Wordpress read more error and length - php

Ok so I literally can't find anything that references this issue. I've tracked down why it's happeneing but can't find a solution. So what's happening is when an author makes a post and the tag is used at a point that isnt 40 words in the author page doesnt include the read more link. So basically if the more tag isnt placed atleast 40 words in, the link doesnt show up and assumes the post is only that may words long.
here's a link to the issue...
http://www.dudnyk.com/blog/author/frank-powers
if you look at the 2nd post down you can see there is no "Continue Reading" but if you look for that post on the main blog page, you'll see there is a link for it. If I moved the read more further down in the post it would work. any ideas??
the loop is initiated here... get_template_part( 'loop', 'author' ); but i'm not sure how to find where that function is doing what where.

You are using (a child theme of) the wordpress theme 'TwentyTen', which ships with wordpress since 3.0 was released.
There are two functions in your functions.php that together cause the effect your witnessing.
function twentyten_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
sets the excerpt length to 40 words. And something along the lines of this:
function twentyten_auto_excerpt_more( $more ) {
return ' …';
}
add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
There should be a third function, twentyten_continue_reading_link(), which should be called by twentyten_auto_excerpt_more( $more ) in the second line like so:
return ' …' . twentyten_continue_reading_link();
But somehow it doesn't work. Either because the auto excerpt function does not call the continue reading link, or because the continue reading link does not exist.
Anyway, if you do not use excerpts anywhere else on your site, just set the excerpt length to a lower value than 40 in the first function I posted - or, if your using excerpts, modify the second such that it returns a 'Continue reading...' link.

Related

Call first word of post title in page template

I want to do exactly what is talked about in this question: Get first word in string php the_title WordPress
(I think) I understand that putting this code in functions will trim the title, but how do I actually call it in the page template?
In Wordpress one does not call specialized functions in the template to modify such things as the title (or other data from the database).
Instead Wordpress offers filters to modify the output. See:
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
Example: (in functions.php or some related PHP file from the used theme)
function title_first_word( $title, $id = null ) {
/* optional: check here on which page you are or which template is in use */
$titleParts = explode(' ', $title);
return $titleParts[0];
}
add_filter( 'the_title', 'title_first_word', 10, 2 );

Execute a WordPress shortcode in Genesis breadcrumbs

I've created a shortcode [year] to show the current year dynamically. I enabled it in the post title by using this filter:
add_filter( 'the_title', 'do_shortcode' );
Shortcode executes successfully in the post title but displays as it is in the breadcrumbs. I'm using Genesis Framework so is there any way to make it work in the Genesis breadcrumbs too?
Using the_title filter like you have done, should actually work just fine (I just tested and confirmed it's working on a local site of mine).
As a matter of fact it can sometimes be a problematic filter because people expect it to filter just the page title, but in reality it filters the_title whenever it's pulled from the database, including the Page Title, Breadcrumbs, Menu Item Name, etc.
That said, if it's not working for some reason, Genesis has a handy Genesis_Breadcrumb class. Most notably it has the genesis_breadcrumb_args filter that lets you remove the "You are here:", etc. Provided the theme you're using is implementing Genesis Breadcrumbs and not some custom ones, you actually have access to the lesser utilized genesis_build_crumbs filter.
The build_crumbs() method returns an array of Page/Post Names that you can modify using a simple foreach loop or array_map.
Take the following function:
function so52299149_breadcrumbs( $crumbs ) {
$new_crumbs = array_map( function($val){
return do_shortcode( $val );
}, $crumbs);
return $new_crumbs;
}
add_filter( 'genesis_build_crumbs', 'so52299149_breadcrumbs' );
What it will do is run each element in the $crumbs array through the do_shortcode() function, giving you the desired output. Again, the way you have it, add_filter( 'the_title', 'do_shortcode' ); should be working - so check how the Child Theme you're using is implementing breadcrumbs, especially if the function above doesn't work - if that's the case there's likely something else at play.

PHP run same function twice for different output

In pure theory my question is whether it is possible to make one php function run twice taking a different variable each time and returning it as a result.
In practice, I'm tinkering with a child theme for WordPress (and learning some PHP basics). The original theme, twentysixteen, has a function to construct a link to Google fonts using several parameters and output the final URL, which is then taken by some other function and inserted in to the head template with added HTML markup - link, href, rel, type.
In my child theme I had to replace one of the default fonts completely, so instead of reconstructing the function, I simply minimized it to just contain and output the final font URL like so:
if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
function twentysixteen_fonts_url() {
$fonts_url = 'https://fonts.googleapis.com/css?family=Merriweather:400,700,900,400italic,700italic,900italic|Inconsolata&subset=latin,latin-ext,cyrillic,greek';
return $fonts_url;
}
endif;
What I'm trying to do now is get another URL (https://code.cdn.mozilla.net/fonts/fira.css) into this function to load a third font from Mozilla's CDN alongside the Google ones.
Can't put it into a separate function with a different name, because its output does not get picked up by the head template. Using an array puts both urls into one link href.
Can somebody please point me in the right direction?
Thank you.
Twentysixtee juste use one font, and the URL is generated with twentysixteen_fonts_url ()
If you look into Twentysixtee's function.php, twentysixteen_fonts_url () is use only 2 times. It's use to include font url in editor (line 144) and in front end (line 233).
Add an other font
If you want to add an other font you need to add it in your child theme.
You could do it by adding the following code in your function.php
function my_child_theme_font_url()
{
$fonts_url = 'https://code.cdn.mozilla.net/fonts/fira.css';
return $fonts_url;
}
function my_child_theme_enqueue_scripts()
{
wp_enqueue_style( 'my_child_font', my_child_theme_font_url(), array(), null );
}
add_action( 'wp_enqueue_scripts', 'my_child_theme_enqueue_scripts' );
As suggested by #MonkeyZeus the simplest way to achieve having another webfont enqueued in such cases is leaving the first function as it is and adding a different one right after it to enqueue the needed file like so:
add_action( 'wp_enqueue_scripts', 'register_custom_plugin_styles' );
function register_custom_plugin_styles() {
wp_register_style( 'fira', 'https://code.cdn.mozilla.net/fonts/fira.css' );
wp_enqueue_style( 'fira' );
}
As for the theoretical part of my question, I understand that somehow forcing a function to run a second time and produce a different output would not force the other function using this output to run a second time as well, producing a different result. For this to work, the calling function has to be modified, not the emitting one.
Thank you #MonkeyZeus and #Jordane-CURE for your help.

How to add_filter to the_content after a plugin does it

I have a function that adds a block of html to the top of the_content() wordpress function, however, a wordpress plugin I have also does the same, and it inserts its code ABOVE the block of html I already inserted. But I want my block of html to always be at the top of the_content output.
Is there a way to specify this?
This is my function at the minute (the plugin does it in a similar way, so I assume it's done in order of loading
function my_function($content){
global $post;
$content = $content . my_function($post) ;
return $content;
}
add_filter( 'the_content', 'my_function', 9 );
the plugin uses:
add_filter( 'the_content', array( &$this, 'the_content' ) );
I set mine to priority 9, so surely it would be loaded before the plugin's 10 (default)?
Thanks guys.
I'm not sure if you still need help with this but it might be helpful to other people who find your question.
I set mine to priority 9, so surely it would be loaded before the
plugin's 10 (default)?
That is correct, but you want your filter to take effect AFTER the plugin's filter. Currently, your filter inserts a block of HTML into the top of the content section. Then, the plugin filter inserts a block of HTML into the top of the content section, above your previously inserted block. So, you should set the $priority filter to a higher value like 20 so that it will occur later and insert the HTML block above all HTML blocks inserted by other filters.

Wordpress: Inserting if-else statement into next_post_link() / previous_post_link() parameters?

First question here, though reading and searching has saved my bacon a number of times.
I'm a PHP hack at best, and a Wordpress theme I'm building is forcing me to learn a lot.
I've run into a problem in trying to get the next & previous links' formats to display differently based on their respective categories (and not the single.php's page).
Here's my best try so far:
<?php next_post_link(
'%link',
if ( in_category( 'tweet' ) ) {
get_the_content()
} else {
echo '%title'
};,
FALSE
)?>
This results in a syntax error.
My first question is can I even use an if-else statement within a function's parameters?
If I'm approaching it in completely the wrong way, then it's back to the drawing board. I'd appreciate any alternate suggestions, then, as well!
I know the following work:
next_post_link( '%link', get_the_title(), FALSE );
next_post_link( '%link', '%title', FALSE );
To try and be more clear, I'm looking to affect output of the next/previous links based on the category of the links themselves, and not the present host page they are on.
The problem I'm having is a) how to determine what category the next_post_link() and previous_post_link() are, and then b) display their Link parameter accordingly. If I may throw up the white flag, the examples I've found using get_adjacent_post() and various arrays and the like seem way over my head at this point. I'd really appreciate someone walking me through any solutions using them.
What to aim for?
I think you're mixing stuff a bit too much which makes your problem more complicated than it needs to be. I know with many new things, this can happen quite easily, so I think it's worth to spend some thoughts about the what to be done before starting to code. It should help in the end.
Start to formulate what you want to achieve. If I understand you right you write that you want to display the next and prev links below the current post differently based on category.
But which category? The one of the current post that displays? Or the category of the prev/next linked post?
Next to that, posts can have multiple categories. Do you want to change the display based on one or on multiple of or even all of the categories?
And finally: Do you want to output tons of HTML tags for the visual styling? Or do you want to use CSS for that.
How to do it?
After you have thought about what you actually want to achieve, you can make your mind how this can be done.
In the following I'll show some example code that will do the following:
Explains the use of the link-format parameter you want to change.
Shows how to get the CSS-classes for the links in question.
Extends a theme's output with these CSS-classes.
Make it re-useable for different files within your theme.
So this example covers the categories of the linked posts for visual styling via your Theme's CSS which I think is the preferable way.
The PHP code
PHP is pretty flexible so it's in the end easy to get that done, but without knowing what exactly to do, this flexibility in PHP can turn out to be a problem because it's also possible to loose the trail and then code something non-working.
Let's just say you just want to change the link-format for the next_post_link() function.
The function will replace %link with the HTML link tag (<a href=...>Post Title</a>), so if you take 'This is the next post of my blog: %link; waiting to be read!' as parameter, you've added some text around the link.
<?php
$format = 'This is the next post of my blog: %link; waiting to be read!';
next_post_link($format);
?>
You can additionally add HTML tags around it as well:
<?php
$format = '<span style="font-size:2em">%link</span>';
next_post_link($format);
?>
Which will make all next post links having a double of their standard font-size.
So now to make this a bit more easy for themeing, best would be to add css classes of the category/ies then you can change the display easily within the CSS.
<?php
$inSameCategory = true; // depends on what you want, can be false as well
$nextPost = get_adjacent_post($inSameCategory, '', false);
$cssClasses = get_post_class('next-post-link ', $nextPost);
$format = sprintf('<span class="$s>%link</span>', $cssClasses);
next_post_link($format);
?>
This will wrap the next post link into a <span> containing all CSS-classes of the linked post plus a next-post-link class in case you need this for the CSS-selector to style the links. Here some example CSS:
.next-post-link.category-tweet {font-size: 2em;}
Making it reuseable
As you have a next and prev link you could copy over that code only for changing one parameter. But we're lazy and instead we wrap it into a function of it's own:
/**
* my theme's format string for prev|next_post_link()
*
* #param bool $previous (optional) previous (true, default) or next (false) post
* #param bool $inSameCategory (optional) use same category, default: true
* #return string
*/
function my_prev_next_link_format($previous = true, $inSameCategory = true) {
$postId = get_adjacent_post($inSameCategory, '', $previous);
$cssClasses = get_post_class('next-post-link ', $nextPost);
$format = sprintf('<span class="$s>%link</span>', $cssClasses);
return $format;
}
Place this function into your theme's functions.php. It's available then in all your templates. Using it becomes now very easy:
<?php prev_post_link(my_prev_next_link_format()); ?>
...
<?php next_post_link(my_prev_next_link_format(false)); ?>
If you've put this all together, you can easily style the next and prev links within your theme's CSS.
Have you tried anonymous functions? In that way you can use if-else statements within a function
Try:
$format_string = in_category('tweet') ?
'<span style="color:red;">%link</span>' : '%link';
next_post_link($format_string);
The second and third parameters are optional and default to title and false.
Edit:
I don't know this plugin, but..
if(in_category('tweet')){
$format_string = '<span style="color:red;">%link</span>';
$link_text = get_the_content();
}else{
$format_string = '%link';
$link_text = '%title';
}
next_post_link($format_string, $link_text);

Categories