Wordpress is_page() for blog post listing page - wordpress

I'm trying to use an is_page() conditional in Wordpress to detect whether or not the current page is the blog listing page (or a blog post page actually). The conditional works fine for any other page (contact, whats-on etc) but for some reason it doesn't work for the blog page.
Plus the actual blog post pages - URLs are www.domain.com/the-post-title format, so I can't check for 'blog' in the URL from $_SERVER or anything. Any help would be hugely appreciated.

We need to check:
if this page is blog page.
if this page is "page for posts"
function is_page_for_posts() {
$result = false;
if ( is_home() && ! is_front_page() ) {
$page = get_queried_object();
$result = ! is_null( $page ) && $page->ID == get_option( 'page_for_posts' );
}
return $result;
}

You may need to do a combination of is_home() - if your home page is the blog
or is_single() based on the type of blog post
reference: http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page

According to this article, this is how you use these kind of conditional functions:
if ( is_front_page() && is_home() ){
// Default homepage
} elseif ( is_front_page()){
//Static homepage
} elseif ( is_home()){
//Blog page
} else {
//everything else
}

Related

Disable Elementor on specific page

I have a plugin that uses the default woocommerce my account shortcode and displays the page only for a specific role. But, I have customized the My Account page using elementor & crocoblock, and the plugin is not working properly. They did give a workaround for this in the documentation, but I can't seem to make it work. This is the code
add_filter( 'elementor/frontend/the_content', 'wcb2bsa_disable_elementor_on_sales_agents_area' );
function wcb2bsa_disable_elementor_on_sales_agents_area( $content ) {
if ( is_account_page() && is_user_logged_in() && wcb2bsa_has_role( get_current_user_id(), 'sales_agent' ) ) {
return '[woocommerce_my_account]';
}
return $content;
}
Is it because the elementor/frontend/the_content part is changed?

Trying to load separate headers for home, page, post, and category - only category doesn't work

Since I have different Google Ad Manager codes that need to load in the of different content types, I created separate headers for each type and put this code snippet in index.php to load them accordingly:
if ( is_front_page() ) {
get_header('home') ;
}
elseif ( !is_front_page() AND is_page() ) {
get_header('page') ;
}
elseif ( is_single() ) {
get_header('article') ;
}
elseif ( is_category() ) {
get_header('category') ;
}
else {
get_header() ;
}
All of them work great - except for the category. It isn't loading. It just loads the default header.
What am I doing wrong?
Any help is much appreciated!
The short answer would be to try and use is_archive() instead of is_category(). If that doesn't work, you may need to dump your global $wp_query variable and see what query types are being returned on that particular url.
In that same vein, you may consider using is_singular() instead of is_single(), because is_single is limited to Posts.
One more point to consider is using && instead of AND (they're synonymous other than precedence. Take a look at this answer for a bit more in-depth on it)
Lastly, if you're only loading in a separate Google Ad Manager code, I'm not sure you need to maintain x number of header files. Have you considered dropping in the script codes into your functions.php file and loading them on the wp_enqueue_scripts or wp_head hooks?
instead of maintaining separate headers like this:
if( is_front_page() ){
get_header( 'home' );
} else if( is_page() ){
get_header( 'page' );
} else if( is_singular() ){
get_header( 'article' );
} else if( is_archive() ){
get_header( 'category' );
} else {
get_header();
}
You could drop those scripts straight in based on the same is_ functions using something like this:
add_action( 'wp_head', 'load_ad_manager_scripts' );
function load_ad_manager_scripts(){
if( is_front_page() ){
echo '<script>// Home Ad Manage Code</script>';
} else if( is_page() ){
echo '<script>// Page Ad Manage Code</script>';
} else if( is_singular() ){
echo '<script>// Single Manage Code</script>';
} else if( is_archive() ){
echo '<script>// Archive Manage Code</script>';
} else {
echo '<script>// Generic Manage Code</script>';
}
}

How to check WooCommerce thank you page

In Wordpressis_page() can check page by ID,name or by slug but how can we check WooCommerce thank you page whether its a part of checkout page.
Also We have a lot of WooCommerce conditional tags but cant find something solve my issue
for example I try
if(is_page('checkout')) {
//some thing for only checkout page
}else if(is_page('thankyou') && !is_page('checkout')){
//some thing for only thank you page but not on checkout page
}else{
//some thing for all other page
}
This sample code may work:
if ( is_checkout() && !empty( is_wc_endpoint_url('order-received') ) ) {
...
}
I think better to use endpoint like
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
//Get Order ID
$current_order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
echo $current_order_id;
}

