Different header in different pages in wordpress - wordpress

I am going to develop a WordPress theme for my own site. It has three types of menu.
first one is only for the home page, the second one is for any normal page (like about us, contact etc.) and the third one is for article page (when a news will be clicked then where one will be gone).
how can I do this?
thanks.

There are multiple ways you can go about doing this. For the article I would create a PHP file named page-article.php and within the file follow what developer tells you:
<?php
/**
* Template Name: Article
*
*/
and then create a custom header for the article in a header-article.php file:
header('article') ?>
To target the home you could use is_page() and pass it home but I would suggest using is_home() instead but you could always pass both like:
if ( is_page('home') || is_home() ) :
// further code
endif;
Do note this would depend on your settings as you do not mention if you will have a custom page set by the setting under Settings -> Reading so if you set front page you should target it with is_front_page():
if ( is_page('home') || is_home() || is_front_page() ) :
// further code
endif;
You could always code all of this in one header.php file but I would suggest creating an additional header file and calling it header-article.php

You can do it using is_page() function.
See full documentation here
if( is_page( array( 'about-us', 'contact') ){
//display your page menu
}
if( is_page( 'home' ){
//display your home page menu
}
if( is_page( 'article' ){
//display your article page menu
}else{
//display default menu
}

If you need different header in different page then you have to create a page template then you can add different header file in different page template-
Normally we use get_header() to include header.php
Now create a copy of header.php file and named it header-alternative.php
Then create a page template named "Alternative header"
<?php get_header('alternative');
/*
*Template Name: Alternative Header
*/
?>
<section class="error-page-inner">
<h1 class="error-code"><?php esc_html_e( '404', 'labstore' ); ?></h1>
</section>
<?php get_footer(); ?>
Then header-alternative.php is called for this page template.

Related

How to repeat ACF Flexible Content on single pages?

Disclaimer: If you have never used ACF Flexible Content to build a site, please reserve the hate and all that - looking just for help.
I'm creating a website that's 100% modular, done with Flexible Content.
I have Pages such as:
Home, About, Services, Blog, Portfolio, Contact
All of those pages styled using Flexible Content, these are PAGES.
I have created a custom field for Services and Portfolio, and I used WP Query to display each individal item. These are CUSTOM fields. Show this field group if post type to Services. Not a page.
So when you click it, what happens you go to single-[custom-post-type].php, however I just have single.php to keep it dynamic, which works.
The problem lays that when I have showed the flexible content page templates in the Services or Portfolio, and I add few items, I need go to every single item and set the layout.
How can I do so the layout stays all the same in Service or Portfolio custom field?
Firstly create your ACF Master block, for example, a testimonial section, this would have the repeater fields inside the block etc. In this example, we will call it Testimonial Master.
Then Create another Flexible Content Field Called Testimonial Block with a checkbox that you check to display or hide.
You will now populate Testimonial Master on your selected Parent Page or on an Options page. You then should add the Testimonial Block to any page you are looking to have that block appear on.
You will then went to check that the Testimonial Block is on the page, if it is on the page then you will check if Testimonial Master has a value if it has a value then you can pull in the values from Testimonial Master.
<?php
if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) :
the_row();
if( get_row_layout() == 'testimonial_block' ):
if( get_sub_field('testimonial_block_toggle') == 'show' ):?>
<div class="testimonials">
<?php the_sub_field('testimonial_master_title', 1); ?>
</div>
<?php endif;
endif;
endwhile;
endif;
?>
The "1" is the page ID, use this if you have populated the field on a master page. Or if you are using the ACF Options Page plugin change the "1" to "'options'".
Here is some further documentation on getting values from other posts including that of repeater fields.
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
Edit: Custom Header as per your example
Make a custom header file and add your code for that header block.
Now make an advanced custom field for a text area. For example header_text_block, set it to a WYSIWYG editor.
Now in your code just use:
<?php the_field('header_text_block');?>
This will then use the exact same header on every page you pull it into, however it will let you edit the text individually for each page.
Edit: Example of what this code looks like and does
Also I think you are very confused, there will only be 1 php/html file? you could use page.php.
Here's an example of what I am saying, 1 php file:
<?php if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) : the_row();
if( get_row_layout() == 'page_featured' ):
$image = get_sub_field('page_featured_image');
if( !empty($image) ):?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
<?php endif;
endif;
if( get_row_layout() == 'page_breadcrumbs' ):
if( !in_array( 'hide', get_sub_field('page_breadcrumbs_hide') ) ):?>
<?php if ( function_exists('yoast_breadcrumb') ) {yoast_breadcrumb('<p id="breadcrumbs">','</p>');}?>
<?php else :
endif;
endif;
endwhile;
endif;
?>
This then looks like this on the page editor:
You can then edit each section as you please and also drag and drop sections where you want them which enables easy split testing.
All the styling will be the same, just you can change the content and if you wanted you could add the ability to alter stylings pretty easily. And it only uses 1 PHP file for the template.

How to handle a custom url with Wordpress?

i want to handle a custom url with WordPress theme files, for example http://www.example.com/wordpress/login
the question is that how i handle this /login with WordPress theme files like page.php , index.php or a custom template file for this reason, if exists!.
This plugin might be useful to you.
https://wordpress.org/plugins/login-logout/
It appears you could create a page called 'Login' which Wordpress would automatically create as http://www.website.com/login/
This plugin appears to be a widget, but I like that it has the ability to manage where a user is re-directed after successful login/logout.
You could register a new widget in functions.php, then call the widget with a conditional to the 'Login' page that way it could be in the main content area rather than look like a widget.
Example:
<?php } if ( is_page(9) ) { ?>
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('login-widget') ) : ?>
<?php endif; ?>
<?php } else { } ?>
My example would assume page 9 = your login Page and 'login-widget' would be the name of the widget you assigned in functions.php

