Genesis Conditional Hooks Not working What am I doing wrong? - php

So I am trying to call the genesis_do_nav hook so its only on the front page and the homepage. For some reason it doesn't work unless I do
add_action( 'genesis_entry_header', 'genesis_do_nav' );
But that is going to execute the menu on the entry header globally which is exactly what I am trying to avoid. Any suggestions
remove_action( 'genesis_after_header', 'genesis_do_nav' );
add_action( 'genesis_entry_header', 'menu_only_on_homepage' );
function menu_only_on_homepage() {
if( is_home() && is_front_page() ){
genesis_do_nav();
}
}

Two Ideas I would like you to try:
A. First Issue I see is with your condition i.e. is_home() && is_front_page() I think this condition will never be true as both are different pages. So maybe you wanted to have it is_home() || is_front_page()
B. Try Re Arranging the code, Try arranging your conditional in different order so that you have condition before you add the hook i.e.
if(is_home() || is_front_page() ){
add_action( 'genesis_entry_header', 'genesis_do_nav' );
}
See if it helps in your situation please note code is untested as I wrote here so first test it on dev environment before trying on live so you could easily fix any typos or syntax errors.

Related

Wordpress Posts Are Showing In Reverse Order (oldest first)

I am helping a friend with their Wordpress site and all of their blog posts are showing in reverse order (oldest first). I haven't found any plugins responsible and haven't identified any code that was added. I also tried adding the code
//function to modify default WordPress query
function wpb_custom_query( $query ) {
// Make sure we only modify the main query on the homepage
if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
// Set parameters to modify the query
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
$query->set( 'suppress_filters', 'true' );
}
}
// Hook our custom query function to the pre_get_posts
add_action( 'pre_get_posts', 'wpb_custom_query' );```
They are using the theme RT-Theme 18
Does anyone have any idea why this might be happening?
As mentioned by #fraggley, start off by disabling all plugins and setting the theme to TwentyTwenty (the default theme). The RT-Theme 18 also exhibits similar behaviour in their demo with no obvious setting to change this behaviour. If you aren't already, ensure you're making customisations in a child-theme. Without creating a new function, try using a standard loop to start off to see whether the order is still incorrect.
<?php
get_header();
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        the_content();
    endwhile;
else :
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
get_footer();
?>
This would typically be placed in your home.php or index.php at the root of your child theme. WordPress provides good documentation on this in their codex. You can determine this by checking which file the original code occurs in the parent theme. So if the loop occurs in the parent theme home.php, create a file called home.php in the same relative location on your child theme. More documentation on this here.

Load custom page templates in WordPress admin dashboard

I have several custom page templates in my theme. But I want to hide a few with a plugin, and only show the custom home page template on the page that has been set as the front page "is_front_page" or "is_home". This is what I am using in my plugin to stop some of the page templates from showing up.
This is for a large multisite where there are two tiers of sites, one gets the full set of features, and the other a stunted set. I have everything working as I need except for this.
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
unset( $pages_templates['page-home.php'] );
return $pages_templates;
}
The code above works totally fine, but I need a conditional in there to show the page-home.php when the page has been set to the home page. I have tried this code, but it doesn't work.
if ( is_front_page() ) :
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
return $pages_templates;
}
else :
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
unset( $pages_templates['page-home.php'] );
return $pages_templates;
}
endif;
Any ideas on how I can get this to work?
A few things:
One, it appears you've only added the add_filter call to the is_front_page()'s true condition, that's probably part of it.
Second, you should not be defining and/or redefining functions inside if statements!
Third, for WordPress specifically, with it's Action Hook and Filter system, it's generally considered a best practice to add the filters and actions at all times, and run if/else or abort type statements inside the function to allow for easier extensibility.
Edit:
Based on some clarification, I understand a bit better. You need to get the ID of the page that's set to the front page, which is stored as an option named page_on_front in the options table. The is_front_page() function will only work in the scope of the current $wp_query loop.
I think this would solve it for you. Run the je_remove_page_template function on the theme_page_templates filter, and check the current page's ID against the one stored in the page_on_front option:
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ){
unset( $pages_templates['page-topimage.php'] );
$home_page_id = absint( get_option( 'page_on_front' ) );
$this_page_id = get_the_ID();
// If this isn't the home page, remove `page-home`
if( $this_page_id != $home_page_id ){
unset( $pages_templates['page-home.php'] );
}
return $pages_templates;
}
Also of note would be that is_front_page() is different than is_home() - make sure you're using the correct one! (I believe you are, as generally IFP is what people want)

Custom Layout for Single Product page - Woocommerce

I am trying to create a custom layout for one of our product pages in Woocommerce. I have searched and followed so many tutorials, but none are working for me. I copied 'single-product.php' and placed it into my active child theme in the folder 'woocommerce'. I then duplicated this and renamed it 'single-product-landing.php'.
I then placed the following code into my functions.php page:
add_filter( 'template_include', 'custom_single_product_template_include', 10 );
function custom_single_product_template_include( $template ) {
if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-landing.php';
}
return $template;
}
Then I tried changing items in single-product-landing.php but nothing is changing, so it can't be picking it up. To confirm, I have the product assigned to the 'custom' category.
I should also add that my product is a 'variable' product, am not sure if that has any affect on anything here.
EDIT: I have found the following code in my 'single.php' file - I think this may be interfering. Problem is, if I delete this nothing shows on any of my pages (even those not affected by the new template):
<?php
if(get_theme_mod('product_layout') == 'custom') {
wc_get_template_part( 'content', 'single-product-custom' );
} else {
wc_get_template_part( 'content', 'single-product' );
}
?>
Any idea how I can modify this to still have content show but not over-rule the template change that i'm trying to make?
Page templates are loaded after the template_filter has been run, so adding that code to a page template file will have no effect. To make that run, place the same code in your child theme's functions.php.
The 566 at the end of the add_filter line refers to the filter's loading priority and not a category id. Simply put, the higher the number the later the filter will be loaded.
Finally, your if statement can be simplified a little using Woocommerce functions - if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) { - doesn't make much difference, but it's tidier.

wordpress how to prevent load specific js in specific page

I want to prevent specific js in specific page for example suppose i have page name is detail_event and i would like to prevent load script mouse.min in this page .
I wrote following code in curent theme's functions.php but still mouse.min js is loading how to stop it.
add_action( 'wp_enqueue_scripts', 'my_register_javascript', 100 );
function my_register_javascript() {
if ( is_page('my-page') ) {
wp_deregister_script( 'mouse.min' );
}
}
And location of mouse.min js is mysite.com/wp-includes/js/jquery/ui/mouse.min.js?ver=1.11.4
please anyone know how to do this.
There are couple of things to be noticed here:
The handle for mouse.min.js is jquery-ui-mouse and not mouse.min
You are deregistering the script which means that as soon as that page is visited, WP will stop enqueuing this script in all pages.
Instead of doing that you should try below mentioned code:
add_action( 'wp_footer', 'custom_wp_dequeue_script', 11 );
function custom_wp_dequeue_script() {
if ( is_page('my-page') ) {
wp_dequeue_script( 'jquery-ui-mouse' );
}
}
You just need to make the conditional include negative:
if ( !is_page('my-page') ) {

The difference between wrapping a require_once inside a conditional statement and not doing so in functions.php

Without the conditional statement:
require_once( dirname( __FILE__ ) . "/edit_info_check_password.php" );
works fine.
But by simple adding:
if( substr($_SERVER["REQUEST_URI"], 18, 13) == "edit-userinfo" )
require_once( dirname( __FILE__ ) . "/edit_info_check_password.php" );
inside the functions.php file, then the functions inside the required file do not work completely. But they work partially. Why can't I keep the condition if i only want to include the file for a certain page?
EDIT - additional requested info:
basically trying to add the following actions inside the required file:
add_action( 'wp_enqueue_scripts', 'admin_ajax_setup' );
add_action( 'wp_ajax_ajax-checkpasswords', 'check_info_passwords' );
add_action("wp_ajax_nopriv_ajax-checkpasswords", "check_info_passwords");
The admin ajax setup function runs perfectly, it enqueues two javascript files and localized the admin ajax for javasscript access. That's great, but my jquery ajax function returns response 0. Whereas without the conditional statement, I get the full response. Obviously adding the condition changes the way functions.php works with required files.
The reason this does not work is because your ajax admin hooks are not being called.
The reason the ajax hooks are not called is because they are not called at the "edit-userinfo" page. Those ajax calls are handled at a different page. Your require will only load on the "edit-userinfo" page.
The best solution I have is to wrap the ajax hooks in the init hook and the add an is_admin conditional check.
This means that you will need to include your file without the conditional. I don't think performance should be an issue if that file is included every time.
You setup your file like this:
if( substr($_SERVER["REQUEST_URI"], 18, 13) == "edit-userinfo" )
add_action( 'wp_enqueue_scripts', 'admin_ajax_setup' );
function init_admin_ajax_hooks()
{
if (is_admin()) {
add_action( 'wp_ajax_ajax-checkpasswords', 'check_info_passwords' );
add_action("wp_ajax_nopriv_ajax-checkpasswords", "check_info_passwords");
}
}
add_action('init', 'init_admin_ajax_hooks');
I left your enqueue script hook as is because I'm not sure if it's in the admin or not.
If it is in the admin you should use admin_enqueue_script hook instead:
if( substr($_SERVER["REQUEST_URI"], 18, 13) == "edit-userinfo" )
add_action( 'admin_enqueue_scripts', 'admin_ajax_setup' );

Categories