Showing secondary nav on one page wordpress - php

I have this bit of code in my header:
<?php if (has_nav_menu('sub-header-menu', 'responsive')) { ?>
<?php wp_nav_menu(array(
'container' => '',
'menu_class' => 'sub-header-menu',
'theme_location' => 'sub-header-menu')
);
?>
<?php } ?>
And I need something that will make it only show on the blog page and the children for that page (i.e the categories).. I'm not great with PHP but I guess this would be something simple

Just add a page Id of your blog page in you condition.
$parentPageId = is_subpage();
if (has_nav_menu('sub-header-menu', 'responsive') &&
(is_page( $blogPageId ) || $parentPageId == $blogPageId))
You can alos check your page using slug.
is_page( 'blog' )
Function to get Parent Page Id if exists.
function is_subpage() {
global $post;
if ( is_page() && $post->post_parent ) {
return $post->post_parent;
} else {
return false;
}
}

Find out the ID of the blog page $blogid = 123 (for example) and then check with if ($page->ID == $blogid) { /*show menu*/ }

Related

Check if its home page or not

I'm trying to get if its home page or not. (I mean all kind of home page in wordpress: Default, static, blog). I wrote this code in function.php file but in all pages I get "not home"!!
if ( is_front_page() && is_home() ) {
die("home");
} elseif ( is_front_page() ) {
die("home");
} elseif ( is_home() ) {
die("home");
} else {
die("not home");
}
Set home page screenshot https://prnt.sc/rbfgy8
Add this code in theme functions.php
//Put the condition in wp_footer hook to see the result
add_action('wp_footer', 'custom_wp_footer_fun');
function custom_wp_footer_fun(){
if ( is_front_page() && is_home() ) {
echo 'home';
} elseif ( is_front_page() ) {
echo 'home';
} elseif ( is_home() ) {
echo 'home';
} else {
echo 'not home';
}
}

How To Get Custom Wordpress Page Template Conditionally

I'm trying to alter the experience of the user by changing the page templates based on URL UTM variables.
Example, if the utm_source=google I want to show the person the page-google.php.
Here's the code I have so far.
<?php if ( isset( $_GET['utm_source'] ) && $_GET['utm_source'] == 'test' )
{
get_page_template(test);
}
else {
get_page_template(gallery);
}
?>
I'm not sure 1) if this is the right way 2) where it should be placed in wordpress.
Thanks for your help!
in page.php
<?php if ( isset( $_GET['utm_source'] ) && $_GET['utm_source'] == 'test' ) {
get_template_part('test');
}
else {
get_template_part('gallery');
}
?>
this is if test.php is in your sites theme directory

Different template for second page of posts Wordpress

I'm trying to have a different template for the second page of posts, here's the code I came up with.
<?php
if ( is_home() ) { ?>
SOME HTML
<?php } else if (is_page('chi-sono')){ ?>
SOME HTML
<?php } else if (is_paged()){} else{}?>
It seems straightforward to me but the elseif is_paged() doesn't work, what am I doing wrong? I also tried
$paged = (get_query_var('paged'));
elseif ($paged >= 2)
is_home() return true on blog page so you are not going on is_paged().
try this
if (is_home()) {
if (is_paged()) {
echo 'second page';
} else {
echo 'home page';
}
}

Variable Content w/ Advanced Custom Fields

I am trying to use the Advanced Custom Fields "True/False" plugin for Wordpress to display varied content depending on the user's referral ID.
1) If there is a Ref. ID & "Create" is True, display "Paid Nav"
2) If there is a Ref. ID & "Create" is False, display "Main Nav"
3) If anything else, show nothing.
Everything is working properly, except Item #1. When the True/False is enabled, BOTH Navigation Menus appear.
<?php while(the_repeater_field('referrers', 'options')): ?>
<?php if(isset($_COOKIE['referrer']) && get_sub_field('create') == true) {
$referrer = json_decode(stripslashes($_COOKIE['referrer']));
echo wp_nav_menu( array('container' => false, 'menu' => 'Paid Nav' ) );
} elseif(isset($_COOKIE['referrer']) && get_sub_field('create') == false) {
$referrer = json_decode(stripslashes($_COOKIE['referrer']));
echo wp_nav_menu( array('container' => false, 'menu' => 'Main Nav' ) );
} else {
echo '';
}?>
<?php endwhile; ?>
The problem is that you have the show menu inside the loop. What may be happening is that in one moment it shows the first one, and in another, the second one. Here is a different way to code it:
<?php
if(isset($_COOKIE['referrer'])):
$menu_to_display = 'Main Nav';
$referrer = json_decode(stripslashes($_COOKIE['referrer']));
while(the_repeater_field('referrers', 'options')):
if(get_sub_field('create'))
$menu_to_display = 'Paid Nav';
endwhile;
echo wp_nav_menu( array('container' => false, 'menu' => $menu_to_display ) );
endif;
?>
In this way, the menu is shown only once, and for only one menu. Also, it uses fewer tests.
Home it helps.
Like this?
<?php
$menu_to_display = 'Main Nav';
if(isset($_COOKIE['referrer'])):
$referrer = json_decode(stripslashes($_COOKIE['referrer']));
while(the_repeater_field('referrers', 'options')):
if(get_sub_field('create'))
$menu_to_display = 'Paid Nav';
endwhile;
endif;
echo wp_nav_menu( array('container' => false, 'menu' => $menu_to_display ) );
?>

Getting the literal value of a variable without echo

I have a function below, list_child_sibling_pages, which returns a string of 'hidden' when the initial 2 if / ifelse statements are false.
I need to put the literal return value 'hidden' in to the array below as shown here
array( 'classname' => 'custom child-parent-menu-container '. $hidden, 'description' => 'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages' );
I know I can output it with with
echo list_child_sibling_pages();
But I don't know how I would work that in to the code below. The value is returned fine if I just echo it out on it's own my only issue is echoing it where I need it
Relevant code follows
function tmm_child_parent_menu() {
function list_child_sibling_pages()
{
global $post;// if outside the loop
$post_type = '&post_type='.get_post_type(); 
$children = get_pages('child_of='.$post->ID.$post_type);
$parent_title = get_the_title($post->post_parent);
$parent_permalink = get_permalink($post->post_parent);
 
//child pages
if ( $post->post_parent && !is_search() && !is_404() ) 
{ ?> 
<h4><?php echo ''.$parent_title.''; ?></h4>
<ul class="parent-page-children">
<?php wp_list_pages( 'title_li=&child_of='.$post->post_parent.'&sort_column=menu_order'.$post_type ); ?>
</ul>
<?php 
} 
//parent pages
elseif ( count( $children ) != 0 && !is_search() && !is_404() )
{ ?>
<h4><?php the_title(); ?></h4>
<ul class="parent-page-children">
<?php wp_list_pages( 'title_li=&child_of='.$post->ID.'&sort_column=menu_order'.$post_type ); ?>
</ul>
<?php 
} 
else 
{
return 'hidden';
}
}
$hidden = list_child_sibling_pages();
/* Widget settings. */
$widget_ops = array( 'classname' => 'custom child-parent-menu-container '. $hidden, 'description' => 'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages' );
confused but i think you want
$array=array('classname'=>'custom child-parent-menu-container '.list_child_sibling_pages(),
'description'=>'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages');

Categories