Header on pages. (wordpress) - php

(my site is http://www.musaeus.dk/)
I have a Wordpress.org site and I want to make a button that takes the user back to the front page but I really can't figure out how. The button should be up in the top left corner and it shouldn't be on the front page.
If have tried putting this in the "header.php" file
<?php if(is_page(<page id>)) { ?>
--------- Header 1 -----
<?php } else { ?>
--------- Header 2 --------
<?php } ?>
but with no luck. If you have any ideas or know how I should do this I would really appreciate your help!

in your header.php file do something like this:
<?php
// check to see we are not on front page or on home page
if ( ! ( is_front_page() ) || ! ( is_home() ) ) : ?>
<div class="back-to-home">
Back to Home
</div>
<?php endif; ?>
and then style back-to-home class to match your needs.

Try this. It should works.
<?php
/**
* If page is front page,
* don't display button "Back to Home"
*/
if ( is_front_page() ) :
$btn = false;
else :
$btn = true;
endif;
if ( $btn === true ) : ?>
<div class="back-to-home">
Back to Home
</div>
<?php endif; ?>

Just add this CSS:
.home .back-to-home {
display: none;
}
The .home class is (only) in the body tag of your homepage, so that rule with this combined selector will hide the .back-to-home element only on the homepage

Related

php function to change css

I'm trying to add a piece of code to change css on a page. I added the code to functions.php in WordPress. However, it does not seem to work. Since I'm quite new to this there might be something quite basic wrong with the code... Any idea why it might not be working?
// This code is added to functions.php
// intro is the class name of the element I'm trying to change
add_action( 'intro', function () {
if ( is_user_logged_in() ) {
?>
<style>
display: none!important;
</style>
<?php
};
exit;
});
I got it to work by removing exit; and targeting an element:
add_action( 'wp_head', function () {
if ( is_user_logged_in() ) {
?>
<style>
.intro{
display: none!important;
}
</style>
<?php
};
});
I think what you are trying to do is change the content of the css class which I do not think you can do. Instead a solution would be to assign a css class with the propertied that you want applied to the element e.g.
<div class="<?php if ( is_user_logged_in() ) { echo 'intro';} ?>">
// Whatever you have here will get the css style applied
// if user is logged in
</div>
And inn The CSS you have the following
.intro{
display: none!important;
}
You can create multiple files for different styles e.g. another class
.outro{
display:initial;
}
And you can add it in the code as
<div class="<?php if ( is_user_logged_in() ) { echo 'intro';} else{ echo 'outro';} ?>">
// Whatever you have here will get the css style applied
// if user is logged in and if logged out then outro class will be applied
</div>

How to make so slider show up just in home page and anywhere else

PICTUREI have short code <?php echo do_shortcode('[wonderplugin_slider id=1]'); ?> and have to put it above the page see in the picture
The issue is the slider (shortcode) should display JUST on Homepage or Main page.
I have tried many plugins and codes like this <?php if(is_page(<111>)) { ?><?php }>but it does not work
You can use is_front_page(), is_home() and is_page() functions.
In your code, you want to mention the page id directly means please put it without any quotes like below.
<?php
if(is_page(111)){
echo do_shortcode('[wonderplugin_slider id=1]');
}
?>
Or else you already set the page with id 111 as the home page or front page means, you can use is_home() and is_front_page() function.
if( is_front_page() ) {
echo do_shortcode('[wonderplugin_slider id=1]');
}
or
if( is_front_page() && is_home() ) {
echo do_shortcode('[wonderplugin_slider id=1]');
}
Thanks.
You shpuld check both is_home() and is_front_page(), but their usage depends on the frontpage you have. If you have a statick frontpage you can have:
if( is_front_page() ) {
...
}
In the other way around you can conbine both like:
if( is_front_page() && is_home() ) {
...
}

Redirecting logged out users from specific pages

I have a wordpress website, its using buddypress and bbpress. I need to hide/redirect all the buddypress and bbpress pages from people who are not logged in. So if someone lands on the members page, profile page or any forum topic it needs to redirect them to the signup page.
I tried maybe 5 plugins, all of them caused issues like 404 errors, not working or just white pages.
The url structure is like this:
www.example.com/members
www.example.com/members/luke
www.example.com/forums
www.example.com/forums/forum/general-chat
Does anyone know how I can do this without a plugin?
you have to modify from within a child theme the profile-loop.php file
your-child-theme/members/single/profile/profile-loop.php
On the first line of the file, add
<?php if ( is_user_logged_in() ) : ?>
At the end of the file, insert between the last endif and the last do_action this:
<?php else : ?>
<?php echo “<div style=’width: 600px;height:25px; padding: 4px; border: 3px solid #ff0000; text-align: center; font-style:bold; font-size: 1.3em;’> You must be logged in to view a member profile</div>”; ?>
<?php endif; ?>
Change the div inline style to whatever you need accordingly to your theme. The example fits with bp-default.
If you can not do that, then try this plugin,
plugin
Try this but make sure to change the url to what you want
add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );
function redirect_non_logged_users_to_specific_page() {
if ( !is_user_logged_in() && is_page('add page slug or i.d here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/page/' );
exit;
}
Try this in your theme/functions.php or in bp-custom.php:
function lukedi_private_check() {
if ( ! is_admin() && ! is_user_logged_in() ) {
if ( is_front_page() || is_home() || bp_is_register_page() || bp_is_activation_page() )
return;
$redirect_url = trailingslashit( site_url() ); // change this to whatever you need
// member page
if ( bp_is_user() )
bp_core_redirect( $redirect_url );
// bbPress
if( is_bbpress() )
bp_core_redirect( $redirect_url );
// members loop
$bp_current_component = bp_current_component();
if ( false != $bp_current_component ) {
if ( 'members' == $bp_current_component )
bp_core_redirect( $redirect_url );
}
}
}
add_action( 'bp_ready', 'lukedi_private_check' );

Show header only on shoppage Wordpress

Took me some time to find out a working code to show the header above the sidebar.
But I now have the problem that I just want to show the header on the shop-page and not on the product pages.
So I'm trying to figure out what do I need to change?
add_action('woocommerce_before_main_content','before_main_content');
function before_main_content() { ?
<img src="image url">
<?php
}
?
Thank you guys for all the answers :)
This one worked for me :
function before_main_content() {
if (is_shop()) {
?>
<img src="image url">
<?php
}
}
?>
Replace *** with your shop page name:
<?php
if ( is_page('***')) {
**Insert your header code here**;
}
?>
This works for me :)
Use the page conditional, is_page(), to determine if you should show the image. WooCommerce has a function to get the shop page ID. Test for it and return if it's not a match.
Example:
function wpse_before_main_content() { // before_main_content could be problematic, choose a prefix.
// Check if we're on the shop page.
if ( ! is_page( woocommerce_get_page_id( 'shop' ) ) ) {
return false;
} ?>
<img src=". . .
<?php
}
add_action( 'woocommerce_before_main_content', 'wpse_before_main_content' );

