不同品牌的自定义字段链接按钮



i m为手机创建一个WordPress主题,其中我使用了带有"品牌"名称的自定义字段。并添加三种类型的品牌价值LG,诺基亚和索尼..

然后如何为这三种值创建三个单独的链接按钮,这些链接显示了所有其他帖子以相同的品牌价值。

   <a href="#"><?php echo get_post_meta( get_the_ID(), 'brand', true ); ?>
        <?php _e( '', 'mobilewebsite' ); ?></a>
    <li><a href="#">Nokia</a></li>
    <li><a href="#">LG</a></li>
    <li><a href="#">Sony</a></li>

假设品牌自定义字段是帖子的元值,您可以使用此代码回声每个品牌字段。

<?php  
// WP_Query arguments
$args = array (
    'post_type'     => array( 'post' ),
    'post_status'   => array( 'publish' ),
    'meta_query'    => array(
        array(
            'key'   => 'brand',
        ),
    ),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        ?>
        <a href="#">
            <?php echo get_field('brand'); ?>
        </a>
        <?php
    }
} else {
    // no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>

最新更新