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
}
Related
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.
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 :)
I am reading that query_posts() should be avoided in favor of wp_query() and pre_get_posts(). I am not confident with messing with the Loop and do not fully understand the codex.
Does the code below use query_posts() ? If yes and since query_posts() should be avoided, can you suggest a method that does not use query_posts() but still accomplish the same thing?
This code in functions.php is used to sort posts by random or by price.
function my_custom_query($query){
if ( $query->is_home() && $query->is_main_query() ) {
$sort= $_GET['sort'];
if($sort == "pricelow"){
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
if($sort == "random"){
$query->set( 'orderby', 'rand' );
}
}
}
add_action( 'pre_get_posts', 'my_custom_query' );
.
Link A (Random) and Link B (Price) are posted in my menu by using this code. Thus the visitor to the website can sort the posts simply by clicking a link.
Random
Price
I have done a very detailed explanation on this very topic on WPSE, and for the sake of the value and benefit it might have for SO users, here is the complete post copied from that question on WPSE. For interest sake, here is a link to the complete post on WPSE: Some doubts about how the main query and the custom query works in this custom theme?
Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts
PART ONE
When to run a custom query (This is not a definitive list)
To create custom content sliders
To create a featured content area in a page
On page.php templates if you need to display posts
If you require custom content on a static front page
Display related, popular or informational posts
Any other secondary or supplementary content outside the scope of the main query
When to make use of the main query.
To display the primary content on
On your homepage and the page set as a blogpage in the backend
All archive pages which includes templates like archive.php, category.php, author.php, taxonomy.php, tag.php and date.php
PART TWO
To select all the featured posts I use this line that create a new WP_Query object that define a query having the specific tag featured:
So, from what I have understand, this is not the WordPres main query but it is a new query created by me. From what I have understand it is better create a new query (as done) and not use the main query when I want perform this kind of operations
Correct. This falls out of scope for the main query. This is secondary or supplementary content which cannot be created with the main query. You SHOULD ALWAYS use either WP_Query or get_posts to create your custom queries.
NEVER USE query_posts to create custom queries, or even any other query. My emphasis.
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Moving on
Ok, going on I show all the posts that have not the featured tag, to do this I use this code snippet that on the contrary modify the main query:
query_posts( array( 'tag__not_in' => array ( $term->term_id )));
So I think that this is pretty horrible. Is it true?
That is all wrong and your statement is unfortunately true. As said before, NEVER use query_posts. It runs a complete new query, which is bad for performance, and it most cases breaks pagination which is an integral part of the main query for pagination to work correctly.
This is your primary content, so you should be using the main query with the default loop, which should look like this, and this is all you need
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile;
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
You can completely get rid of this part, delete it, burn it and forget about it
<?
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id )));
?>
OK, once you've done that, you'll see that posts from the feature tag appear in your home page using the main query and default loop.
The correct way of removing this tag from the homepage is with pre_get_posts. This is the proper way to alter the main query and the hook you should always use to make changes to your primary content loop.
So, the code with pre_get_posts is correct and this is the function that you should use. Just one thing, always do a check that you are not on an admin page because pre_get_posts alters the back end as well. So this is the proper code to use in functions.php to remove posts tagged featured from the homepage
function exclude_featured_tag( $query ) {
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
$query->set( 'tag__not_in', 'array(ID OF THE FEATURED TAG)' );
}
}
add_action( 'pre_get_posts', 'exclude_featured_tag' );
PART THREE
Extra reading material which will be helpful in future
Conditional tags
When should you use WP_Query vs query_posts() vs get_posts()?
When to use WP_query(), query_posts() and pre_get_posts
Query Overview
Guidance with The Loop for CMS
Creating a new WP_Query() object is always fine.
$sort= $_GET['sort'];
if($sort == "pricelow"){
$sort_args = array('meta_key' => 'price', 'orderby' => 'meta_value_num', 'order', 'ASC');
$new_query = new WP_Query($sort_args);
}
blah blah blah...
No no no sorry about that. I didn't see the pre_get_posts hook.
The code in your question is good for hooking queries. As in described in WordPress Plugin API/Action Reference/pre_get_posts:
pre_get_posts runs before WP_Query has been setup.
So it hooks the default WP_Query() where you want (in your code, it changes WP_Query on GET request).
In your template files, use new WP_Query($args).
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