0

I'm trying to make it so that when someone goes into WordPress and goes to update a section of a page, WordPress only loads the customize options for that page that they're specifically editing $wp_customize->add_section. Right now it's showing all of the sections for all of the pages on the website and is not manageable.

EDIT: Trying another approach that I think is cleaner but still having the same issue...edited the code to reflect what I'm currently working with:

beginning of functions.php file:

<?php
function add_css()
{
   wp_register_style('style', get_template_directory_uri() . '/assets/css/style.css', false,'1.1','all');
   wp_enqueue_style( 'style');
}
add_action('wp_enqueue_scripts', 'add_css');

function add_script()
{
wp_register_script('js-script', get_template_directory_uri() . '/assets/js/scripts.js', array ( 'jquery' ), 1.1, true);
wp_enqueue_script( 'js-script');
}
add_action('wp_enqueue_scripts', 'add_script');
add_theme_support( 'menus' );

global $template;
global $currentPage;


function boat_homehero_callout($wp_customize) {
   if (is_page('home')) {
      $currentPage = 'Home';
      echo $currentPage;
   $wp_customize->add_section('boat-homehero-callout-section', array ('title' => 'Homepage Hero Section'));
?>

end of my functions.php file:

<?php
wp_reset_query();
if (is_page('find-your-trip')) {
    boat_findyourtrippage_callout();
} elseif (is_page('home')) {
    boat_homehero_callout();   
} elseif (is_page('who-we-are')) {
    boat_whoweare_callout();
} elseif (is_page('what-we-do')) {
    boat_partnership_callout();
} elseif (is_page('support-our-mission')) {
    boat_supportourmission_callout();
} // else (is_page('annual-report')) {
 elseif  (is_page('annual-report')){
    boat_annualreport_callout();
} else {
    boat_homehero_callout();
}
?>
 

Thank you for any guidance!

I've tried using is_page() https://developer.wordpress.org/reference/functions/is_page/ . I've spent days trying to troubleshoot and reading other Stackoverflow answers and I'm stuck. This is my first custom WordPress template build and I've read some answers saying to use wp_reset_query() and other answers saying to never use wp_reset_query(). I tried using it and I'm not sure if I placed it in the wrong area, but it didn't work for me.

Or is it something else entirely I'm missing? I'd really appreciate any help at all. I checked the WordPress pages permalink section and the permalink matches what I'm calling with the is_page() function.

I'm also using localwp for editing the template locally. https://localwp.com/

2nd Edit: Found out how to view the console on WordPress and am getting the error below when I log $currentPage; taking a look at things...

PHP Warning: Undefined variable $currentPage in C:\Users\Melis\Local Sites\boat\app\public\wp-content\plugins\wp-console\includes\Core\Console\RestController.php(139) : eval()'d code on line 1
 
#1 C:\Users\Name\Local Sites\boat\app\public\wp-content\plugins\wp-console\includes\Core\Console\RestController.php(139): eval()
Ram Chander
  • 1,088
  • 2
  • 18
  • 36
Dreamyth
  • 1
  • 4
  • You need to specifically explain what is not working, with errors, debugging diagnostics etc. wp_reset_query is specific to the loop. If you read the is_page() notes carefully, it explains that is_page will not work inside "the loop". wp_reset_query is only something used to fix up some global state once a query loop has completed. It is not a magical fix for is_page() not working inside the loop. – gview Aug 15 '23 at 21:09
  • @gview hey! I figured out how to view the console log in wordpress and I am getting the error below when I log $currentPage; PHP Warning: Undefined variable $currentPage in C:\Users\Melis\Local Sites\boat\app\public\wp-content\plugins\wp-console\includes\Core\Console\RestController.php(139) : eval()'d code on line 1 #1 C:\Users\Name\Local Sites\boat\app\public\wp-content\plugins\wp-console\includes\Core\Console\RestController.php(139): eval() I have the variable listed as a global outside of the functions and also inside of the functions, taking a look at options.... – Dreamyth Aug 15 '23 at 22:20
  • have been taking a look at https://stackoverflow.com/questions/55492966/undefined-variable-error-although-the-variable-is-present-in-an-already-included – Dreamyth Aug 15 '23 at 22:28
  • and now reviewing this - sorry I've had quite a bit of coffee and can get chatty! https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12778634#12778634 – Dreamyth Aug 15 '23 at 22:33
  • This gets into php scoping rules. For a page, variables declared in that page are global. However, unlike some other languages (with the exception of "super globals") global variables within a page are not visible inside a function. You can make those visible by declaring them INSIDE the function using the global keyword. You should not be declaring variables as global outside the functions. – gview Aug 15 '23 at 23:05
  • @gview thank you very much!! I just recently got into php from javaScript and this was incredibly helpful. This is my second language and I'm amazed and confused at how similar and different some things are between languages. I'll adjust the code and read up more on php scoping rules! That is good info to know about not declaring variables as global outside functions - thank you again! – Dreamyth Aug 15 '23 at 23:14
  • @gview PHP scoping rules was the answer! Would you like to post your comment as an answer and I'll accept? – Dreamyth Aug 16 '23 at 12:36
  • No worries, glad you got it fixed up. – gview Aug 17 '23 at 20:00

0 Answers0