Answer the question
In order to leave comments, you need to log in
How to display news of a specific tag on a page?
There is a BMW page, where there is a description of the logo and dt, and I want to display the news there, by the BMW tag.
To choose which label on which page I decided to use Advanced Custom Fields, I got the following code
<?php
$term = the_field('btag');
$query = new WP_Query("tag_id=$term");
?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php if ( get_field('s_img') ) {
include "block/sshortnews.php";
}
else {
include "block/shortnews.php";
}
?>
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
<div class="wp-pagenavi"><?php if (function_exists('wp_corenavi')) wp_corenavi(); ?></div>
<?php else : ?>
<?php endif; ?>
Answer the question
In order to leave comments, you need to log in
/* Если используем слаг-ярлык тэга */
<?php
$post_tag = 'BMW';
$the_query = new WP_Query( 'tag='.$post_tag );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// постов нет
}
/* Сбрасываем значение переменной $post до стандартного */
wp_reset_postdata();
/* Если используем ID тэга */
<?php
$post_tag = 29;
$the_query = new WP_Query( 'tag='.$post_tag );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// постов нет
}
/* Сбрасываем значение переменной $post до стандартного */
wp_reset_postdata();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question