I am really quite new to programming and I need help with something that I think is not difficult. What I want is to delete the title of the attachment page, I have searched and in this forum I have found a code that has worked partially. That is, the code removes the title but also removes the menu from the attachment page. I have searched everywhere, and I have seen that conditional tags are required to generate that function, however, I am a beginner in the world of websites and I need your help. The code is the following:
add_filter( 'the_title', 'remove_page_title', 10, 2 );
function remove_page_title( $title, $id ) {
if (is_attachment())
return '';
return $title;
}
Thanks in advance for your help.
In your child theme you would add this into the functions.php file:
function remove_page_title( $title, $id ) {
if (is_attachment()):
return '';
return $title;
endif;
}
remove_action('virtue_page_title_container', 'remove_page_title', 20);
I hope it would help you out.
Related
I'm having trouble changing the posts display in my WordPress website. So far, the posts can display a title and text content, and I would like to display tags, categories and an image. I added the following code in functions.php and it actually displays the p tags, but around the content, and not in it. Also, it is displayed on all pages of my website, while I just want to add these HTML tags inside the posts.
So,
How can I put the tags inside the_content() and only display the custom HTML in posts?
I hope my question is clear, I starting learning PHP a few days ago, sorry!
Thank you so much in advance for you help!
The code :
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);
Not totally sure what you want for the "in it" part but I can help with conditionally applying your code to posts only... See below:
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
// if not a post then return the $content as is
if ('post' !== get_post_type()) {
return $content;
}
// must be a post if we get here so lets do something with it
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);
I know there are other questions like this but didn't find a reliable answer. So:
First activate the thing (simplyfied code):
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
Second, delete title tag from header.php.
Third, on page templates, before calling get_header(), add something like this:
add_filter('wp_title', 'set_custom_title', 10, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
Well, this is not working at all, in any template, being a page, an archive, a custom taxonomy or post type archive. No nothing. Wordpress is generating titles by itself.
Why? Am I doing something wrong? Note that this code once upon a time just worked: used in other sites/themes.
Is it maybe an issue of wp5.2.0?
So, thanks to #Vel, the answer is to re-add the title tag (even if in previous wp versions > don't know til what version you had to delete it form head instead).
Current working code for me:
//functions.php
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
//header.php
<title><?php wp_title('|', true, 'right'); ?> | <?php echo get_bloginfo('name') ?></title>
//page templates
$window_title = // do something
add_filter('wp_title', function($title, $sep, $seplocation) use($window_title){ return $window_title; }, 10, 3);
Try to use follows code -
add_filter('document_title_parts', function($titles){
return array('title' => 'Custom Title');
});
For anyone still having this issue of the wp_title filter not working, I'd suggest adding a higher priority value. The higher priority value will ensure that your filter is executed and not overriden by other filters in your theme or plugins installed. Please see below: (ref: https://developer.wordpress.org/reference/functions/add_filter/)
// the 9999999 priority value will force this filter to be executed closer to the end. A lower number corresponds with earlier execution
add_filter('wp_title', 'set_custom_title', 9999999, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
In my case Yoast SEO was changing the way title was rendered and only the following worked:
function filter_lp_title($title) {
return 'New title';
}
add_filter( 'pre_get_document_title', 'filter_lp_title', 25 );
I've been searching for a while now with no certain answer. I'm looking to append text to a WordPress site for prior posts that is moving from one domain to a new one and retaining content.
So what I want to do is, add "This article was originally posted at xyz.com." to all posts that were posted before today's date.
Right now this could be done through the database, or a WP functions filter, I'm okay with either option as long as it is long lasting.
Any suggestions on how to go about this would be appreciated?
You can use the_content filter that like this:
add_filter( 'the_content', 'old_wp_content' );
function old_wp_content( $content ) {
if( get_the_date('Y-m-d') < "2017-02-28" ) {
$content = "<p>This article was originally posted at xyz.com.</p>" . $content;
}
return $content;
}
This filter gets fired when you call the_content() of a post. with the_content filter you can adjust the return value of the the_content() function.
I finally got it. That extra "if" before the get_the_date statement and possibly the double quotation marks (swapped for single) wrapping the p tags for the inserted text were the culprits. The following code works:
function old_wp_content( $content ) {
if (get_the_date('Y-m-d') < '2017-02-28' ) {
$content = $content . '<p>This article was originally posted at <a
rel="canonical" href="#">xyz.com</a>.</p>';
}
return $content;
}
add_filter( 'the_content', 'old_wp_content' );
#kevinvhengst, thanks again for your time and patience in helping me figure this out!
I am just working on filter titles in pages and posts and everything works as expected but when I view the single.php whereas it shows the actual post as should be, the function hook filter i am working on inside functions.php of my plugin is filtering every title of each post of my blog, I was expecting to filter just the title of the current post:
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
/**/
}
}
any help is grateful.
Thanks ;)
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((some condition true)){
$add_this = 'some string';
$new_title = $title.' '.$add_this;
return $new_title;
}
return $title;
}
when you view any post it is shown through single.php so every post becomes current post when you view it
I understand now that for the filter title hook it is the same viewing a single post from single.php than viewing the whole blog. would there be a way of filtering with a condition to only retrieve the title of the current post in single.php?, I did try "get_the_title(get_the_ID())" like this but obviously I cant nest it.
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
if(is_single()){
$title=get_the_title(get_the_ID());
}
}
}
Hellow there all I tried searching everywhere but I coudn't just find it - may be because its a wiered requirement all I want to do is remove my Comment Text area in wordpress comment - I sucessfully removed URL website and name in the comment field by using following code
<?php
function remove_comment_fields($fields) {
unset($fields['url']);
unset($fields['author']);
unset($fields['email']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
?>
But not able to remove the Text area - Actually you all will thought what will I do removing all this I am using a plugin which allow you to post images in your comment and the only option I want to give to user is post images via comment. please guide me.
Two options, the comment_form_defaults filter:
add_filter( 'comment_form_defaults', 'so16856397_comment_form_defaults', 10, 1 );
function so16856397_comment_form_defaults( $defaults )
{
$defaults['comment_field'] = '';
return $defaults;
}
or the comment_form_field_comment filter:
add_filter( 'comment_form_field_comment', 'so16856397_comment_form_field_comment', 10, 1 );
function so16856397_comment_form_field_comment( $field )
{
return '';
}
Check the comment_form source code.
You can also hide the textarea comment box by taking its id set to display:none from CSS
Just simple, add this code in functions.php file
function disable_comments_everywhere( $open, $post_id ) {
return false;
}
add_filter( 'comments_open', 'disable_comments_everywhere', 10 , 2 );