WordPress: How can I check if a search result is a post of a page?

In my search archive view, some of the results will be posts, and some will be pages. I want to give posts and pages different visual treatments in the template.
I've tried using if (is_post()) and if (is_page()) with no luck. How can I accomplish this?
The is_page() depends on how the request maps to the main query variables, so you could instead check the post type within the loop:
the_title();
$post_type = get_post_type();
if( 'post' === $post_type ) {
// it is post, and style it like the way you need
} elseif ( 'page' === $post_type ) {
// it is page
}

How to load different Bloginfo value for top level pages?

I am no PHP programmer therefore I have been scratching my head for two hours on this issue as there seems to be many ways of doing it. None of which I managed to get working because of my poor PHP/Wordpress syntax and logic knowledge.
What is the best way to create and call a function that loads a different value for <?php bloginfo('name') ?> only on top level pages? i.e. top level menu headings.
I was wondering if there would be the possibility of doing something along these lines:
<?php
if ( is_page( 'about' ) || == $post->post_parent ) {
bloginfo = 'about';
} elseif ( is_page( 'work' ) || == $post->post_parent ) {
bloginfo = 'work';
} elseif ( is_page( 'contact' ) || == $post->post_parent ) {
bloginfo = 'contact';
} else {
bloginfo('name');
}
?>
Please treat it as pseudo code as I am still familiarizing myself with the wordpress and PHP codex/syntax, hence why I cannot get anything to work. But basically, what I need to do is use the main nav link names as the bloginfo name for every page in their section. Anything else is an exception which defaults back to the home bloginfo name.
Would it be easier to try to parse the nav link titles themselves into the function?
Please help if you can!
UPDATE
I have completely ditched the previous option and went the following route:
<?php if (is_page() || is_single( array( 62, 57, 51, 8 ) )) {
echo wp_title('');
}
else{
$category = get_the_category();
echo $category[0]->cat_name;
}?>
It is kind of botched but after reading lots of reference material on the wordpress site, I think I am beginning to understand the syntax. So, instead of targeting bloginfo() I am now targeting the page title. However I do not want the posts to have a huge title therefore I am making all pages other than top level "pages" pick up the first category instead, except for a handful of posts which are in fact custom pages, so need to display the wp_title value.
If anyone has any tips on turning this into a more flexible/efficient function please drop a post otherwise I will make this as resolved in a couple of hours.
the syntax would be
<?php
if ( is_page( 'about' ) || == $post->post_parent ) {
bloginfo('about');
} elseif ( is_page( 'work' ) || == $post->post_parent ) {
bloginfo('work');
} elseif ( is_page( 'contact' ) || == $post->post_parent ) {
bloginfo('contact');
} else {
bloginfo('name');
}
?>
but there are no such parameters like about,work,contact to the bloginfo function in wordpress.
For more information on bloginfo, please check the this url -> http://codex.wordpress.org/Function_Reference/bloginfo
You may try this to filter/change the name using bloginfo filter (just add this code snippet in your functions.php file)
function my_custom_blogname( $output, $show )
{
if( $show == 'name' )
{
if( is_page('about') ) $output = 'It\'s my about page';
if( is_page('work') ) $output = 'It\'s my work page';
if( is_page('contact') ) $output = 'It\'s my contact page';
}
return $output;
}
add_filter('bloginfo', 'my_custom_blogname', 10, 2);
Update : Also you can check if the page is parent/top lavel page using
if( !$post->post_parent ) // To check only if it's a parent/top lavel page
if( is_page('about') && !$post->post_parent ) $output = 'about'; // To check if it's a parent/top lavel page and about page

Resources