Hi!
So I'm new to this and not exactly the greatest at PHP, but I'm having a go anyway.
What I want to do is remove the title elements from pages, but not from posts. I found the code snipit to achieve removing the post title, but obviously this removes it from posts and pages, which isn't what I want.
I've tried doing it by removing the post header
beans_remove_action( 'beans_post_header' );
But that's probably the wrong syntax as it doesn't achieve anything.
Sorry if this issue has already been tackled, I've searched the forum and couldn't find anything the same but I appologise if I missed it!
If anyone has any ideas/the answer I'd be very grateful.
Cheers, Adam
Hello Adam,
Welcome to Beans!
You want to remove page titles. Okay, here's a snippet of code for you to put into your child theme:
beans_add_smart_action('beans_before_posts_loop', 'remove_page_title_on_pages_only' );
/**
* Remove the page title on pages.
*
* @since 1.0.0
*
* @return void
*/
function remove_page_title_on_pages_only() {
if ( is_singular() && is_page() ) {
remove_all_actions( 'beans_post_header' );
}
}
This code works just before the Loop starts looping. Then it checks if this is a single "page" post type. If yes, it removes all of the actions for the beans_post_header
. That means the header's HTML will not be built and rendered out to the browser.
A Word of Caution - SEO
I do want to caution you though. SEO will be affected when a web page does not have a `` element.
Tonya Mork@ I've a question. I can achieve the same effect using any of the follwoing codes too.
if ( is_singular() && is_page() ) {
beans_remove_action('beans_post_title');
}
or
if ( is_singular() && is_page() ) {
beans_modify_action_hook( 'beans_post_title', '__return_false' );
}
I'm sure your code snipped is more adavanced than mine. My question is among the 3 code snippets (including yours too) which one is actually best and which one I must not use and why?
Thanks in advance
Hello again,
So doing beans_remove_action('beans_post_title');
removes just the post title, but not the header itself.
What do I mean by that?
By default on a page such as the "About" page, the HTML looks like this:
<header data-markup-id="beans_post_header">
<h1 class="uk-article-title" itemprop="headline" data-markup-id="beans_post_title">
<!-- open output: beans_post_title_text -->
About
<!-- close output: beans_post_title_text -->
</h1>
</header>
When when we do beans_remove_action('beans_post_title');
, here is what is rendered out to the browser:
<header data-markup-id="beans_post_header"></header>
The element is still there. Why? Because `beans_remove_action('beans_post_title');` only removes the
element.
Which is Better?
It's a better practice to remove empty elements that are not going to be used.
If there are cases where you'll dynamically add meta data or styling regardless if the post title is there, then leave the `` element. Otherwise, I'd recommend removing it.
Thanks very much for this, finally got around to playing with this again and it's worked perfectly.