admin管理员组

文章数量:1123939

I have pages with a custom meta field called "ctr_state", and it contains a state name, like "Arizona".

I am trying to construct a hyperlink that will return an archive-like list of pages that match the meta_value I am looking for.

For instance,

<a href="/?type=page&amp;meta_key=ctr_state&amp;meta_value=Arizona">Show All Arizona Pages</a>

This isn't working, but is just to show how I want it to work as a link. I don't want a list on the page, I just want it to link to an archive type page, showing just the page's that match the state I'm looking for.

Is this possible?

I have pages with a custom meta field called "ctr_state", and it contains a state name, like "Arizona".

I am trying to construct a hyperlink that will return an archive-like list of pages that match the meta_value I am looking for.

For instance,

<a href="http://mydomain.com/?type=page&amp;meta_key=ctr_state&amp;meta_value=Arizona">Show All Arizona Pages</a>

This isn't working, but is just to show how I want it to work as a link. I don't want a list on the page, I just want it to link to an archive type page, showing just the page's that match the state I'm looking for.

Is this possible?

Share Improve this question asked Nov 24, 2013 at 21:30 stabilimentastabilimenta 214 bronze badges 7
  • To do this with a custom field you could write a Page Template. If you are not already too invested in the custom field switching to a Custom Taxonmy would allow you to go to a url that matches any state name with WordPress doing much of the work for you. No template needed (unless you want to customize it). – Charles Clarkson Commented Nov 24, 2013 at 23:16
  • If I used a template, would I need to create an empty page to assign the template to, in order to link to it, or is there a way to link directly to a template and not having it assigned to any particular page? – stabilimenta Commented Nov 25, 2013 at 22:39
  • The former is the normal case. Before that page template PHP file is executed, WordPress loads, any plugins, the theme and perhaps a few other things happen; Then that code is executed. If you went directly to the file you would have to manually start all that stuff first and then add your template code. I would suggest trying it the way WordPress programmers expect you to do so (using a page template or a custom taxonomy) and get that working and try something more exotic later. – Charles Clarkson Commented Nov 26, 2013 at 0:36
  • Thanks Charles. This helped me, and have it working now. For many reasons I couldn't use taxonomy nor have placeholder pages in this project. I was able to add a function for a template redirect: – stabilimenta Commented Nov 26, 2013 at 16:59
  • This is how I got it working: ADDED this to functions.php: function include_template_function( $template_path ) { global $wp; if ($wp->request == 'state') { $template_path = locate_template( array ( 'state.php' ) ); } return $template_path; } ADDED THIS to the base template (page.php): <?php $baseURL = esc_url( home_url( '/' ) ); $ak_link = $baseURL . 'state?st=AK'; ?> <a href="<?php echo $ak_link; ?>">Show Alaska Pages</a> CREATED state.php template that handles state queries: <?php $st = $_GET['st']; get_header(); ?> ?> and can test for $st and do stuff accordingly. – stabilimenta Commented Nov 26, 2013 at 17:19
 |  Show 2 more comments

1 Answer 1

Reset to default 1

This is how I got it working:

ADDED this to functions.php:

function include_template_function( $template_path ) {
    global $wp;
    if ($wp->request == 'state') {
        $template_path = locate_template( array ( 'state.php' ) );
    }
    return $template_path;
}

$state_name = $_GET['st']; //this is added to use as a global variable

ADDED THIS to header.php because WP thinks it's serving a 404 page

    if ($wp->request == 'state') { 
        echo 'Member Centers in ' . $state_name . ' | ';
    } else {
        wp_title( '|', true, 'right' );
    } 
?>
</title>

ADDED THIS to the base template (page.php):

<?php 
    $baseURL = esc_url( home_url( '/' ) );
    $ak_link = $baseURL . 'state?st=Alaska'; // for each state
?>
<a href="<?php echo $ak_link; ?>">Show Alaska Pages</a> 

CREATED state.php template that handles state queries:

<?php global $state_name; ?> ?>

<?php 
    $mypages = get_pages('child_of='.$parent_page_id.'&sort_order=ASC&parent='.$parent_page_id.'&meta_key=ctr_state&meta_value='.$state_name);
    foreach($mypages as $page)
    {
?>
... Do Stuff! ...
<?php } ?>

本文标签: archiveshow to make URL link query string