Make current comment on a WordPress post active - php

I'm using Bootstrap carousel to display 3 comments of a specific post in WordPress and the latest comment must have the active class on the div. However, the code that I have adds the active class to all 3 comments. Hence, all 3 shows in a single carousel block, where it should be divided or paginated by 3. Here is my current code:
<?php
$args = array(
'status' => 'approve',
'number' => '1',
'post_id' => 9,
'orderby' => 'comment_date',
'order' => 'DESC'
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
echo '<div class="carousel-item active">
<div class="carousel-caption">
<span class="fad fa-quote-right" aria-hidden="true"></span>';
comment_text();
echo '<div id="image-caption">— '.$comment->comment_author.'</div>
</div><!-- carousel-caption -->
</div><!-- carousel-item -->';
endforeach;
?>
I would like to have a condition where if comment is latest, add $active = ' active'; else, show nothing to the <div class="carousel-item'.$active.'">.
Is this possible?

Here you go... Use for instead of foreach and check if that is the first item ($i==0)
<?php
$args = array(
'status' => 'approve',
'number' => '1',
'post_id' => 9,
'orderby' => 'comment_date',
'order' => 'DESC'
);
$comments = get_comments( $args );
for($i=0; $i<count($comments); $i++ ) :
$comment = $comments[$i];
echo '<div class="carousel-item ' . ($i==0 ? "active" : "") . '">
<div class="carousel-caption">
<span class="fad fa-quote-right" aria-hidden="true"></span>';
comment_text();
echo '<div id="image-caption">— '.$comment->comment_author.'</div>
</div><!-- carousel-caption -->
</div><!-- carousel-item -->';
endfor;
?>

Related

Pagination wordpress does not display

I have coded my theme from base and I have a problem displaying pagination in my page.
I have a foreach over my posts that is working properly and it is showing 9 posts in every page, but I could not display paginations below that to switch between page.
It would be great if someone can help me with it.
<div class="row">
<?php
$args = array(
'posts_per_page' => 9,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post); ?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?= get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach (get_the_category($post->ID) as $category) {
echo '' . $category->name . '';
} ?> |
<span>
مدت زمان مطالعه
<?= get_field('duration'); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php endforeach; ?>
<?= previous_posts_link()?>
</div>
It does not display with any of these built in functions
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
The functions you mention:
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
All require that the global $wp_query variable contains the necessary fields to generate the pagination (namely found_posts, paged, and posts_per_page).
Since you're using get_posts, the fields in $wp_query will never be set.
Instead, you can overwrite the $wp_query variable with a WP_Query instance which means the fields will be set. It is good practice to then restore $wp_query after your loop in case you have any data later in the page or in the footer that relies on it.
Example:
global $wp_query;
$args = array(
'posts_per_page' => 9,
// 'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
// this one is important, it lets the query object know what page you're on.
'paged' => get_query_var( 'paged' ),
// are you sure you want to suppress filters ? make sure you know what this does.
'suppress_filters' => true,
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?php echo get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach ( get_the_category( get_the_ID() ) as $category ) {
echo '' . $category->name . '';
}
?>
|
<span>
مدت زمان مطالعه
<?php echo get_field( 'duration' ); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php
endwhile;
the_posts_pagination();
// restore the original $wp_query.
wp_reset_postdata();

Is there a way to list all the authors who have published videos in a certain category and show them together, like a list?

Please help me with this website https://desarrollowebtotal.space/yoentrenoencasa/
I'm using goWatch theme from touchsize as parent theme.
What I need is this:
When I click on any of the categories listed on the home page, e.g. Yoga, the site will navigate to an archive with all the videos created in that taxonomy, it is not the behavior I want.
What I want is for that archive to return a list of authors who have published videos in that category and nested, as a list, show the videos associated with that author within that category.
Is it possible to do this?
// This query brings the videos within the videos_categories taxonomy that match the current slug
$term = get_queried_object();
$loop = new WP_Query( array(
'post_type' => 'video',
'posts_per_page' => 10,
'tax_query' => array( //(array) - use taxonomy parameters (available with Version 3.1).
'relation' => 'AND', //(string) - Possible values are 'AND' or 'OR' and is the equivalent of running a JOIN for each taxonomy
array(
'taxonomy' => 'videos_categories', //(string) - Taxonomy.
'field' => 'slug', //(string) - Select taxonomy term by ('id' or 'slug')
terms' => $term->slug, //(int/string/array) - Taxonomy term(s).
include_children' => true, //(bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
'operator' => 'IN' //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
),
),
) );
This is what I've done so far, I have no idea how to nest the video listings so that the parent is the author and the child the videos associated with that author
//UPDATE 1
$argsA = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => null,
'optioncount' => false,
'exclude_admin' => true,
'show_fullname' => false,
'hide_empty' => false,
'echo' => true,
// 'feed' => [empty string],
// 'feed_image' => [empty string],
// 'feed_type' => [empty string],
'style' => 'list',
'html' => true,
);
$authors = wp_list_authors($argsA);
// This query brings the videos within the videos_categories taxonomy that match the current slug
$term = get_queried_object();
HTML PART WHERE I LOOP THE RESULTS
<div class="container airkit-archive-content">
<div class="row">
<?php if (!empty($authors)): ?>
<?php foreach ($authors as $author): ?>
<?php
$author_id = $author->ID;
$author_display_name = $author->name;
$query_args = array(
'author' => $author_id,
'post_type' => 'video',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'videos_categories',
'field' => 'slug',
'terms' => $term->slug,
'include_children' => true,
'operator' => 'IN'
),
),
);
?>
<?php endforeach; ?>
<?php endif ?>
</div>
</div>
The OUTPUT
Cristian Entrenador
j.b
Jose Miguel
Yonatan T.
What i need
Trainer name
Videos he/she posted in the current category
UPDATE 2:
Doing as #Tokant requested/recommended i copy/pasted his snippet, it worked, it printed an author, i think it's because he's the only one who has posted videos
<div class="row">
<?php $term = get_queried_object(); ?>
<?php $authors_args = array(
'echo' => false,
'orderby' => 'name',
'order' => 'ASC',
);
$author_list = wp_list_authors($authors_args);
if (!empty($author_list)):
echo $author_list;
foreach ($author_list as $author):
$author_id = $author->ID;
$author_display_name = $author->name;
$query_args = array(
'author' => $author_id,
);
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
endwhile;
wp_reset_postdata();
endif;
endforeach;
endif; ?>
</div>
OUPUTS THIS
Jose Miguel
Good morning guys, thanks to Tokant I was able to find the solution in my own way.
What I did was put the layout together so that the hierarchy would be like this.
Author
- Videos posted in the current category.
This is the solution:
You have to install Buddypress plugin for 'orden' key to work, and add this script to functions.php of your child theme
/*Función para ordenar los autores*/
add_action('show_user_profile', 'extra_user_profile_fields');
add_action('edit_user_profile', 'extra_user_profile_fields');
function extra_user_profile_fields($user)
{ ?>
<?php if (current_user_can('administrator')) { ?>
<h3><?php _e("Administrar Posiciones", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="orden"><?php _e("Modificar Orden"); ?></label></th>
<td>
<input type="number" name="orden" id="orden" value="<?php echo esc_attr(get_the_author_meta('orden', $user->ID)); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Campo solo para administradores"); ?></span>
</td>
</tr>
<tr>
</table>
<?php } ?>
<?php }
add_action('personal_options_update', 'save_extra_user_profile_fields');
add_action('edit_user_profile_update', 'save_extra_user_profile_fields');
function save_extra_user_profile_fields($user_id)
{
if (!current_user_can('edit_user', $user_id)) {
return false;
}
update_user_meta($user_id, 'orden', $_POST['orden']);
}
Loop for the authors:
<?php
// Theme's WP_Query arguments
$airkit_video = '';
$figure_attributes = array();
$airkit_is_img = airkit_single_option('img');
$airkit_img_src =wp_get_attachment_url(get_post_thumbnail_id($videos->ID));
$airkit_video_type = get_post_meta($videos->ID, 'video_type', true);
$airkit_external_video = get_post_meta($videos->ID, 'video_url', true);
$airkit_embedded_video = get_post_meta($videos->ID, 'video_embed', true);
$airkit_uploaded_video = get_post_meta($videos->ID, 'video_upload', true);
//Arguments for users Query
$arg_user = array(
'role__in' => ['author'],
'post_type' => 'video',
'has_published_posts' => ['video'],
'fields' => ['ID'],
'meta_query' => array(
array(
'relation' => 'OR',
array('key' => 'orden', 'value' => '0', 'compare' => '!='),
array('key' => 'orden', 'compare' => 'NOT EXISTS'),
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
//var_dump($arg_user);
$yec_args = get_users($arg_user); //We obtain the list of all the users of the site to compare with the criteria of the query, it will look for in the author role, the users that have published entries in the custom post type video and it will avoid to bring the ones that do not have entries.
$yec_user_id = wp_list_pluck($yec_args, 'ID');
//print_r($yec_user_id);
?>
?>
Then we loop through each author and output the html
<div class="container container-todo" style="transform: none; display: flex;">
<div class="post-details-row current-reading" style="transform: none;">
<?php
// Loop & retrieve el ID authors ID
foreach ($yec_user_id as $author_id): //main FOREACH
// We get the ID of the current author who is returning the loop
$curauth = get_userdata($author_id);
//var_dump($curauth->user_nicename);
$args = array(
'posts_per_page' => -1,
'post_type' => 'video',
'author' => $author_id,
'groupby' => 'author',
'tax_query' => array(
array(
'taxonomy' => 'videos_categories',
'field' => 'slug',
'terms' => get_queried_object()->slug,
),
),
'meta_key' => 'airkit_views',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
//We return the videos and count them.
$posts = get_posts($args);
$number_post = count($posts);
//var_dump($number_post);
//if this author has posts, then include his name in the list otherwise don't
if (isset($posts) && !empty($posts)) { // MAIN IF?>
<div class="row" style="display: flex; align-items: center;">
<div class="sidebar-right col-lg-12 col-md-12 col-xs-12" style="display: flex; justify-content: center; flex-wrap: wrap;">
<!-- Datos del autor -->
<div class="tszf-author-body">
<div class="item col-md-12 col-xs-12" style="text-align: center;">
<div class="yec-user-image">
<img src="<?php echo esc_url(get_avatar_url($author_id)); ?>" />
</div>
<div class="item col-xs-12">
<span class="yec-authors"> <?php echo "" . $curauth->first_name . " " . $curauth->last_name . ""; ?> </span>
</div>
</div>
</div>
<div class="tszf-user-name">
<ul class="social"><?php echo airkit_var_sanitize($social_icons, 'the_kses'); ?></ul>
</div>
<!-- Fin de datos del autor -->
<!-- Aquí deben ir los vídeos de ese autor -->
<?php $count = 0; ?>
<?php foreach ($posts as $post) { ?>
<?php if ($count == "4") {
// Si hay vídeos no imprimas más de 2 y salte del condicional
break;
} else{ ?>
<!-- Este es el bloque iterable -->
<div class="item col-lg-4 col-md-6 col-sm-6 col-xs-12">
<article class="video type-video status-publish has-post-thumbnail hentry has-lazy-placeholder airkit_view-article text-left below-image effect-always hidden-excerpt has-image" itemscope="" itemtype="http://schema.org/Article">
<figure class="image-holder has-background-img lazy lazyloaded" style="background-image: url(<?php echo get_the_post_thumbnail_url($post->ID, 'full') ?>); display: block;">
<a class="post-format-link" href="<?php the_permalink(); ?>" title="<?php echo $post->post_title; ?>">
<span class="post-type"><i class="icon-play-full"></i></span>
</a>
<div class="overlay-effect ">
<a href="<?php the_permalink(); ?>" class="overlay-link darken">
</a>
</div>
</figure>
<header class="entry-content-below">
<div class="entry-content">
<ul class="entry-categories">
<li class="term">
<?php $slug = get_queried_object()->slug; ?>
<?php echo $post->post_title; ?>
<!-- <?php echo $slug; ?> -->
</li>
</ul>
<h2 class="entry-title" itemprop="name headline">
<?php echo $video->post_title; ?>
<div class="widget-meta">
<ul class="list-inline">
<li class="red-comments">
<a href="<?php echo get_permalink($post->ID) . '#comments' ?>">
<i class="icon-comments"></i>
<span class="comments-count">
<?php echo airkit_var_sanitize($post->comment_count, 'the_kses') . ' '; ?>
</span>
</a>
</li>
<li class="meta-views">
<i class="icon-views-1"></i> <?php airkit_get_views($post->ID, true); ?>
</li>
</ul>
</div>
</h2>
</div> <!-- Aquí termina el contenido de los vídeos -->
</header>
</article>
</div>
<!--Fin del bloque iterable -->
<?php $count++; } //CIERRE DEL IF DEL COUNT ?>
<?php } //CIERRE FOREACH DE POST ?>
</div>
<!-- Flecha que nos lleva al perfil del autor -->
<a href=" <?php echo get_option('siteurl') . '/members/' . $curauth->user_nicename . '/posts' ?>" class="post-link">
<span style="font-size: 3em; color: rgba(60,185,207,1);">
<i class="fas fa-angle-double-right" style="margin-top:50px"></i>
</span>
<br />
</a>
<span style="color: rgba(60,185,207,1);"> (<?php echo $number_post; ?>) <i class="fas fa-video"></i> </span>
</div>
<?php } // END of MAIN IF ?>
<?php endforeach; //END OF MAIN FOREACH ?>
You could do a WP_Query for all authors. In the loop for each author you first get the author's ID and then create a WP_Query for the 'video' post type, just like you have already done but adding the authour's ID in the $args, like so:
$authors_args = array(
'echo' => false,
'orderby' => 'name',
'order' => 'ASC',
);
$authors = wp_list_authors($args);
if (!empty($authors)):
foreach ($authors as $author):
$author_id = $author->ID;
$author_display_name = $author->name;
$query_args = array(
'author' => $author_id,
[your args]
);
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) :
[output author name]
while ( $the_query->have_posts() ) : $the_query->the_post();
[output post/video]
endwhile;
wp_reset_postdata();
endif;
endforeach;
endif;

Pagination in Wordpress home.php

Pagination isn't working in a Wordpress blog. I've tracked the specific file responsible, to home.php.
The original blog page displays posts fine (10 at a time), however as mentioned pagination isn't working.
<?php
/**
Category Page
*/
get_header(); ?>
<div class="container-fluid blogs post-section" id="blogs">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-9 col-lg-8">
<?php echo category_description( $category_id ); ?>
<div class="category-posts">
<?php
$args = array(
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => "10",
"orderby" => "date",
"order" => "DESC"
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-12">
<div class="inner-post">
<?php if ( has_post_thumbnail() ) { ?><div class="thumbnail col-sm-4" style="background:none!important;"><?php the_post_thumbnail(); ?></div><?php } ?>
<div class="abso custom_abso col-sm-8"><div class="inner-box">
<div class="date">
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('M');
$archive_day = get_the_time('d');
?>
<span class="month"><?php echo $archive_month; ?></span> <span class="day"><?php echo $archive_day; ?>,</span><span class="year"><?php echo $archive_year; ?></span>
</div>
<h2 style="margin-top:0;"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<p><a href="<?php the_permalink(); ?>" class="readmores">Read More <i class="fa fa-caret-right" aria-hidden="true"></i>
</a></p>
</div>
<?php endwhile; endif;
?></div>
<div class="pagination text-center">
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
</div>
<?php wp_reset_query(); ?>
</div>
<?php get_sidebar(); ?>
</div></div></div>
<?php get_footer(); ?>
Based on this Wordpress documentation, I've updated the top of the file, to look like the following:
<div class="category-posts">
<?php $args = array(
'posts_per_page' => 10,
'offset' => 0,
'cat' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
$posts_array = get_posts( $args ); ?>
<div class="col-sm-12">
This has worked, in that the pagination buttons work, however, only one post is displayed per page:
Any advice on how to display 10 posts per page?
Please add the following parameter 'paged'. It sets pagination query variable. The pagination is works as per this parameter.
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
to the $args.

Simple output from parent category

I am working with this code in template page for WP.
<div id="content" role="main">
<?php
$query = array(
'post_type' => 'post',
'category_name' => 'jewellery-design',
'orderby' => 'date',
'order' => 'DESC'
);
$featured_home = new WP_Query( $query );
if( $featured_home->have_posts() ) {
?>
<div class="row">
<?php while ( $featured_home->have_posts() ) : $featured_home->the_post();?>
<span class="cat"><?php the_category(); ?>
<div class="col-3">
<a href="<?php the_permalink(); ?>">
<div class="featured-home-img" <?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"', $image_src[0] );
}?>>
<div class="blog-info-content">
<h3><?php the_title(); ?></h3>
<div class="obsah"><?php the_content(); ?></div> </span>
<p class="postmetadata">
Posted in <?php the_category(', ') ?>
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>
</span>
</div>
</div>
</a>
</div>
<?php
endwhile;
?>
</div>
<?php
}
wp_reset_postdata();
?>
</div>
And I am getting output like this:
But, i´d like to have output like this:
Can someone tell me how can I do it? Or may I use some plugin for Wordpress to make it easier for me?
Thank you.
I might think about sorting them into groups first, then re-looping over the sorted groups. I am a little rusty on Wordpress so I have set it up essentially, but all the functions/methods need to return values, not echo, so you will need to adjust that:
<?php
# Create base function
function getItemsByCategoryType($category)
{
$query = array(
'post_type' => 'post',
'category_name' => $category,
'orderby' => 'date',
'order' => 'DESC'
);
# Get query
$featured_home = new WP_Query($query);
# Just stop if empty
if(!$featured_home->have_posts())
return false;
# Set a default
$array = array();
while($featured_home->have_posts()){
$featured_home->the_post();
$thumbnail_id = get_post_thumbnail_id();
$image_src = ($thumbnail_id)? wp_get_attachment_image_src($thumbnail_id,'normal-bg')) : false;
# You are looking to store the category title as the key
# That will isolate each group
$array[the_category()][] = array(
'thumb_id' => $thumbnail_id,
'image' => $image_src,
'permlink' => the_permalink(),
'title' => the_title(),
'content' => the_content(),
'categories' => the_category(', '),
'edit_mode' => edit_post_link('Edit','','<strong>|</strong>')
);
}
wp_reset_postdata();
# Send back stored data as an array
return $array;
}
# To use, get the data from your function
$Items = getItemsByCategoryType('jewellery-design') ?>
<div id="content" role="main">
<?php if(!empty($Items)): ?>
<div class="row">
<?php foreach($Items as $categories => $rows): ?>
<span class="cat"><?php echo $categories ?>
<?php foreach($rows as $row): ?>
<div class="col-3">
<a href="<?php echo $row['permlink'] ?>">
<div class="featured-home-img"<?php if(!empty($row['image'][0])) echo ' style="background-image: url('.$row['image'][0].');"' ?>>
<div class="blog-info-content">
<h3><?php echo $row['title'] ?></h3>
<div class="obsah">
<?php echo $row['content'] ?>
</div>
<p class="postmetadata">Posted in <?php echo $row['categories'] ?><strong>|</strong><?php $row['edit_mode'] ?></p>
</div>
</div>
</a>
</div>
<?php endforeach ?>
</span>
<?php endforeach ?>
</div>
<?php endif ?>
</div>
NOTE: Just to reiterate, I am not up to speed on WP so you need to take the idea of what I am proposing and fixing it, specifically when it comes to returning values vs allowing the WP functions to echo content.
EDIT:
You want an array to come out of the function similar to:
Array (
[ Fibre Collection ] => Array (
[ 0 ] => Array (
[ title ] => FIBRE_BRACELET
[ content ] => etc...,
etc...
)
),
[ Tetragon Collection ] => Array (
[ 0 ] => Array (
[ title ] => Test2
[ content ] => etc...,
etc...
),
[ 1 ] => Array (
[ title ] => TETRAGÓN_BRACELET
[ content ] => etc...,
etc...
)
)
)
I found a solution to the output I needed. If this helps anyone :)
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 2
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<div class="test"><p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<p><a href="<?php the_permalink() ?>" <?php the_content(); ?></a></p>
<?php
} // foreach($posts
echo '</div>';} // if ($posts
} // foreach($categories
?>
The 'parent' => 2 means ID of parent category from which you want to get your posts.
Have a nice day.