Trying to add two different strings in PHP

I came up with an solution for not showing an sidebar in Wordpress when this is not filled with widgets. I did this by calling for the slug from the current page and named my sidebars the same as the slugs from my pages.
I use the Custom Sidebars plugin to add sidebars. This plugin adds 'cs-' in the id of a sidebar before the name of the sidebar. So for example I've named my sidebar 'Test' than the id of the sidebar will be 'cs-test'.
Now I want my php code to check if the sidebar is named the same as the slug from the page. So I called the_slug and before the slug I want to add cs-. With some help I came up with this:
<?php if ( is_active_sidebar( 'cs-'.the_slug(false) ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( 'cs-'.the_slug(false) ) ) ?>
<?php } else { ?>
<?php } ?>
But that doesn't seems to help. Any thoughts how to fix this? Thanks already!
Your are concatenating a string cs- with a function the_slug(false). This might cause the error, depending on what the function is returning.
Solution 1:
You could call and save the function beforehand and concate two vars:
<?php
$slug = the_slug(false); //Get return value
$slug = (string)$slug; //Force string conversion
$slug = 'cs-'.$slug; //Concate
if ( is_active_sidebar( $slug ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( $slug ) ) ?>
<?php } else { ?>
<?php } ?>
.
Solution 2:
If you need this type of functionality often try to add a string parameter to the the_slug() function (should be a custom function in your functions.php) and do the concatenating within the function itself:
function the_slug($boolen, $string='') {
//Your code
return $string.$your_var;
}
Your sidebar code would look like this:
<?php if ( is_active_sidebar( the_slug(false, 'cs-') ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( the_slug(false, 'cs-') ) ) ?>
<?php } else { ?>
<?php } ?>

Categories