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 );
Related
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.
I'm creating a theme and i want the user to be able to use shortcodes.
Right now it outputs [the_shortcode] and I think I know why but don't know how to fix it.
I load the content of the page not in the conventional way.
Preferably the way is to load the_content() function. But the way my template is designed it loads content based upon placement in the hierarchy of pages.
So a parent has a different look then a child.
To do this I load the content with a foreach loop and echo out $grandchild->post_title. The page being a grandchild of a parent.
Now the way to fix this, according to the internet, is to use the apply_filters() function.
The function expects two parameters and I have no clue on how to fill them:
function apply_filters( $tag, $value )
This is my function for the shortcode:
function output_function(){
return 'Knees weak, arms are heavy';
}
add_shortcode('output', 'output_function');
The shortcode is placed in a page post like this: ['output']
Any thoughts on how to output page content through the filter?
What you want is the_content
$content = 'some string that has a [output] shortcode';
echo apply_filters('the_content', $content);
This filter will make sure all $content is parsed like the WordPress editor.
The same like the_content().
Has anyone encountered this problem before? Directly calling the shortcode from WordPress and calling the shortcode via functions.php yields different results.
Refer to this
Image 1 has decent design since it was invoked directly from WordPress UI while Image 2 somehow messed up the design because it was invoked from php (functions.php).
Additional info:
Current theme is DIVI
Shortcode/Plugin used is TableMaster
To add further information, I have this line of shortcode in WordPress (as seen on image 2).
[get_blogs_sc]
And on my functions.php, I have this function,
function GetActiveBlogs($i)
{
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (preg_match('/sites/',$actual_link))
{
$user_id = get_current_user_id();
if ($user_id > 0)
{
echo do_shortcode('[tablemaster buttons="true" datatables="true" class="black-header-gray-alternate-rows" sql="some select where user_id = '.user_id.'"]', true);
}
}
}
add_shortcode('get_blogs_sc', 'GetActiveBlogs');
But when I use the shortcode directly from WordPress:
[tablemaster buttons="true" datatables="true" class="black-header-gray-alternate-rows" sql="some select where user_id = 14]
the display on my page looks good (as seen on image 1)
My objective on putting the shortcode in php-layer is for me to be able to capture the logged-in user from WordPress.
Thanks.
it's hard to diagnose what the issue is only from images ! without what the code really says.
but let's know what difference between do_shortcode and add_shortcode
do_shortcode( string $content, bool $ignore_html = false )
Search content for shortcodes and filter shortcodes through their hooks.
it return (string) Content with shortcodes filtered out.
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
for more information check link
while add_shortcode Adds a hook for a shortcode tag.
<?php add_shortcode( $tag , $func ); ?>
$tag
(string) (required) Shortcode tag to be searched in post content
Default: None
$func
(callable) (required) Hook to run when shortcode is found
Default: None
it return nothing but when you write shortcode tag in post area it apply the function of shortcode for more information check Link
I am looking for the way how to alter a WordPress post title and body when it is displayed to a normal user. But not in any other context, ie in admin module.
The task at hand is to fetch a query parameter, carry out some calls to external webservice and database operations and replace some text in both title and body. The <title> tag should also contain the changed title.
I do not look for a plugin solution, but rather were I should include a php file with all the code that does all the necessary processing.
My expectation was that I don't have to deeply meddle with the theme files. Of course, I could inject my code in the respective theme's page.php or content.php file. But I'd like to do it in less intrusive way, for example by including my functions_custom.php file in the top of functions.php, and this file would contain a collection of functions and add_filter() calls to hook those functions on certain events. Is that possible to achieve my goal that way?
This is my first attempt:
<?php
function alter_title( $title, $id = null ) {
return 'Altered title: ' . $title;
}
add_filter( 'the_title', 'alter_title', 10, 2 );
?>
This causes change in all the post titles in all contexts, including the post list in the Dashboard. But I need it to be altered only in view context (this also does not influence the text used in the <title> tag). Perhaps I can sniff this context somehow, and engage the alteration only when the view mode is detected?
While ago I needed to do similar thing and my fast and short workaround was to have an "API" (
Created a template for that specific post type and using PHP generated a post ID indicator:
$post = get_post(); echo "var post_id = '".$post->ID."';";
I created a custom API that "takes" $_POST request with "post_id" param and it would fetch adequate/custom (in your case a content that will be shown to the user) content from database for the post with the post_id from $_POST request.
Then for every post there would be an ajax request to that API which sends post_id and fetches all the needed info (in your case post title and body) and then using eg. jQuery
.append() would populate post title with response post title and body too, respectively.
Also, if you'd like to avoid a extra request than you should try to generate that 'user-visible' content with PHP. That would mean you need to collect the content before loading the page and echoit in the place of default content.
I suppose you should create a function in functions.php that takes an $post->ID and fetches title and body from the database. Then you should call that function on every loop and single post template, if you understand me.
is_admin() will return true if you are viewing an admin page.
So you could do this:
function alter_title( $title, $id = null ) {
if(is_admin()){
return $title;
}else{
return 'Altered title: ' . $title;
}
}
add_filter( 'the_title', 'alter_title', 10, 2 );
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.