word press常用的标签调用时的php代码调试

 

简述:<?php//头部导航class Custom_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl( &$output, $depth = 0, $args = null ) { $indent = str_repeat("\t", $depth); $sub_depth = $depth + 2; // Increment the depth by 1 $output .= "\n$indent<div class=&#39;sub$sub_depth&#39;><ul>\n"; } function start_el( &$output, $item, $depth = 0, $args = null, $current_object_id = 0 ) { $_zdyiocn = get_field(&#39;_zdyiocn&#39;, $item); $output .= &#39;<li&#39;; // 检查当前项是否是当...

详情:

<?php 








//头部导航

class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {

    function start_lvl( &$output, $depth = 0, $args = null ) {

        $indent = str_repeat("\t", $depth);

        $sub_depth = $depth + 2; // Increment the depth by 1

        $output .= "\n$indent<div class='sub$sub_depth'><ul>\n";

    }


    function start_el( &$output, $item, $depth = 0, $args = null, $current_object_id = 0 ) {

        $_zdyiocn = get_field('_zdyiocn', $item);

        $output .= '<li';

        

             // 检查当前项是否是当前页面

        if ($item->current || $item->current_item_ancestor) {

            $output .= ' class="cur"';

        }



        if ($args->walker->has_children) {

            $output .= ' class="hasChild"';

        }


        $output .= '>';

        // $icon = get_field('icon', $item);

        // error_log('Icon URL: ' . print_r($icon, true));


        if ($args->walker->has_children && $depth == 0) {

            $output .= '<a href="javascript:;">' . esc_html($item->title) . '</a><em>+</em>';

        } else {

            $output .= '<a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a><div class="nav-icon"><a href="'. esc_url($item->

url) .'"><img src="'. esc_url($_zdyiocn) .  '" alt="' . esc_html($item->title) . '"></a></div>';

        }

    }



    function end_lvl( &$output, $depth = 0, $args = null ) {

        $indent = str_repeat("\t", $depth);

        $sub_depth = $depth + 2; // Increment the depth by 1

        $output .= "$indent</ul></div>\n";

    }


    function end_el( &$output, $item, $depth = 0, $args = null ) {

        $output .= "</li>\n";

    }

    


}




// 底部导航

class Custom_Walker_FNav_Menu extends Walker_Nav_Menu {

    function start_lvl( &$output, $depth = 0, $args = null ) {

        if ($depth == 0) {

            $output .= '<ul class="">';

        } elseif ($depth == 1) {

            $output .= '<ul class="' . ($depth + 2) . '">';

        }

    }


    function start_el( &$output, $item, $depth = 0, $args = null, $current_object_id = 0 ) {

        $_zdyiocn = get_field('_zdyiocn', $item);


        $output .= '<li class="item cat8999"';  


        if ($args->walker->has_children) {

            $output .= ' class="hasChild"';

        }



        $output .= '>';


        if ($args->walker->has_children && $depth == 0) {

            $output .= '<div class="t">' . esc_html($item->title) . '</div>';

        } else {

            $output .= '<a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a>';

        }

    }


    function end_lvl( &$output, $depth = 0, $args = null ) {

        $output .= '</ul>';

    }


    function end_el( &$output, $item, $depth = 0, $args = null ) {

        $output .= "</li>\n";

    }

}


// 产品侧边导航

class Custom_Walker_ProductSide_Menu extends Walker_Nav_Menu {

    

    function start_lvl( &$output, $depth = 0, $args = null ) {

        $indent = str_repeat("\t", $depth);

        // $sub_depth = $depth + 2; // Increment the depth by 1

       $output .= "\n$indent<ul class='sub' >\n";

        

    }


    function start_el( &$output, $item, $depth = 0, $args = null, $current_object_id = 0 ) {

        $output .= '<li class="item ';

        

             // 检查当前项是否是当前页面

        if ($item->current || $item->current_item_ancestor) {

            $output .= 'cur"';

        }else{

            $output .= '"';

        }



        $output .= '>';

        // $icon = get_field('icon', $item);

        // error_log('Icon URL: ' . print_r($icon, true));


        if ($args->walker->has_children && $depth == 0) {

            $output .= '<a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a><i class="iconfont sn-down"></i>';

        } else {

            $output .= '<a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a>';

        }

    }



    function end_lvl( &$output, $depth = 0, $args = null ) {

        $indent = str_repeat("\t", $depth);

        $sub_depth = $depth + 2; // Increment the depth by 1

        $output .= "$indent</ul>\n";

    }


    function end_el( &$output, $item, $depth = 0, $args = null ) {

        $output .= "</li>\n";

    }

    


}








// 主题导航

function theme_setup() {

    // Enable support for custom menus

    add_theme_support('menus');

}


add_action('after_setup_theme', 'theme_setup');






//   $litpic = get_field('litpic', $item);


// 在主题循环中获取litpic字段的值

$litpic = get_field('litpic');


// 输出litpic字段的值

if ($litpic) {

    echo '<img src="' . esc_url($litpic['url']) . '" alt="' . esc_attr(get_the_title()) . '">';

}


// 启用自定义页面

add_theme_support('solutions-detailed');

add_theme_support( 'zdyiocn' );






// 自定义产品分类列表页

function load_product_category_template($template) {

    $new_template = '';


    // Check if it's a product category archive page

    if (is_product_category()) {

        // Get the current product category object

        $product_category = get_queried_object();


        // Check if it belongs to a specific product category

        if ($product_category) {

            // Use the template file for the specific product category

            $new_template = locate_template(array('archive-product.php'));

        }

    }


    // Load the custom template if found

    if (!empty($new_template)) {

        include($new_template);

        exit;

    }


    // Return the new template if available, otherwise, return the original template

    return ($new_template != '') ? $new_template : $template;

}


