I am not able to selectively disable plugins in mu-plugins folder using the is_singular() functions in a conditional. It works with functions such as is_user_logged_in etc..
But i need to disable plugin for posts hence trying the is_singular() but again it does not work in mu-plugins on a multi site, even if I declare the blog id.
I also tried many version such as 'post' == $post->post_type still no luck. Any ideas for a working conditional in muplugin for multisite to check for post types? Thanks
is_singular() conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type portfolio.
is_singular( 'portfolio' );
Try to add custom post type in condition.
Related
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
}
Im trying to use a conditional statement on my single.php page.
What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.
I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):
if ( is_single( 'current-products' == get_post_type() ) {
// If the post type is "Current Products" use this template...
// Please help me out on this bit
} elseif ( is_single() ) {
// If not, use the standard one...
// please help me out on this bit
}
I think that's right...?
WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex
In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
where {post_type} is the $post_type argument of the register_post_type() function.
You can use is_singular() function, according to WordPress documentation: Ref Link
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type book.
is_singular('book');
This will accomplish your task.
I believe if you want to use for posts try this:
is_singular('post');
Thanks