【问题标题】:Linking to a wordpress page where articles are filtered by custom field data链接到按自定义字段数据过滤文章的 wordpress 页面
【发布时间】:2014-06-03 23:45:24
【问题描述】:

我必须链接到 wordpress 中的存档页面,并仅显示具有某些特定“元值”的帖子。我模板中的“archive.php”仅查找“类别”、“日”、“月”等,但我需要有可能通过自定义字段的“元值”进行过滤。我在另一个网站上看到过这个:

“www.mywebsite.com/cities/?city=seelbach”

(浏览器的地址栏)我试过了,但没有任何反应。

这是我的循环:

    <?php
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query(); 
        $wp_query->query('&showposts=6'.'&paged='.$paged); 
    ?>
    <?php $count = 0; ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $count++; ?>

        <li class="cf">

            <div class="jobs-stream-leftcol">
                <h2><?php the_title(); ?></h2>

                <?php echo get_post_meta($post->ID, 'description', true); ?>
                <br />
                <strong>Wo:</strong> <?php echo get_post_meta($post->ID, 'zip', true); ?>, <?php echo get_post_meta($post->ID, 'city', true); ?>
                <br />
                <strong>Frei ab:</strong> <?php echo get_post_meta($post->ID, 'date', true); ?>
            </div>


            <div class="jobs-stream-rightcol">
                <a href="#" class="jobs-stream-link-go">› Jetzt bewerben</a><br />
                <a href="<?php the_permalink() ?>">› Mehr Info</a>
            </div>

        </li>

    <?php endwhile; ?>        

    </ul>

【问题讨论】:

  • 您是否要过滤与 URL 婴儿车一起显示的内容?你的循环的一些代码会有帮助
  • 我有一个标准循环,编辑了我的问题。我按照本教程进行操作:advancedcustomfields.com/resources/tutorials/… 但没有任何反应,我认为这只适用于“高级自定义字段”?

标签: php wordpress filter


【解决方案1】:

稍作修改

   <?php
        $cityvar = $_POST["city"];
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query(); 
        $wp_query->query('&showposts=6'.'&paged='.$paged); 
    ?>
    <?php $count = 0; ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $count++; 

    if (get_your_custom_meta_Value_here() == $cityvar){

    ?>

        <li class="cf">

            <div class="jobs-stream-leftcol">
                <h2><?php the_title(); ?></h2>

                <?php echo get_post_meta($post->ID, 'description', true); ?>
                <br />
                <strong>Wo:</strong> <?php echo get_post_meta($post->ID, 'zip', true); ?>, <?php echo get_post_meta($post->ID, 'city', true); ?>
                <br />
                <strong>Frei ab:</strong> <?php echo get_post_meta($post->ID, 'date', true); ?>
            </div>


            <div class="jobs-stream-rightcol">
                <a href="#" class="jobs-stream-link-go">› Jetzt bewerben</a><br />
                <a href="<?php the_permalink() ?>">› Mehr Info</a>
            </div>

        </li>

    <?php
    }
    endwhile; ?>   

【讨论】:

  • 好的,我创建了一个页面(“clubs”),我已经在该页面中放置了循环,然后我链接到“www.mywebsite.com/clubs/?city=berlin”,但我得到了一个404 错误。我必须替换“get_your_custom_meta_Value_here”吗?
  • 是的 codex.wordpress.org/Custom_Fields 和 404,因为这不是你在 wordpress 中链接内容的方式,查看页面然后在末尾附加 ?city=berlin
【解决方案2】:

您将不得不深入研究代码。假设你有一个查询字符串:

//Get the query string
$myMetaValue = $_GET['myMetaKey']; 

//Set Up your arguments including the metaValue
$args = array(
  'post_type' => 'any',
  'meta_query' => array(
   array(
    'key' => 'myMetaKey',
    'value' => $myMetaValue
  )
)

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) { ...

希望这会有所帮助!

【讨论】:

  • 你的意思是我必须编辑着陆页上的循环来获取 URL 参数?
  • 这就是我会做的。无论帖子显示在哪里,您都想在哪里添加此内容
猜你喜欢
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 2014-04-30
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多