最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use Advanced Custom Fields - replace line of code that hardcodes the tag into the template

programmeradmin0浏览0评论

I am trying to tweak a page template for a WordPress site that uses Advanced Custom Fields and I am stuck on replacing this line of code (which hard codes the tag test123 in the template):

<?php   $tag_query = new WP_Query( array( 'tag' => 'test123' ) );   ?>

This is the relevant code currently in the template file

<?php   
$blog_tag   = get_field('blog_tag');
?>
<?php   $tag_query = new WP_Query( array( 'tag' => 'test123' ) );   ?>
<?php while ($tag_query -> have_posts()) : $tag_query -> the_post(); ?>
<div id="blog" class="col-md-4">
    <div class="card card-plain">
        <div class="card-image no-shadow"><a href="<?php the_permalink() ?>">
            <?php if (has_post_thumbnail() ) { ?>
            <div class="atvImg">
                <?php the_post_thumbnail();?>
            </div>
            <?php } ?>
        </div></a>
        <div class="card-content">
            <h6 class="categories text-success"><?php the_category(', ')?></h6>

            <header class="entry-header">
                <h3 class="card-title"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
            </header>
        </div>
    </div>
</div>  

I created a new custom field with field name "blog_tag". When editing the Wordpress Page I can choose the tags etc so this bit seems aok.

I was hoping for a simple solution like this to just replace the one line of code in the template but I can't get it to work:

<?php   $tag_query = new WP_Query( array( 'tag' => 'echo $blog_tag;' ) );   ?>

The display is fine - just want to make the tag(s) dynamic instead of hardcoded.

I have tried about 10 different combinations of code that I have found from searching for a solution but I can't get it to work.

Any ideas?

Many thanks in advance.

ps my php is very limited.

I am trying to tweak a page template for a WordPress site that uses Advanced Custom Fields and I am stuck on replacing this line of code (which hard codes the tag test123 in the template):

<?php   $tag_query = new WP_Query( array( 'tag' => 'test123' ) );   ?>

This is the relevant code currently in the template file

<?php   
$blog_tag   = get_field('blog_tag');
?>
<?php   $tag_query = new WP_Query( array( 'tag' => 'test123' ) );   ?>
<?php while ($tag_query -> have_posts()) : $tag_query -> the_post(); ?>
<div id="blog" class="col-md-4">
    <div class="card card-plain">
        <div class="card-image no-shadow"><a href="<?php the_permalink() ?>">
            <?php if (has_post_thumbnail() ) { ?>
            <div class="atvImg">
                <?php the_post_thumbnail();?>
            </div>
            <?php } ?>
        </div></a>
        <div class="card-content">
            <h6 class="categories text-success"><?php the_category(', ')?></h6>

            <header class="entry-header">
                <h3 class="card-title"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
            </header>
        </div>
    </div>
</div>  

I created a new custom field with field name "blog_tag". When editing the Wordpress Page I can choose the tags etc so this bit seems aok.

I was hoping for a simple solution like this to just replace the one line of code in the template but I can't get it to work:

<?php   $tag_query = new WP_Query( array( 'tag' => 'echo $blog_tag;' ) );   ?>

The display is fine - just want to make the tag(s) dynamic instead of hardcoded.

I have tried about 10 different combinations of code that I have found from searching for a solution but I can't get it to work.

Any ideas?

Many thanks in advance.

ps my php is very limited.

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Feb 17, 2020 at 5:14 user3643520user3643520 175 bronze badges 3
  • Let me know which parameter you have set for return value in Advanced Custom Field? like Term Object OR Term ID – Tanmay Patel Commented Feb 17, 2020 at 5:25
  • Term ID at the moment (not sure if that is correct?) Thanks. – user3643520 Commented Feb 17, 2020 at 5:31
  • If you can attach full screenshot of advanced custom field and also attach one of the post with this field display so It is easy to understand your question. – Tanmay Patel Commented Feb 17, 2020 at 5:40
Add a comment  | 

1 Answer 1

Reset to default 1

Display post by Using ONE Tag

Display post by tag id in same way. You just need to change the tag parameter name. This will display post which are tagged with 13 tag id. Here 13 is tag ids which is used just for example to explain in better way.

$tag_query = new WP_Query( array( 'tag_id' => '13' ) );

Display posts from either tag

To display posts from either tag you need to use tag__in. This will return posts from either tag id.

$tag_query = new WP_Query( array( 'tag__in' => '13, 27' ) );

Now, you can try below code to fix your issue.

<?php $tag_query = new WP_Query(array('tag__in' => $blog_tag)); ?>
发布评论

评论列表(0)

  1. 暂无评论