I need to display HTML code to all user roles except "subscriber" in wordpress.
Here is code that I can't get working.
<?php
$current_user = wp_get_current_user();
?>
<?php if ( $current_user->role == 'subscriber' ) : ?>
<span>here is my html</span>
<?php endif; ?>
PS
I'm not that good with php as you can tell.
Solved! I used following code that gives desired functionality:
<?php if ( $this->current_user_can_edit_posts ) : ?>
<span>my html</span>
<?php endif; ?>
For others, you might need to change "$this" part. So you can retrieve current_user. Maybe someone can still post an answer for them.
Tried and tested code that works. Let me know if it works for you too! :)
<?php
$current_user = wp_get_current_user();
if ( ! in_array( 'subscriber', (array) $current_user->roles ) ) {
?>
<span>here is my html</span>
<?php
}
?>
Related
I'm setting up a client area in wordpress, and I'm going to use the author part to choose the client that is associated with the project.
In the posts grid part on the frontend, I would like only the posts of the current user to appear.
That is, each user will only view their own posts.
I found some similar articles but nothing about what I really need.
You can create shortcode easily like this:
add_shortcode('currentauthorpost', 'ak_currentauthorpost');
function ak_currentauthorpost(){
if ( is_user_logged_in() ):
global $current_user;
wp_get_current_user();
$author_query = array( 'post_type'=>'post', 'posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else :
echo "not logged in";
endif;
}
you can use simply this shortcode
[currentauthorpost]
I am using the user registration plugin (https://wpeverest.com/wordpress-plugins/user-registration/). When a user signs up, there is an option to ask them if they're 'new team' or not. If they check 'new team' I then want to display text when they are signed in.
I have tried the following:-
<?php
$user = wp_get_current_user();
if ( in_array( 'msp_team', (array) $user->user_registration_msp_team ) ) {
echo ('TESTTTT');
}
?>
But the text is not displaying. Could someone please advise what I am doing wrong here?
Thank you!
Managed to sort this with the following code:-
<?php if ( get_field( 'user_registration_msp_team', 'user_' . $current_user->ID ) ): ?>
FIELD CODE....
<?php else: ?>
OTHER CONTENT IF FIELD IS EMPTY
<?php endif; ?>
I have an ACF true or false based if the condition to show a section based on the mentioned code. it works correctly on the front page/homepage of WordPress. but it doesn't work in the inner pages, not getting the values of the field and I use this code in footer templates.
its a WordPress site with the latest version. and the code is in the footer template
<?php
$show_company_info = get_field( "show_company_info", get_the_ID() );
if($show_company_info ):
?>
<section class="company">
<div class="container">
<?php if ( is_active_sidebar( 'company-info-widget' ) ) : ?>
<?php dynamic_sidebar( 'company-info-widget' ); ?>
<?php endif; ?>
</div>
</section>
<?php endif; ?>
if we checked the ACF true/fields condition in any page it will show the section otherwise not. but it actually works in homepage only
Assuming that you have created a field in a Home Page.
So, to retrieve the home page field you need to do like this :
$frontpage_id = get_option( 'page_on_front' );
$show_company_info = get_field( "show_company_info", $frontpage_id );
if ( $show_company_info ) {
//Your code
}
I am trying to display specific text at the bottom of a menu but I only want this text to show if I am looking at a certain menu. This is the code I have:
<?php get_post_type( $post ) ?>
<?php if ( $post->post_name === 'belden-village' ) : ?>
<?php echo "<p>*For all groups of 8 or more, an 18% gratuity will be included on your guest check. Thank You</p>"; ?>
<?php endif ?>
This code works in staging but as soon as we moved the site live it no longer displays the text when it is supposed to. I am calling it by the "slug" and the slug is the same in both staging and live site. Any ideas why this has suddenly stopped working? This is my first experience taking a site live so any help would be greatly appreciated. Thanks.
Why not use the is_single() function?
<?php get_post_type( $post ) ?>
<?php if ( is_single( 'belden-village') ) : ?>
<?php echo "<p>*For all groups of 8 or more, an 18% gratuity will be included on your guest check. Thank
You</p>"; ?>
<?php endif ?>
If you are looking for a page instead of a post you can use the is_page() function instead:
<?php get_post_type( $post ) ?>
<?php if ( is_page( 'belden-village') ) : ?>
<?php echo "<p>*For all groups of 8 or more, an 18% gratuity will be included on your guest check. Thank
You</p>"; ?>
<?php endif ?>
I'm a bit new to this, but I researched this topic a bit online and can't seem to figure out what I'm doing wrong. So basically, I created a duplicate footer to call for the homepage. We use a marketing automation tool called Pardot and we don't want to track visits to the homepage. So I created a file in the footer folder next to "footer-default.php" called "footer-nopardot.php" that omits the code.
In the home.php file, I edit the bottom with
<?php
get_footer('nopardot'); ?>
But it appears the homepage is still calling the default footer. Any advice on what to do or what I'm doing wrong?
Thanks!
just add this to your home template:
<?php
if ( is_home() ) :
get_footer( 'nopardot' );
else :
get_footer();
endif;
?>
you can use this loop just replace the home or 404 by your page name where you want to display your footer:
<?php
if ( is_home() ) :
get_footer( 'home' );
elseif ( is_404() ) :
get_footer( '404' );
else :
get_footer();
endif;
?>
and since you are just changing your home page footer u can just do this
<?php
if ( is_home() ) :
get_footer( 'yourName' );
else :
get_footer();
endif;
?>
Make sure of the file name to be footer-yourName.php
and then call it as follow:
get_footer( 'yourName' );
hope that helps :)