WordPress: Conditional for Post template Not PAGE - php

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 :)

Related

WordPress conditional statement how to exclude several page templates and also a taxonomy archive page

I have code in my header that excludes certain templates. I need to change it to also exclude an archive page for my Custom Post type. The file name for the archive template is taxonomy-press_category.php.
But I don't know how to also include my custom post type archive template.
I am currently using:
if ( ! is_page_template ( array( 'homepage_template.php', 'services_template.php') ) )
I highly recommend reading the "Docs" first. It'll help you get your question answered quicker!
"I need to change it to also exclude an archive page for my Custom Post type"
To exclude an archive page you could use !is_archive() and !is_post_type_archive(). To include it, then remove the !.
is_archiveDocs
and
is_post_type_archiveDocs
if (is_post_type_archive('name-of-your-custom-post-type')){
// Write your code
}
Since you mentioned something about taxonomy, although it was not clear, is_tax() could be another conditional check that might be useful.
is_taxDocs
Again I highly recommend reading the "Docs", or at least, include the details of your problem in a clear way so that people could help you faster.
Thank you so much for your references, that helped.
This is the code that finally worked for me:
if( !is_page_template ('homepage_template.php') && !is_page_template( 'services_template.php' ) && !is_tax( 'press_category' )) {

How would you enqueue scripts in wordpress to show only on single posts & blog pages?

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

wordpress functions.php - use different page template for each post category

I want to hook into the save_post function, find out what category the post is in, and then assign a different page template for posts in each category. I've tried about 30 different versions of this with no luck. Will someone please help point me in the right direction?
add_action( 'save_post', 'assign_custom_template' );
function assign_custom_template($post_id) {
$category = get_the_category($post_id);
$cat_id = $category->cat_ID;
if( $cat_id == 1 ) {
update_post_meta($post_id, "_wp_page_template", "template1.php");
}
if( $cat_id == 2 ) {
update_post_meta($post_id, "_wp_page_template", "template2.php");
}
}
You just need to create category-1.php which rendered as template1.php and category-2.php which rendered as template2.php in your theme root.
See template hierarchy for more info.
I tried to emulate the official WP hierarchy scheme among my posts & custom post types, but it just wasn't happening. I ended up using Custom Post Types so that I could assign templates to both the "list" pages and the "individual" pages. And then I wrote some javascript that looks for the post-type string in the URL, and if it's detected, it adds the current_page_parent/ancestor classes to the appropriate menu items. Not perfect or totally future-proof, but it gets the job done.
If someone comes up with a better solution, please post it!

Checking for a post page template?

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
}

Wordpress Conditional if is_single

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

Categories