How to restrict what tags are fetched in WordPress

I have this PHP script to fetch tags (as we use WordPress as a CMS, the tags are "categories" for our partners).... However, I'm trying to free up "tags" to be used on blog posts - but currently the code below fetches ALL tags - how can I restrict this to only tags I specify (happy to enter each Tag ID)
Here is the code:
<?php
$tags = get_tags('order=ASC&orderby=name);
foreach ( $tags as $tag ) { ?>
<?php $tag_name = $tag->slug;
echo '<div class="col-md-12 padbot50" id="tabs-' . $tag->slug . '">';
echo '<div class="green_txt font20">' .$tag->name. '</div>';
//echo $tag_name;
$args = array( 'posts_per_page' => 10, 'cat'=> 11, 'tag' => $tag_name, 'orderby' => 'slug', 'order' => 'ASC' );
query_posts( $args );
// The Loop
?>
<div class="row">
<?php
while ( have_posts() ) : the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true);
$thumb_url = $thumb_url_array[0];
$post_id = get_the_ID();
?>
<div class="col-md-6 col-sm-5 col-xs-7 pad10 center"> <a href="<?php the_permalink(); ?>">
<div class="partners_container">
<div class="row" style="height:125px;">
<div class="col-md-12 col-xs-12"><div class="left"> <img src="<?php echo $thumb_url; ?>" height="100%" alt="Volo Commerce Partners - Multichannel Automated Back Office Software. eBay, Amazon, Rakuten" title="Volo Commerce Partners - Multichannel eCommerce Software. Stock & Inventory Management"/><div class="partner-more-icon"></div></div></div>
</div>
<div class="padtop10" style="text-align:left;font-weight:bold;color:#f08f00!important;"> <?php echo '<div>' . the_title() . '</div>';?> </div>
<div class="padtop10 left"> <?php echo '<div>' . the_field('partner_page_description', $post_id ) . '</div>';?> </div>
</div>
</a> </div>
<?php
endwhile;
?>
</div>
</div>
<?php
wp_reset_postdata(); }?>
If anyone could be so kind to tell me how I fetch ONLY certain tag ID's - that would be absolutely perfect!
Thanks muchly!
This is a basic example of WP_Query which should get you started:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'slug',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'field' => 11
),
array(
'taxonomy' => 'tag',
'field' => 'term_id',
'field' => array( /* Your Tag IDs */ ),
'operator' => 'IN'
)
)
);
$post_query = new WP_Query( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) :
$post_query->the_post();
// Inner loop stuff code
endwhile;
endif;
wp_reset_postdata();?>
get_tags function accepts include argument. You could pass by it ids of tags you want to get.
$tags = get_tags('order=ASC&orderby=name');
change to
$tags = get_tags('order=ASC&orderby=name&include=12,45,67');
12, 45 and 67 are ids of tags
get_tags on codex

Categories