Check wordpress shortcode is exists on home page

Is there any way to know specific shortcode is applied on specific page?
for e.g. On any WP Theme's home page if related_products shortcode is applied then I want to check it in my plugin file that is applied or not?
Is there any way?
You can use has_shortcode() to check if your shortcode is applied. If you need to specifically check if it is used on the home page, you can add the is_home() check
global $post;
if( is_home() && has_shortcode( $post->post_content, 'related_products') ) {
echo "YEAH, shortcode is applied on home page";
}else{
echo "Houston, we have a problem!!!";
}

wp_title() not working properly with blog in non-root folder

I followed this guide to have my blog appear under mydomain.com/blog:
http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder
In short, I'm using a custom page template to create a static page "Blog", which then goes and renders the posts:
<?php
/*
Template Name: Blog
*/
// Which page of the blog are we on?
$paged = get_query_var('paged');
query_posts('cat=-0&paged='.$paged);
// make posts print only the first part with a link to rest of the post.
global $more;
$more = 0;
//load index to show blog
load_template(TEMPLATEPATH . '/index.php');
?>
In my theme, I render the <title> tag using the following syntax:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
This is described here in the codex:
http://codex.wordpress.org/Function_Reference/wp_title#Covering_Homepage
The problem is that now on mydomain.com/blog, there is no title rendered by wp_title('');
If I stop using the blog.php template, then the title appears correctly. Obviously the blog posts do not anymore though. How to have the wp_title() display the correct title in this situation?
You should be following the updated guide for Wordpress 2.1+:
http://codex.wordpress.org/Creating_a_Static_Front_Page

WordPress page content in Modal Pop-Up

I am working on a WordPress gallery website which has only three pages: Home, Gallery, and Bio (http://adamgreener.com/).
When you click Bio, the Bio content pops up, powered by Easy Modal plugin (the content is manually typed in HTML in the plugin settings).
I also have the exact same HTML content in a WordPress page (which mobile viewers see, rather than a popup).
The page is very simple to edit, but the Modal content is not so friendly to the average user. I am seeking a way that I can allow the user to edit only the Bio page, and have that modal content update at the same time.
What would be the best route for such an action?
Thanks in advance!
You could use get_page to get the page content, and a shortcode to display it in the popup. In the functions.php file of your WordPress theme, e.g.:
add_action( 'init', 'greener_shortcode_init', 11 );
function greener_shortcode_init()
{
add_shortcode( 'greener_bio', 'greener_bio_shortcode' );
}
function greener_bio_shortcode( $atts )
{
$page_id = 123; // the ID of the Bio page
$page_data = get_page( $page_id );
$return = '';
$return .= '<h2>' . $page_data->post_title . '</h2>';
$return .= $page_data->post_content;
return $return;
}
Then, in your modal, use the shortcode:
[greener_bio]
There are two possible ways to accomplish what you're going for, both described in depth in my blog post: https://allurewebsolutions.com/open-wordpress-post-modal-without-plugin
You can also just use my plugin for this: https://wordpress.org/plugins/wp-post-modal/
First Method
The first method requires modifying the template that serves the content you want to appear in the modal. For example, you have a template called modal-content-template.php.
Inside that template we wrap the content we want loaded into the modal with:
<?php if (!$_GET) { ?>
// content that gets loaded on mobile
<?php } ?>
Full example of a template where we are excluding the WordPress header and footer:
<?php if (!$_GET) { get_header(); } ?>
<article>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="page-title"></h1>
<section class="entry-content cf">
<?php the_content(); ?>
</section>
</article>
<?php if (!$_GET) { get_footer(); } ?>
CodePen example for the JS required to make this work
Second Method
The second method is a little more elegant. We use the modalContent.load(post_link + ' #modal-ready'); method to load the modal content.
The modal contact is wrapped in a wrapper container with ID #modal-ready. This can be done without even touching the page template using a function that hooks into the content.
add_action('the_content', 'wrap_content');
function wrap_content($content)
{
return '<div id="modal-ready">' . $content . '</div>';
}

Resources