Print icon for taxonomy terms - php

In my wordpress theme Taxonomy = my-category & categories are AB,AC,AD etc for custom post type.
I want to print icons for custom categories, and using if & else statement inside wp_query on index.php below is my code.
$args = array(
'post_type' => 'mycpt',
'posts_per_page' => 10,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
the_content();
if ($term_id === 10){
echo '<p class="icon-gear"></p>';
}
else if ($term_id === 11){
echo '<p class="icon-star"></p>';
}
else if ($term_id === 12){
echo '<p class="icon-tv"></p>';
}
//and continuous ..............
}
}
CSS
.icon-gear:before { background:url('gear.svg') no-repeat; }
.icon-star:before { background:url('star.svg') no-repeat; }
.icon-tv:before { background:url('tv.svg') no-repeat; }
How to print svg icon for taxonomy categories?

Tested code, try this
$args = array(
'post_type' => 'mycpt',
'posts_per_page' => 10,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
the_content();
$terms = get_the_terms( $post->ID, 'my-category');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
if ($termID[0] === 10){
echo '<p class="icon-gear"></p>';
}
else if ($termID[0] === 11){
echo '<p class="icon-star"></p>';
}
else if ($termID[0] === 12){
echo '<p class="icon-tv"></p>';
}
//and continuous ..............
}
}

I think this is the wrong way to go about it and is not maintainable.
Personally, I would be looking into using custom fields to add a text field to your categories and you can set the icon for each one through the dashboard. Then you just have to echo one custom field during your loop and can default to blank/default icon if it is empty.
Look into Advanced Custom Fields plugin if you are not already using it.

Related

CF7 Flamingo get messages from specific form

How can i get the messages from a certain form?
With this i can get all the inbound messages:
<?php
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo '<article>';
echo get_the_title();
echo '</article>';
}
}
wp_reset_postdata();
?>
But i wanna show only the messages from a specific form for the current month. I search on Google and here but i can not find any example or anything for it so i hope somebody knows.
I have made a solution, maybe not the nicest php writing but it works:
<?php
$currentmonth = date('m');
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish',
'monthnum' => $currentmonth
);
$query = new WP_Query($args);
if( $query->have_posts() ){
echo '<ul>';
while( $query->have_posts() ){
$query->the_post();
$results = get_post_meta( get_the_ID() );
foreach($results['_meta'] as $result) {
$normaldata = unserialize($result);
if ($normaldata['post_id'] == '1234') { // The id of the post where the form is send from
$title = get_the_title();
echo '<li>' . $title . '</li>';
} else {
}
}
}
echo '</ul>';
}
wp_reset_postdata();
?>

WordPress display loop in 2 columns

I know this question has been asked before. I checked through multiple answers on this site,
for example:
Wordpress loop with different bootstrap columns
https://wordpress.stackexchange.com/questions/222278/how-to-separate-posts-loop-in-to-two-columns/222281
... but I cannot work out how to integrate answers with my code (assuming that is possible).
I want to display a list of Categories and their related posts on a page.
The code I'm using works fine BUT displays the results in a single column down the page:
I want to split the display into 2 columns, like in the image below, if possible:
The code I'm using (currently placed in a new page template) is as follows:
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
get_footer();
?>
Wondering if anyone can help me to get this code to display loop results in 2 columns.
Many thanks.
UPDATE TO QUESTION
Karl, thanks for your answer. Your script works, but with a small problem:
The Categories/Related Posts display in 2 columns but a 'gap/space' appears in the middle of the display of data (see image below):
I added to your code slightly so I could display a custom field I inserted into each post. I'm not sure if this has caused the problem.
Altered code (changes are immediately after $query->the_post();):
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
?>
<div style="height:100px"></div>
<?php
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'>";
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'>";
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$customfieldvalue = get_post_meta($post->ID, "PDF", true);
?>
<p><a href="<?php echo $customfieldvalue; ?>" target="_blank"><?php
the_title(); ?></a></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";
}
get_footer();
?>
I've used bootstrap classes (row, col-6). Checked the size of categories array and used 2 variables - one as a counter and the other one to check if the column is first or second.
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
//get size of category
$catSize = sizeof($categories);
$j = 1;
$n = 1;
// Loop through categories
foreach ( $categories as $category ) {
if($n == 1){
echo '<div class="row">';
}
echo'<div class="col-6">';
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div></div>';
if($n == 1){
if($j == $catSize){
echo '<div class="col-6"></div>
</div>';
}
else{
$n = 2;
}
}
else{
echo '</div>';
$n =1;
}
$j++;
}
// Restore original Post Data
wp_reset_postdata();
} // End foreach
get_footer();
?>
Try this, I used a modulos operator "%" to group loops into 4, it will create new column every 4 loops. MAKE SURE YOU ADD CSS TO class new-column to arrange it like columns.
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'">;
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'">;
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";`enter code here`
}
get_footer();
?>

WordPress - Archive with showing first characters as headline

I used a shortcode to include my own customized archive on a WordPress page.
Currently it looks like this:
- Here is title number 1
- Title 2
What I know want to achieve is something like this:
H:
- Here is title number 1
T:
- Title 2
Does somebody know if a function for that already exists somewhere? My code is currently pretty straight forward:
//[show_archive]
function create_archive( $atts ){
$args = array(
'post_type' => 'post',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
_e("Es sind keine beiträge gefunden worden.");
}
/* Restore original Post Data */
wp_reset_postdata();
}//function
add_shortcode( 'show_archive', 'create_archive' );
Thanks!
while ($the_query->have_posts())
{
$the_query->the_post();
$current_character = ucfirst(substr(get_the_title(),0,1));
echo '<h2>'.$current_character.'</h2>';
if ($last_character != $current)
{
echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</li>';
$last_character = $current_character;
}
}

