Variable Content w/ Advanced Custom Fields - php

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 ) );
?>

Related

WordPress wp_list_pages - Change children of parent from URLS into #anchor links

I'm setting up a side navigation menu using wp_list_pages and I would like to convert the children links from URLs into anchor links i.e #link rather than the current https://example.com/link being generated by wp_list_pages .
I was attempting to use the following code:
<?php
$my_pages = wp_list_pages('echo=0&title_li=&child_of=5&depth=1');
$pieces = explode('"', $my_pages);
$i = 5;
$j = 3;
$limit = count($pieces);
for (;$i<$limit;) {
$tmp1 = '#'.$pieces[$j];
$pieces[$i] = $tmp1;
$i = $i+6;
$j = $j+6;
}
$tmp2 = implode('"',$pieces);
echo $tmp2;
?>
But it seems to be very old and I can't wrap my head around how to properly implement it into my current structure. Maybe this code is useless for what I'm trying to do but I couldn't find anything that would work.
This is what I have currently:
<div class="hero-container">
<?php
global $children;
global $post;
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
) );
} else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
) );
}
if ( $children ) : ?>
<?php echo '<div class="hero-side-menu">', '<h1>', get_the_title(), '</h1>', '<ul>', $children, '</ul>', '</div>' ?>
<?php endif; ?>
</div>
Any suggestions would be very much appreciated as I've been trying to figure this out for a few days and have gotten nowhere... also would appreciate an explanation as I'm trying to learn where I went wrong!
I need to modify the children of the parent pages to have #anchor links rather than URLS as I've condensed the pages into their parents but still wish to have them as options within the side menu.
Clarification: I have a page with children pages that I referred to as parents however, they are indeed children. I would like to keep the URLS for the children pages and then make the children of the children #links.
This depends a bit on what you want the anchor text to be. Whether it's an ID or a title, etc. But you can modify that to your need.
You may want to try get_pages() instead, so you have a little more control over the output. Something like the following:
<div class="hero-container">
<?php
global $post;
$current_page_title = $post->post_title; // current page title
$child_pages =
get_pages(
array(
'child_of' => $post->ID
),
);
if ($child_pages) : ?>
<div class="hero-side-menu">
<h1><?php echo $current_page_title; ?></h1>
<ul>
<?php foreach ($child_pages as $page) : ?>
<li><?php echo $page->post_title; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>

Listing WordPress Pages - Replace heading with custom field text

I am working with a hierarchy of pages and want to list the parent & children as a sidebar index (with current page bolded). It's sort of working, but the PHP script I've come up with is using the main page heading to create the link text. These headings are very long for SEO purposes, so I'd like to replace them with the ACF 'subtitle' field that I'm already using. Here's the project before I begin to explain:
https://newstart.staging.wpengine.com/treatment/mental-health/depression
In This Section shows the script-generated list first with the obnoxiously long titles. The shorter titles (beginning with "Mental Health") were hard coded as placeholders, and that is what I would like the script to end up looking like. For example:
Depression is a Huge Risk Factor for Substance Abuse
(should be...)
Depression
How can I replace these titles with the subtitles? The get_field value in ACF is subtitle
Here is what the PHP looks like now:
<?php
//---- GET THE PAGE'S ANCESTORS
global $post;
$anc = get_post_ancestors( $post->ID );
//---- IF $ANC HAS MORE THAN 1 VALUE
//---- THEN WE HAVE A 3RD LEVEL CHILD PAGE
//---- AND THUS WANT THE PARENT ID
$top_id = $post->ID;
if (count( $anc ) > 1 ){
$top_id = $anc[0];
}
//---- TAKEN FROM WP CODEX
$ancestor_id = $top_id;
//VERY IMPORTANT, THE ORDER OF THIS ARRAY
//ENSURE THE SORTING MATCHES IN WP_LIST_PAGES BELOW
$descendants = get_pages(array(
'child_of' => $ancestor_id,
'sort_order' =>'ASC',
'sort_column' => 'menu_order',
));
$incl = "";
foreach ($descendants as $page) {
if (($page->post_parent == $ancestor_id) ||
($page->post_parent == $post->post_parent) ||
($page->post_parent == $post->ID))
{
$incl .= $page->ID . ",";
}
}
//---- MAKE A FINAL ARRAY FOR USE
//---- AS THE PREV//NEXT NAVIGATION
$pages_array = explode( ',', $incl );
array_pop($pages_array); //pop off last empty value
array_unshift( $pages_array, $ancestor_id); //add the top level page
// echo '<pre>';
// print_r($pages_array);
// echo '</pre>';
?>
<?php
//FIND THE NEXT ITEM
//IN THE PAGES ARRAY
//AFTER THE CURRENT PAGE
$curr_key = array_search($post->ID, $pages_array);
$curr_key++;
?>
<?php if( $curr_key < count($pages_array) ){ ?>
<?php
$id = $pages_array[$curr_key];
$title = get_the_title( $id );
$post = get_post( $id );
?>
<div class="col-md-2 hidden-sm hidden-xs section-index">
<h6>In This Section</h6>
<ul>
<?php
wp_list_pages(array(
"include" => $ancestor_id,
"link_before" => "",
"title_li" => "",
));
wp_list_pages(array(
"child_of" => $ancestor_id,
"include" => $incl,
"link_before" => "",
"title_li" => "",
'sort_order' =>'ASC',
'sort_column' => 'menu_order',
));
?>
</ul>
I come from a design background so I am not good with PHP syntax yet. Most of this is inherited code that I only vaguely understand. Any help would be appreciated!

Showing secondary nav on one page wordpress

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*/ }

Wordpress - Add "active" class if current page is active

<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
$parent_title = get_the_title($post->post_parent);?>
<li><?php echo $parent_title;?></li>
<?php echo $children; ?>
<?php } ?>
The code above lists the parent and all child pages in a list.
Parent Page
Child Page
Child Page class="active"
Child Page
Child Page
I would like to add a class of "active" to the currently active page. Any help is greatly appreciated. Thanks
To look for a specific page and add an active class to it, you can try using is_page and define the URL/slug of the page.
<a class="<?php if (is_page('name-of-page')) echo 'active'; ?>" href="#">Link</a>
You can easily add active and other classes by checking the $post->post_title against the $item->title
function addLinkClassesWithActive( $atts, $item, $args ) {
global $post;
// check if the item is in the primary menu
if( $args->theme_location == 'main-nav' ) {
// add the desired attributes:
$atts['class'] = $post->post_title === $item->title ? 'mdl-layout__tab is-active' : 'mdl-layout__tab';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'addLinkClassesWithActive', 10, 3 );
I am using this myself and stripping out the wrapping container, the ul, and the li tags so that I have just a link. See example below.
<nav role="navigation" class="mdl-navigation mdl-layout--large-screen-only" itemscope itemtype="http://schema.org/SiteNavigationElement">
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<?php
$primaryMenu = array(
'container' => false,
'theme_location' => 'main-nav',
'items_wrap' => '%3$s',
'echo' => false,
);
echo strip_tags( wp_nav_menu( $primaryMenu ), '<a>' );
?>
</div>
</nav>

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