How to use If condition for wordpress plugin Widget Logic - wordpress

I'm trying to put an IF condition on a widget through the plugin Widget Logic.
if single post is 'this', then dispay this text.
I tried this but it doesn't work, do you know why ?
if ( is_single( 'dialyse-mayotte' ) ) {
echo 'test'; // conditional content/code
}
thanks for your help

is_single only checks if the page is single you should check if the page is this and is it a single page
if ( is_page( 'dialyse-mayotte') && is_single() ) { echo "test"; }

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?

Skip Certain Gutenberg Block in Loop

I have some WordPress Gutenberg blocks that I want to display in a different part of my theme. I know how to find the block and display it in my theme's template, but I don't know how to stop the block from showing with the rest of the blocks in the loop.
To clarify as an example: I have one block that I want to have in the admin area with the other blocks but I don't want to display it in the loop content. I need a way to skip it or filter it out in the normal loop. Then I use this code to output in another area of my theme:
function be_display_post_blockquote() {
global $post;
$blocks = parse_blocks( $post->post_content );
foreach( $blocks as $block ) {
if( 'lazyblock/area-2' === $block['blockName'] ) {
echo render_block( $block );
break;
}
}
}
The problem with the above code is it duplicates the block to show with the other "normal" loop content and also in my new location. Can you help me write a function/filter to stop specific blocks from showing up in the loop?
I figured this out...
Here is the function to filter the content of the loop and remove a specific block from the output because you already outputted the content of that specific block in another location in your theme or theme template file.
//If single block exists on page or post don't show it with the other blocks
function remove_blocks() {
// Check if we're inside the main loop in a post or page
if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
//parse the blocks so they can be run through the foreach loop
$blocks = parse_blocks( get_the_content() );
foreach ( $blocks as $block ) {
//look to see if your block is in the post content -> if yes continue past it if no then render block as normal
if ( 'lazyblock/top-of-page' === $block['blockName'] ) {
continue;
} else {
echo render_block( $block );
}
}
}
}
add_filter( 'the_content', 'remove_blocks');

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;
}

hide wordpress posts and categories for not logged in users

Basically I handled this issue by overriding parent theme files in child theme.
i.e., by using
if( is_user_logged_in() ){
...................
wordpress loop
}
I had to do this for every template ( wherever there is wordpress loop )
Though I didn't have to do this for displaying sidebars as if there was category in widget and whenever user clicks on it to view it, it would automatically say you don't have proper privileges to view this content.
So, my question is, is there any better way to hide wordpress content ( this may be anything, like normal posts, custom post types,.. ) from just visitors.
function bt_hide_from_guestes( $content ) {
global $post;
if ( $post->post_type == 'post' ) {
if ( !is_user_logged_in() ) {
$content = 'Please login to view this post';
}
}
return $content;
}
add_filter( 'the_content', 'bt_hide_from_guestes' );
Above code didn't help
It looks like your filter function should work for you. Try setting a priority and the "accepted arg" number:
add_filter( 'the_content', 'bt_hide_from_guestes', 10, 1 );

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
}

Resources