Include custom post type in Wordpress loop

Sorry, I think I read every post about this but cant get it to work.
I have a Wordpress custom post typ called "external" and I would like to show both the post from "external" aswell as my normal standard posts at the same time on my homepage.
This is my loop:
<?php
get_header();
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
How can I include the "external" posts in this code?
The theme then uses blocks to display content on the startpage, here is block8.php with a post query.
function block_eight_ajax_query($atts='') {
$args = array (
$is_ajax = 0;
if($atts==''){
$is_ajax=1;
$atts=$_GET;
if($atts['global_query']){
unset($atts['no_found_rows']);
unset($atts['suppress_filters']);
unset($atts['cache_results']);
unset($atts['update_post_term_cache']);
unset($atts['update_post_meta_cache']);
unset($atts['nopaging']);
}
}
$atts['is_ajax'] = $is_ajax;
$query = null;
$query = new WP_Query( $atts );
$html = '';
if ( $query->have_posts() ) {
ob_start();
$i = 1;
while ( $query->have_posts() ):
$query->the_post();
$atts['video'] = rd_field( 'abomb_post_video' );
$atts['extern'] = rd_field( 'extern' );
$atts['audio'] = rd_field( 'abomb_post_audio' );
$atts['gallery'] = rd_field( 'abomb_post_gallery' );
$atts['counter'] = $i;
block_grid_content($atts);
if ($atts['column'] == 'grid-12') { $divide = 1; }
else if ($atts['column'] == 'grid-3') { $divide = 4; }
else if ($atts['column'] == 'grid-4') { $divide = 3; }
else { $divide = 2; }
if ($i%$divide==0 && $divide!=1) {
echo '<div class="clear"></div>';
}
$i++;
endwhile;
if ($atts['nav'] == 'numbered') {
echo '<div class="grid-12">';
rd_pagination($query->max_num_pages);
echo '</div>';
}
wp_reset_postdata();
$html = ob_get_clean();
if($is_ajax==1){
echo $html;
exit();
}
return $html;
}
else{
if($is_ajax==1){
echo '-11';
exit();
}
return '';
}
}
Update: I added this code to functions:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'extern' ) );
return $query;
}
Now I can see the posts in for exampel the search result but I need the Query in Block8.php to fetch them aswell.
I Thanks!
To include a custom post type in the regular loop (ie. posts page) just add the following code to index.php before if ( have_posts() ) :
$args = array(
'post_type' => array('post', 'custom_post_type'),
'post_status' => 'publish',
);
$new_post_loop = new WP_Query( $args );
then modify the following two lines:
if ( have_posts() ) : change it to if ( $new_post_loop -> have_posts() ) :
and
while ( have_posts() ) : the_post(); to while ( $new_post_loop -> have_posts() ) : $new_post_loop -> the_post();
This solution avoids the problem of having the custom post type listed in the all posts screen on the backend, which #David.J's answer produces ;)
Add this to your functions.php file.
/**
* #param WP_Query $query
* #return WP_Query
*/
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'post', 'external' ) );
return $query;
}
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
Ref & duplicate: https://wordpress.stackexchange.com/questions/27104/how-to-display-regular-posts-custom-post-types-that-fall-under-a-category-usin
function add_my_custom_post_type( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ($query->is_main_query()) {
$query->set( 'post_type', array( 'post', 'some_custom_post_type_name' ) );
}
return $query;
}
}
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
This is the correct way to write the code so as not to break the dashboard queries.

Woocommerce alphabetic list of products

I would like to have an alphabet listing the products by ABC... list
Something like
A | B | C | D .... Z
And that all link to the products-list sorted by their first letter.
I would like it placed above the list where the sorting dropdown menu is (top of the woocommerce ).
Is it possible somehow with coding, or is there some plug-in for it?
I put this at the end of orderby.php:
<div id="alphabet">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$by_letter = array();
while( $query->have_posts() ) { $query->the_post();
global $post;
$letter = substr($post->post_name, 0, 1);
if ( ! isset($by_letter[$letter]) ) $by_letter[$letter] = array();
$by_letter[$letter][] = $post;
}
wp_reset_postdata();
?>
<?php
if ( ! empty( $by_letter ) ) {
ksort($by_letter); // order the array
// fill the array with letters have no posts
$by_letter = fill_by_letter_array( $by_letter );
display_letters_anchors( array_keys( $by_letter ) );
foreach( $by_letter as $letter => $posts ) {
?>
<div id="Productlist_<?php echo strtoupper($letter); ?>" class="Productlist_<?php echo strtoupper($letter); ?>_object productlists">
<?php
if ( ! empty($posts) ) {
foreach ( $posts as $post ) {
setup_postdata($post);
// just an example of post output
echo '<p>' . get_the_title() . '</p>';
}
} else {
echo '<p>' . __('No products.') . '</p>';
}
?>
</div>
<?php
}
wp_reset_postdata();
}
?>
</div>
A bit of css, and it Works great!

Categories