add_action('template_include', 'load_product_category_template');



// 自定义文章分类列表页

function load_post_category_template($template) {

    $new_template = '';


    // Check if it's a post category archive page

    if (is_category()) {

        // Get the current post category object

        $post_category = get_queried_object();


        // Check if it belongs to a specific post category自定义路径

        if ($post_category && $post_category->slug == 'xxxxcinematography') {

            // Use the template file for the specific post category自定义页面模板

            $new_template = locate_template(array('xxxxxarchive-product.php'));

        }

    }


    // Load the custom template if found

    if (!empty($new_template)) {

        include($new_template);

        exit;

    }


    // Return the new template if available, otherwise, return the original template

    return ($new_template != '') ? $new_template : $template;

}


add_action('template_include', 'load_post_category_template');




// 自定义详情页

function load_single_template($template) {

    $new_template = '';


    // Check if it's a single post

    if (is_single()) {

        global $post;


        // Check if the post belongs to the 'news' category

        if (has_category('news', $post)) {

            // Use the template file single-news.php

            $new_template = locate_template(array('newsdetail.php'));

        }

    } elseif (is_shop()) {

         global $post;

        // Check if it's a shop page

        // Use the template file for shop page

        $new_template = locate_template(array('single-product.php'));

    }


    // Load the custom template if found

    if (!empty($new_template)) {

        include($new_template);

        exit;

    }


    // Return the new template if available, otherwise, return the original template

    return ($new_template != '') ? $new_template : $template;

}


add_action('template_include', 'load_single_template');







error_reporting(E_ALL);








?>



页面面包屑导航定位效果


<?php

echo '<ul class="inner">';

if (!is_front_page()) {

    echo '<li><a href="' . home_url() . '" class="home">Home</a></li>';

    if (is_single()) {

        $categories = get_the_category();

        if ($categories) {

            foreach ($categories as $category) {

                echo '<li>' . get_category_parents($category, true, '</li><li>') . '</li>';

            }

        }

        echo '<li>' . get_the_title() . '</li>';

    } elseif (is_page()) {

        if ($post->post_parent) {

            $parent_id  = $post->post_parent;

            $breadcrumbs = array();

            while ($parent_id) {

                $page = get_page($parent_id);

                $breadcrumbs[] = '<li><a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a></li>';

                $parent_id  = $page->post_parent;

            }

            $breadcrumbs = array_reverse($breadcrumbs);

            foreach ($breadcrumbs as $crumb) {

                echo $crumb;

            }

        }

        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';

    } elseif (is_category()) {

        $category = get_category(get_query_var('cat'));

        $ancestors = get_ancestors($category->cat_ID, 'category');

        foreach (array_reverse($ancestors) as $ancestor) {

            echo '<li><a href="' . get_category_link($ancestor) . '">' . get_cat_name($ancestor) . '</a></li>';

        }

        echo '<li>' . single_cat_title('', false) . '</li>';

    } elseif (is_tag()) {

        echo '<li>Tag: ' . single_tag_title('', false) . '</li>';

    } elseif (is_day()) {

        echo '<li>Archive: ' . get_the_time('F j, Y') . '</li>';

    } elseif (is_month()) {

        echo '<li>Archive: ' . get_the_time('F Y') . '</li>';

    } elseif (is_year()) {

        echo '<li>Archive: ' . get_the_time('Y') . '</li>';

    } elseif (is_author()) {

        echo '<li>Author: ' . get_the_author() . '</li>';

    } elseif (is_search()) {

        echo '<li>Search results for: ' . get_search_query() . '</li>';

    } elseif (is_404()) {

        echo '<li>Error 404</li>';

    }

}

echo '</ul>';

?>



从二级或三级进入,显示当前二级

<?php

$current_top_level_page_id = 0; // Initialize with 0


// Get the ID of the current top-level page

if (is_page()) {

    // If the current page is not a top-level page, find its top-level ancestor

    $current_top_level_page_id = $post->post_parent ? $post->ancestors[count($post->ancestors) - 1] : $post->ID;

} else{

    $current_top_level_page_id = $post->post_parent ? $post->ancestors[count($post->ancestors) ] : $post->ID;

}


// Check if the current page is a third-level page

$is_third_level = false; // Assume it's not a third-level page by default

if ($post->post_parent) {

    $parent = get_post($post->post_parent);

    if ($parent->post_parent) {

        $is_third_level = true; // If it has a parent and that parent has a parent, it's a third-level page

    }

}


// Get child pages of the current top-level page

$child_pages = get_pages(array(

    'child_of' => $current_top_level_page_id,

    'parent' => $current_top_level_page_id, // Only get direct children

    'sort_column' => 'menu_order',

    'hierarchical' => 0,

));


if ($child_pages) {

    echo '<ul class="clearfix">';

    foreach ($child_pages as $child_page) {

        // Check if the current page is a third-level page and the child page is a direct child of the current top-level page

        if ( $child_page->ID == $post->post_parent) {

            $class = 'cur';

        } else {

            // For other cases, check if the current page ID matches the child page ID

            $class = ($child_page->ID == get_the_ID()) ? 'cur' : '';

        }

        echo '<li class="' . $class . '"><a href="' . esc_url(get_permalink($child_page->ID)) . '">' . esc_html($child_page->post_title) . '</a></li>';

    }

    echo '</ul>';

}


?>