I want to programatically stop search engines to crawl a WordPress posts using PHP.
The scenario is, I'm creating a dummy posts along with user registration, I want it to be not searchable otherwise the user edit and save it already. Is there any way to do that?
Use below code snippet in your theme's functions.php file or custom plugin file.
function ar456_header_metadata() {
// Seperate Conditional Tag for Pages & single page in other post type
if( is_page( array( 'page-slug', 'post_id' ) ) or is_single( array( 'page-slug', 'post_id' ) ) ) { ?>
<meta name="robots" content="noindex,follow">
<?php }
}
add_action( 'wp_head', 'ar456_header_metadata' );
In the code, You can see two conditional tag is_page() and is_single(). The is_page() is for Page post type and is_single() for all other post type.
Update your Page ID, title or slug in conditional tags & meta tag will be available in these pages.
Example given below.
if( is_page( array( 'sample-page', '3' ) ) or is_single( array( 'hello-world', '6' ) ) ) { ?>
https://developer.wordpress.org/reference/functions/is_page/
https://developer.wordpress.org/reference/functions/is_single/
If you want all posts to be excluded, just add the following to your theme's <head> block:
<meta name="robots" content="noindex">
If you want some posts to be excluded, you'll have to include that meta tag conditionally according to whatever rules you want in place.
Ceejayoz has the perfect answer, but I don't have the reputation to comment directly. You will have to create a template that specifically blocks the robot and then call that template for the posts and pages you want omitting.
Related
I am trying to create a custom layout for one of our product pages in Woocommerce. I have searched and followed so many tutorials, but none are working for me. I copied 'single-product.php' and placed it into my active child theme in the folder 'woocommerce'. I then duplicated this and renamed it 'single-product-landing.php'.
I then placed the following code into my functions.php page:
add_filter( 'template_include', 'custom_single_product_template_include', 10 );
function custom_single_product_template_include( $template ) {
if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-landing.php';
}
return $template;
}
Then I tried changing items in single-product-landing.php but nothing is changing, so it can't be picking it up. To confirm, I have the product assigned to the 'custom' category.
I should also add that my product is a 'variable' product, am not sure if that has any affect on anything here.
EDIT: I have found the following code in my 'single.php' file - I think this may be interfering. Problem is, if I delete this nothing shows on any of my pages (even those not affected by the new template):
<?php
if(get_theme_mod('product_layout') == 'custom') {
wc_get_template_part( 'content', 'single-product-custom' );
} else {
wc_get_template_part( 'content', 'single-product' );
}
?>
Any idea how I can modify this to still have content show but not over-rule the template change that i'm trying to make?
Page templates are loaded after the template_filter has been run, so adding that code to a page template file will have no effect. To make that run, place the same code in your child theme's functions.php.
The 566 at the end of the add_filter line refers to the filter's loading priority and not a category id. Simply put, the higher the number the later the filter will be loaded.
Finally, your if statement can be simplified a little using Woocommerce functions - if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) { - doesn't make much difference, but it's tidier.
I'm looking to load a single custom post type as my homepage. Not an archive but a single post, ID 10190. Looking to do this without a redirect for SEO reasons. What would be the simplest way to achieve this?
Try this
add_action( 'template_redirect', 'so_48395149' );
function so_48395149() {
global $post;
if( is_front_page() ) {
$post = get_post( 10190 );
}
}
Note: I haven't tested this, but you'll be wanting something along these lines.
WordPress stores the currently viewed post in a global $post variable. In this case, we want to overwrite that with the specified post [type] only on the front page. We use the template_redirect hook as it's one of the last hooks available before the HTML is generated.
How would you enqueue a script in a theme that you only want to show on the blog and single posts?
I checked other questions here but didn't get convincing answer.
I got the following code from Wordpress site from a question as :
function enqueue_files() {
if ( is_page( 'your-page' ) ) {
// enqueue specific page script files here
} else {
// enqueue common scripts here
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
So after taking a look at is_page() function I am confused that as I need to only show them at single posts & blog pages and while the following function would work only for static pages and since I want it to be dynamic for all of single posts pages and blog pages so how would I be able exactly to do that then with which function?
Use is_singular. It combines is_page() with is_single().
Linky.
It'll also activate on attachment pages though. If that's a problem for you, just use is_page() || is_single().
You don't need to pass the page/post slug (and it'll actually break what you are trying to accomplish. So you just do:
if ( is_page() || is_single() ) {
// if ( is_singular() ) { // or this if you prefer. :)
// enqueue specific page script files here
}
If you want to detect the blogroll, use is_home, I thought you were only targeting single posts.
Link.
Enqueue specific scripts only for the blog homepage and single posts of post type post:
function enqueue_files() {
if ( is_singular('post') || is_home() ) {
// enqueue specific scripts for blog homepage and single posts of post type post
} else {
// enqueue common scripts here
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
Explanation
is_singular('post') checks if a singular post of specified post type post is being displayed (thanks #Umair Shah Yousafzai for this hint)
is_home() determines if the query is for the blog homepage
I have made a custom post template named single-download.php after replicating single.php. However there are certain customizations which I dont want to show on single-download.php but am not able get a specific conditional to achieve the same.
I have tried if(is_page_template( 'single-download.php' )) and also if(!is_page_template( 'single.php' )) but both don't work.
Is there some way in which we can achieve POST template conditioning? Thanks
Okay I finally figured it out. We need to use
if( !is_singular( 'download' )
OR
if ( is_singular( 'download' ) ) {
// conditional content/code
}
It was that simple :)
Is it possible to check what template is a single post type using?
I have a single post page and a single post portfolio page. Post page is single.php and portfolio is single-portfoilo.php. In my body I can see the classes loaded based on what page I'm on (what template is used).
I cannot use is_page_template(), because that will only work on pages, not posts. My alternative is to use is_single(), but that will target the regular posts, and I only want the portfolio posts to be targeted by the if clause.
Is there a workaround for this? It's really impractical :\
You can use something like
if ( is_singular( 'portfolio' ) ) {
// conditional content/code
}
Or if you have more than one custom post types you can insert an array like this
if ( is_singular( array( 'portfolio', 'book' ) ) ) {
// conditional content/code
}