I want have the structure of posts on wordpress site enter link description here
The top row name (slug) posts:
<?php
$tags = get_categories('orderby=name&taxonomy=references&order=ASC');
$output = '<table><tr><td> </td>';
foreach ( $tags as $tag ) {
$term = get_term_by('id', (int)$tag->term_id, 'references');
$output .= '<td>' . $tag->name . '</td>';
}
echo $output . '</tr>';
?>
Then the left column same name posts:
<?php
$tags2 = get_categories('orderby=name&taxonomy=references&order=ASC');
$output = '';
foreach ( $tags2 as $tag ) {
$term = get_term_by('id', (int)$tag->term_id, 'references');
$output .= '<tr><td>' . $tag->name . '</td><td>THIS PLACE -PROBLEM </td></tr>';
}
echo $output . '</table>';
?>
I have custom taxonomy "references". Terms in this taxonomy have same name as the name (slug) of post. Even posts link to several term in taxonomy and even term in taxonomy belongs to several posts.
At intersections of of rows and columns I need to get the value (true or false): if the post of the column to the left refers to the post of the top line put the true, if not, false.
I think it could be resolved if you creat two-dimensional matrix.
For example
$tags = get_categories('orderby=name&taxonomy=references&order=ASC');
$tags2 = get_categories('orderby=name&taxonomy=references&order=ASC');
Then
foreach($tags as $v1){
foreach($tags2 as $v2){
if( $v1 == $v2 /* some action I haven`t understand what you wrote*/){
/*output code */
}
}
}
Or I have mistake ?
Related
I'm trying to build a query to a custom record type that would eventually output a category, and then all posts from that category. However, there is a point with categories with hierarchy. For such categories, I would like to display the parent category once, and not every time. So, I have this code:
<?php
$query = new WP_Query([
'post_type' => 'afdigit',
'posts_per_page' => -1,
'orderby' => 'none',
]);
if ($query->have_posts()) { ?>
<div class="cat">
<?php $q = array();
while ($query->have_posts()) {
$query->the_post();
$a = '' . get_the_title() . '';
$top_term = get_top_term('afd-cats');
$b = '' . $top_term->name . '';
$categories = get_the_terms($post_id, 'afd-cats');
foreach ($categories as $key => $category) {
$c = '' . $category->name . '';
}
if ($top_term->term_id == $category->term_id) {
$b;
} else {
$b = '<div class="top term_">' . $b . '</div>';
$b .= '<div class="child">' . $c . '</div>';
}
$q[$b][] = $a;
}
wp_reset_postdata();
foreach ($q as $key => $values) {
echo '<div class="term">';
echo $key;
echo '<ul class="posts">';
foreach ($values as $value) {
echo '<li class="post">' . $value . '</li>';
}
echo '</ul></div>';
} ?>
</div>
<?php }
wp_reset_postdata(); ?>
In function get_top_term(), I get the parent category if it exists.
function get_top_term($taxonomy, $post_id = 0)
{
isset($post_id->ID) && $post_id = $post_id->ID;
!$post_id && $post_id = get_the_ID();
$terms = get_the_terms($post_id, $taxonomy);
if (!$terms || is_wp_error($terms)) {
return $terms;
}
$term = array_shift($terms);
$parent_id = $term->parent;
while ($parent_id) {
$term = get_term_by('id', $parent_id, $term->taxonomy);
$parent_id = $term->parent;
}
return $term;
}
Here I get almost what I need, namely:
Category 1
Post
Post
Post
Category 2
Post
Post
Post
Parent category 1
Child category 1
Post
Post
Post
Parent category 1
Child category 2
Post
Post
Post
Category 3
Post
Post
Post
However, I would like to exclude repetitions of parent category so that it turns out like this:
Category 1
Post
Post
Post
Category 2
Post
Post
Post
Parent category 1
Child category 1
Post
Post
Post
Child category 2
Post
Post
Post
Category 3
Post
Post
Post
Unfortunately, I have too little knowledge of php. I hope for your help both in solving the first question, and perhaps with advice on how to improve this code. Thanks)
I would like to show the product variations combinations in the same dropdown show as:
I override the variations.php into the woocommerce folder in my child theme. This function show the information well:
$available_variations = $product->get_available_variations();
if (count($available_variations) > 0) {
$output = '<div class="product-variations-dropdown">
<select id="available-variations" class="form-select product-selector mb-4" name="available_variations">';
$output .= '<option value="">' . __('Choose a variation') . '</option>';
foreach ($available_variations as $variation) {
$option_value = array();
foreach ($variation['attributes'] as $attribute => $term_slug) {
$taxonomy = str_replace('attribute_', '', $attribute);
$attribute_name = get_taxonomy($taxonomy)->labels->singular_name; // Attribute name
$term_name = get_term_by('slug', $term_slug, $taxonomy)->name; // Attribute value term name
$option_value[] = $attribute_name . ': ' . $term_name;
}
$option_value = implode(' | ', $option_value);
$output .= '<option value="' . $variation['variation_id'] . '">' . $option_value . '</option>';
}
$output .= '
</select>
</div>';
echo $output;
}
Next, the problem I have is that I don't know how when selecting the add to cart button in the drop-down menu, it activates the button, show price and shows the clean button. Once obtained, I will not know for sure if adding to the cart will add the product/variation correctly.
UPDATE:
I have seen that internally Wordpress works like this. Each selector contains one parameter. Is it possible that it is not in the correct php file? I mean this change may need to be done before you get to variations.php.
I´m trying to display a custom taxonomy item description in the frontend on a single post.
Following situation:
Created a CPT for "Weine"
Added a taxonomy "winzer"
Added the following code to my functions.php to display the "winzer" taxonomy item with its description in the single post using a shortcode:
function wpb_catlist_desc() {
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$string .= '<div>'. $item->name . '<br />';
$string .= '<em>'. $item->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
The code is working well, but it’s displaying all the items I created in the “Winzer” Taxonomy.
I just want to display the Item which is related to the single post.
Any ideas on how to change the code to get this done?
Cheers!!!!
You could use get_the_terms() instead of using get_terms(). So you could do something like this:
add_shortcode('wpb_categories', 'wpb_catlist_desc');
$the_id_of_current_post = get_the_ID(); // get the id of the current post
// pass the id to your function
function wpb_catlist_desc($the_id_of_current_post)
{
$string = '<div>';
$$catlist = get_the_terms( $the_id_of_current_post, 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $cat ) {
$string .= '<div>'. $cat->name . '<br />';
$string .= '<em>'. $cat->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
Or an alternative way would be something like this:
add_shortcode('wpb_categories', 'wpb_catlist_desc');
function wpb_catlist_desc()
{
global $post;
$the_id_of_current_post = $post->ID; // get the id of the current post
$string = '<div>';
$$catlist = get_the_terms( $the_id_of_current_post, 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $cat ) {
$string .= '<div>'. $cat->name . '<br />';
$string .= '<em>'. $cat->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
Let me know if you were able to get this to work!
I have this complex loop:
<?php
$args = array(
'cat' => 54,
'order' => 'ASC',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<h2>' . get_the_title() .'</h2>'
. get_the_post_thumbnail() .
'<p>' . get_the_content("...plačiau") . '</p>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<h1 class="thetitle">' . $category->name . '<span>Išskleisti <i class="fa fa-arrow-circle-down"></i></span></h1>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<div class="straipsniai">';
foreach ($values as $value){
if (count($values) == 1) {
echo '<div class="vienas">' . $value . '</div>';
} else if (count($values) == 2) {
echo '<div class="du">' . $value . '</div>';
} else if (count($values) == 3) {
echo '<div class="trys">' . $value . '</div>';
} else {
echo '<div>' . $value . '</div>';
}
}
echo '</div>';
}
?>
Which worked for me, gave me this nice list/accordion:
http://bruzienesklinika.lt/web/gydytojai/
Now, each person in the category has some articles as posts and I want a list of their articles under their description. (basic Title + exerpt + read more link)
I've tried to do this by using "List category posts" plugin which allows me to use [catlist id=24] shortcode, but the problem is that browser prints it as plain text, source shows [catlist id=24](You can open the most bottom "GYDYTOJA REUMATOLOGĖ" tab to see that). The shortcode does work inside page, which is rendered by single.php, but it does not show when rendered in the loop I gave you in the beginning of the question.
SO, the question is, how do I make the shortcode work in the initial list, where all the categories are listed with posts inside the accordion.
Now it is not the problem of this certain plugin because no shortcodes work in that accordion list.
Or maybe you have an idea how to do this the other way?
I am using the get_categories() function to manually create myself a nav menu. I have a custom taxonomy I'm using called Category and I'm trying to return the link for it for my tags in the menu using the get_category_link() function.
foreach ($categories as $category) {
if ($category->parent == 0) { //Check to see it is a parent
$output .= '<li>';
$output .= '' . $category->name . ''; //display parent taxonomy category
}
}
But it always returns <a href="">. I can echo out the $category->cat_ID successfully so I know it is passing the ID into the function but I don't know why it's returning blank.
Am I missing something? Is it because these are custom taxonomies? They have slugs.
You need something like this for custom taxonomies:
$tax = 'cars';
$cats = get_terms( $tax, '' );
if ($cats) {
foreach($cats as $cat) {
$output .= "<li>";
$output .= '<a href="' . esc_attr(get_term_link($cat, $tax)) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a>';
$output .= "</li>";
}
}
Although you can easily add to the top of the script to get an array of all taxonomies to feed in if you wanted.