get category in posts, php - php

i try to write a simple code for php, which can show date,post autho and category but i can not show category. Where can i add it? this is code:
$posted_on = sprintf(
esc_html_x( 'Posted on111 %s', 'post date', 'sanse' ),
'' . $time_string . ''
);
$byline = sprintf(
esc_html_x( 'by %s', 'post author', 'sanse' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);

Inside have_posts loop you can use get_the_category() function to display posts' categories.
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '' . esc_html( $categories[0]->name ) . '';
}
https://developer.wordpress.org/reference/functions/get_the_category/

Related

Adding link in php Wordpress footer

I need some help in php. I need an hyperlink on the phrase inetio s. r.o.
The code is following:
if ( function_exists( 'get_the_privacy_policy_link' ) ) {
$footer_text = sprintf( _x( 'Copyright © %1$s %2$s. Všechna práva vyhrazena. %3$s', '1: Year, 2: Site Title with home URL, 3: Privacy Policy Link', '' ), esc_attr( date_i18n( __( 'Y', '' ) ) ), ''. esc_attr( get_bloginfo( 'inetio', 'display' ) ) . '', get_the_privacy_policy_link() ) . ' | ' . esc_html( $theme_data->get( 'inetio s. r. o.') ) . ' ' . esc_html__( 'vytvořilo inetio s. r. o.', 'solid-construction' ). ' <a target="_blank" href="'. esc_url( $theme_data->get( '' ) ) .'">'. esc_html( $theme_data->get( '' ) ) .'</a>';
}
Thanks for any help
boza
You can use it under WordPress built-in hook for this purpose.
add_action('wp_footer','dev_testing_func');
function dev_testing_func(){
/*Place your Code here*/
}

Show Last Updated Time instead of Published Time

I'm very new to Php. I want to show the Last Updated date instead of the Published time. Can someone help me? Thank you very much.
if ( ! function_exists( 'minimalistblogger_posted_on' ) ) :
function minimalistblogger_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
esc_html_x( 'Posted on %s', 'post date', 'minimalistblogger' ),
'' . $time_string . ''
);
$byline = sprintf(
esc_html_x( 'by %s', 'post author', 'minimalistblogger' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
endif;
Ah! I found it!
Just use the
get_the_modified_time('F d, Y');
$modifiedtime = get_the_modified_time('F d, Y'); $posted_on = sprintf(
esc_html_x( 'Last modified on %s', 'post date', 'minimalistblogger'
), '' .
$modifiedtime . '' );
thank you :)

Get images from custom taxonomy terms

Ok, I don't know if asking a new question is allowed for this. But I think its a bit different than my previous question. (if not, please tell me).
Im trying to get featured images from my custom taxonomy terms. Yesterday I got pretty far, but my client wants to add the names of the team members. So I'm kind of back to the drawing board on this one.
My theme is made up out of four different collapsible panels. With a Custom Post type and a loop.
So I made a custom taxonomy ons_team because I do not want to show the team members on every panel. So I checked the boxes on the ctp where I want the team members to be shown.
But now that the client wants to add the names of the team members I'm forced to use a different code. Yesterday I got it to work with this code.
<?php $terms = get_the_terms( $post->ID , 'ons_team' );
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="ons-team">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'ons_team',
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'ons_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
This only shows the team members on the panel in which I checked the boxes. But I cannot add the names of the team members to this code.
So I got it to work on the single page using this code:
<?php
// List of image links
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
?>
But if I use that code on the panel I WANT the information to show up. It shows it across all the panels, and not on the only one I want to show it on.
I tried using a combination of both but then every panel still shows the images and names.
<?php $terms = get_the_terms( $post->ID , 'ons_team' );
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'ons_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
Placing the code between the foreach or any other place kind of breaks the code. Is it possible that this doesn't work because I'm using $term / $terms alot?
Using it like this:
<?php $terms = get_the_terms( $post->ID , 'ons_team' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'ons_team' );
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
if( is_wp_error( $term_link ) )
continue;
}
?>
Kind of works, but then it show the team member like 10 times in a row...
Any help is much appreciated!
Solved it!
Here is the correct code if people need it:
<div class="teamleden over-ons-ul">
<div class="center-align">
<div class="row">
<?php
$terms = get_the_terms( $post->ID , 'ons_team' );
if ( $terms != null ){
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
foreach( $terms as $term ) {
unset($term);
} } ?>
</div>
</div>
</div>

WooCommerce 'if ( is_cart() )' in functions.php not working?

I've added the code below within my functions.php so I can change the WooCommerce standard 'Add to cart notice'.
The notice does change but the if ( is_cart() ) does not seem to work. It outputs FALSE on the cart page.
I must have overseen something..?
add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );
function yw_add_to_cart_message($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
if ( is_cart() ) {
$cart_link = '<div class="uk-width-medium-1-5 uk-text-right"><i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '</div>';
} else {
$cart_link = '<div class="uk-width-medium-1-5 uk-text-right"><i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '</div>';
}
$added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div>' . $cart_link . '</div>';
$message = sprintf( '%s', $added_text );
return $message;
}
You have to use get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' instead of is_cart():
add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );
function yw_add_to_cart_message($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
$cart_link = '<i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '';
} else {
$cart_link = '<i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '';
}
$added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div><div class="uk-width-medium-1-5 uk-text-right">' . $cart_link . '</div></div>';
$message = sprintf( '%s', $added_text );
return $message;
}
Related threads: Here and Here

WordPress, A page of thumbnails and titles for a grid of taxonomies for a custom post type

I am able to list the taxonomy children:
<?php
$taxonomy = 'store_name';
$tax_terms = get_terms($taxonomy);
?>
<ul class="split-list2">
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
</ul> ?>
And I found a plugin to make thumbnails for categories/taxonomies:
https://wordpress.org/plugins/taxonomy-images/
But I can't figure out how to add the code to include the thumbnails to the taxonomy category grids.
Does anyone know how to do this
Read the plugin doc perfactly it gives what you want
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'after' => '</div>',
'after_image' => '</span>',
'before' => '<div class="my-custom-class-name">',
'before_image' => '<span>',
'image_size' => 'detail',
'post_id' => 1234,
'taxonomy' => 'post_tag',
) );
Second parameter in apply_filters( 'taxonomy-images-get-terms', '' ); is array as show above
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach( (array) $terms as $term ) {
print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>';
}
print '</ul>';
}

Categories