You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress自定义主题中跳转链接URL正确但页面内容显示index.php而非目标页面的问题求助

Hey Jakub, let's work through why your single_assignment_page.php is loading index.php content—this is a common quirk with WordPress's routing system, and we can fix it step by step:

1. Stop linking directly to theme PHP files

WordPress uses a front-controller pattern (almost all requests go through index.php by default) unless the requested file exists and isn't blocked by .htaccess. When you hardcode links to single_assignment_page.php, either:

  • Your .htaccess rules are still routing the request to index.php even if the file exists, or
  • The relative path is broken, triggering a 404 that WordPress falls back to index.php for.

Instead, use WordPress's native functions to generate valid URLs. First, we'll set up a proper page template (next step), then update your link code in front-page.php:

// Get the URL of your dedicated assignment page (we'll create this next)
$assignment_page = get_page_by_path('single-assignment'); // Replace 'single-assignment' with your page's slug
$assignment_page_url = get_permalink($assignment_page);
// Add the assignment ID as a query parameter
$link_url = add_query_arg('id', $row->id, $assignment_page_url);
// Output the link (don't forget the closing </a> tag!)
echo '<td><a href="' . esc_url($link_url) . '">View Assignment</a></td>';

2. Turn single_assignment_page.php into a Custom Page Template

WordPress won't recognize your file as a theme template unless you add a template comment at the very top. Update your single_assignment_page.php like this:

<?php
/*
Template Name: Single Assignment Page
*/
?>
<?php get_header(); ?>
<?php 
// Sanitize the ID to avoid security risks like SQL injection
$assignment_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
// Fetch the specific assignment data
$assignment = $wpdb->get_row($wpdb->prepare(
    "SELECT id, voornaam, achternaam, bedrijfsnaam, telefoonnummer, emailadres, titel_opdracht, opdrachtbeschrijving 
     FROM form_entry WHERE id = %d", 
    $assignment_id
));
?>
<?php if ($assignment) : ?>
    <h2><?php echo esc_html($assignment->titel_opdracht); ?></h2>
    <p><strong>Voornaam:</strong> <?php echo esc_html($assignment->voornaam); ?></p>
    <p><strong>Achternaam:</strong> <?php echo esc_html($assignment->achternaam); ?></p>
    <!-- Add all your assignment fields here -->
<?php else : ?>
    <p>Assignment not found.</p>
<?php endif; ?>
<?php get_footer(); ?>

Then:

  • Go to WordPress Dashboard > Pages > Add New
  • Title it "Single Assignment" (or your preferred name)
  • In the right sidebar under Page Attributes, select "Single Assignment Page" from the Template dropdown
  • Publish the page, and note its slug (e.g., single-assignment) to use in the link code above.

3. Fix your .htaccess and flush rewrite rules

Make sure your .htaccess has the default WordPress rewrite rules—this ensures existing files are served correctly:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

After updating, go to Settings > Permalinks and click "Save Changes" to flush WordPress's rewrite rules.

4. Bonus: Use Custom Post Types (better long-term solution)

If "Assignments" are a core part of your site, creating a Custom Post Type (CPT) is more aligned with WordPress best practices. This eliminates the need for query args and lets WordPress handle permalinks automatically. Add this to your theme's functions.php:

function create_assignment_cpt() {
    register_post_type('assignment', [
        'labels' => [
            'name' => __('Assignments'),
            'singular_name' => __('Assignment')
        ],
        'public' => true,
        'has_archive' => true,
        'rewrite' => ['slug' => 'assignments'],
        'supports' => ['title', 'editor', 'custom-fields'] // Add fields you need
    ]);
}
add_action('init', 'create_assignment_cpt');

Flush permalinks after adding this, and you'll be able to create Assignment posts in the dashboard. WordPress will automatically load a single-assignment.php template for individual assignments.

Quick side note: Your original front-page.php link was missing the closing </a> tag—make sure to fix that too!

内容的提问来源于stack exchange,提问作者Jakub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 12:27:47