$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Set global $wp_query$post variable for dynamic page generation|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Set global $wp_query$post variable for dynamic page generation

matteradmin11PV0评论

This is a difficult one to describe, but I'll do my best...

I'm trying to set up pages that can be shown based on a second (meta/ACF) slug, dependent on the language the user is using. Essentially, I need to hook into the loading of the 404 page (as the slug won't correlate to an actual page name) and then spoof the page by the meta value.

I've got it mostly sorted, but just struggling with the final hurdle of setting the page to act like a normal page based on the ID as found when querying the meta field.

At the moment, I'm hooking into the wp action, but this could possibly come sooner, I just wanted to hook into the is_404() function, so it doesn't run on pages if the language is the default.

This is where I'm currently at... it seems to be loading the post and showing the correct title etc, but is showing single.php rather than page.php (for a regular page):

if(is_admin() || !is_404()) return;

if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
    $protocol = 'https://';
} else {
    $protocol = 'http://';
}

$url = $protocol . parse_url($_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"], PHP_URL_PATH);
$slug = basename($url);

$pt = "page";
$p = jb_slug_to_post($slug,$pt);
// can find the post ID by custom slug

if(empty($p)){
    // find the post by the actual slug rather than meta/ACF
    global $wpdb;

    $sql = "SELECT p.ID FROM $wpdb->posts p
            WHERE p.post_name = '{$slug}' AND p.post_type = '{$pt}'
            ORDER BY p.post_parent ASC";

    $results = $wpdb->get_col($sql);

    $match = 0;

    if(!empty($results)){
        foreach($results as $r){
            $construct = jb_construct_permalink($r);
            // URL comparison   
            if($url==$construct){
                $match = $r;
                break;
            }   
        }
    }

} else {
    $match = $p;    
}

if($match){
    global $wp_query;       
    $wp_query = new WP_Query(array('p'=>$match));
    $wp_query->is_{$pt} = 1;

    global $post;
    $post = get_post($match);
    setup_postdata($post);
}
Post a comment

comment list (0)

  1. No comments so far