How to list pages starting from a tag?
Hello there! I'm trying to get a list of a given number of pages related to a specific tag, in order to Insert them in a specific spot on my site. Just like the "recent posts" in the sidebar (BlogX theme) but using a specific tag.
I've tried several times in different ways using the sidebar "latest posts" code and the "List all pages related to a particular tag" as a starting point, but with no avail... Not a PHP genius here; probably I get close but I must have broken the code with syntax errors.
Any idea/suggestion, please?
(I'm on 3.15.0 / customized BlogX theme)
I've tried several times in different ways using the sidebar "latest posts" code and the "List all pages related to a particular tag" as a starting point, but with no avail... Not a PHP genius here; probably I get close but I must have broken the code with syntax errors.
Any idea/suggestion, please?
(I'm on 3.15.0 / customized BlogX theme)
Edit Your Theme's Functions.php:
Go to your WordPress dashboard.
Navigate to "Appearance" > "Theme Editor."
On the right side, you should see a list of theme files. Look for functions.php and click on it.
Add Custom Function:
Insert the following code into your functions.php file:
function custom_tag_pages_widget() {
$tag_slug = 'your-tag-slug'; // Replace 'your-tag-slug' with the actual tag slug
$args = array(
'post_type' => 'page',
'posts_per_page' => 5, // Adjust the number of pages to display
'tag' => $tag_slug,
);
$tag_query = new WP_Query($args);
if ($tag_query->have_posts()) {
echo '<ul>';
while ($tag_query->have_posts()) {
$tag_query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
wp_reset_postdata();
}
}
// Add the widget to the sidebar
add_action('widgets_init', function () {
register_widget('Custom_Tag_Pages_Widget');
});
class Custom_Tag_Pages_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'custom_tag_pages_widget',
__('Custom Tag Pages Widget', 'text_domain'),
array('description' => __('Display pages related to a specific tag.', 'text_domain'))
);
}
public function widget($args, $instance) {
custom_tag_pages_widget();
}
}
After you just need to save changes.
Go to your WordPress dashboard.
Navigate to "Appearance" > "Theme Editor."
On the right side, you should see a list of theme files. Look for functions.php and click on it.
Add Custom Function:
Insert the following code into your functions.php file:
function custom_tag_pages_widget() {
$tag_slug = 'your-tag-slug'; // Replace 'your-tag-slug' with the actual tag slug
$args = array(
'post_type' => 'page',
'posts_per_page' => 5, // Adjust the number of pages to display
'tag' => $tag_slug,
);
$tag_query = new WP_Query($args);
if ($tag_query->have_posts()) {
echo '<ul>';
while ($tag_query->have_posts()) {
$tag_query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
wp_reset_postdata();
}
}
// Add the widget to the sidebar
add_action('widgets_init', function () {
register_widget('Custom_Tag_Pages_Widget');
});
class Custom_Tag_Pages_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'custom_tag_pages_widget',
__('Custom Tag Pages Widget', 'text_domain'),
array('description' => __('Display pages related to a specific tag.', 'text_domain'))
);
}
public function widget($args, $instance) {
custom_tag_pages_widget();
}
}
After you just need to save changes.