On my Wordpress website, I have the following code:
if ( is_page( 8 ) ) {
include_once('somecustompage.php');
}
And in my somecustompage.php, I use a custom WP_Query:
// WP_Query arguments
$args = array (
'post_type' => 'farewell',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '10',
'sticky' => false
);
// The Query
$current_farewell_query = new WP_Query( $args );
// The Loop
if ( $current_farewell_query->have_posts() ) {
$current_farewells = '<table><tr><th>NAME</th><th>LOCATION</th><th>FAREWELL</th></tr>';
while ( $current_farewell_query->have_posts() ) {
$farewell_name = ( get_field('name') ? get_field('name') : '' );
$chapels = wp_get_post_terms( get_the_ID(), 'chapels', array('fields' => 'name') );
$current_farewell_query->the_post();
$current_farewells .= '<tr>';
$current_farewells .= '<td>' . $farewell_name . '</td>';
$current_farewells .= '<td></td>';
$current_farewells .= '<td></td>';
$current_farewells .= '</tr>';
}
$current_farewells .= '</table>';
} else {
$current_farewells = 'No Current Farewells.';
}
// Restore original Post Data
wp_reset_postdata();
However, when I try to use the post IDs within this custom loop, both get_the_ID() and $post->ID return 8, which is the main page post id. I need to use the new post IDs that are returned in the new query.
Anybody knows how to resolve this?
I found where the problem was. Everything should've been called after this line: $current_farewell_query->the_post(); for the query to work as expected.
Related
I would like to display the taxonomy term of the post type post besides the post type post title, separated by the "in" text string.
There are two issues:
only "array" is displayed instead of the term
I don't know how to code the "in" text string correctly, between the term and the title (they should be in the same row)
Here is the (wrong) code in question:
$output .= '<div>' . get_the_title() . '</div>'; in $output .= '<div>' . wp_get_post_terms( $post_id, $taxonomy = 'itemscategories') . '</div>';
Embedded in this shortcode:
function myprefix_custom_grid_shortcode( $atts ) {
// Parse your shortcode settings with it's defaults
$atts = shortcode_atts( array(
'posts_per_page' => '-1',
'term' => ''
), $atts, 'myprefix_custom_grid' );
$user_id = userpro_get_view_user( get_query_var('up_username') );
// Extract shortcode atributes
extract( $atts );
// Define output var
$output = '';
// Define query
$query_args = array(
'author'=> $user_id,
'post_type' => 'items', // Change this to the type of post you want to show
'posts_per_page' => $posts_per_page,
);
// Query by term if defined
if ( $term ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'ID',
'terms' => $term,
),
);
}
// Query posts
$custom_query = new WP_Query( $query_args );
// Add content if we found posts via our query
if ( $custom_query->have_posts() ) {
// Open div wrapper around loop
$output .= '<div>';
// Loop through posts
while ( $custom_query->have_posts() ) {
// Sets up post data so you can use functions like get_the_title(), get_permalink(), etc
$custom_query->the_post();
// This is the output for your entry so what you want to do for each post.
$output .= '<div>' . get_the_title() . '</div>'; in $output .= '<div>' . wp_get_post_terms( $post_id, $taxonomy = 'itemscategories') . '</div>';
}
// Close div wrapper around loop
$output .= '</div>';
// Restore data
wp_reset_postdata();
}
// Return your shortcode output
return $output;
}
add_shortcode( 'myprefix_custom_grid', 'myprefix_custom_grid_shortcode' );
The wp_get_post_terms function by default returns an array so if you want to display the first term name you can do something like this:
// This is the output for your entry so what you want to do for each post.
$terms = wp_get_post_terms( $post_id, 'itemscategories' );
if ( $terms && ! is_wp_error( $terms ) ) {
$output .= '<div>' . esc_html( get_the_title() ) . ' ' . esc_html__( 'Posted in:', 'text_domain' ) . ' ' . esc_html( $terms[0]->name ) . '</div>';
}
Or you can instead use the alternative function - https://developer.wordpress.org/reference/functions/get_the_term_list/
// This is the output for your entry so what you want to do for each post.
$output .= '<div>' . esc_html( get_the_title() ) . get_the_term_list( $post_id, 'itemscategories', 'Posted in: ', ', ' ) . '</div>';
This function will display a list of all the terms belonging to the post with links to the term archives. If you want to remove the archive links you can wrap get_the_terms_list inside the strip_tags function.
Completely confused with this one.
If I add the nonpaging => true to the query, I get all the results. However, I wish to only have 4 displayed in this particular loop. If I use the post_per_page => 4, I only get one.
Worse, if I add in orderby => 'rand' I will get some number between 1-5.
What the hell is going on?!?!?
// WP_Query arguments
$args = array (
'post_type' => array( 'bottin' ),
'posts_per_page' => 4,
);
// The Query
$bottin = new WP_Query( $args );
// The Loop
if ( $bottin->have_posts() ) {
while ( $bottin->have_posts() ) {
$bottin->the_post();
$voir_plus = get_field('voir_plus');
if ( !empty($voir_plus) ) {
// Vars
$link = get_the_permalink();
$image_url = get_the_post_thumbnail_url();
$name = get_the_title();
$posttags = get_the_tags();
echo '<a href="' . $link . '"><div class="elementor-row"><div class="elementor-col-50"><img src="' . $image_url . '" class="img-responsive" width="300"></div><div class="elementor-col-50 intervenant-info"><h6>' . $name . '</h6><p>';
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
echo '</p></div></div></a>';
}
}
} else {
// no posts found
}
I think you need this, "paged" and you might need to check this out https://codex.wordpress.org/Pagination
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'bottin'
'posts_per_page' => 4,
'paged' => $paged
);
$bottin = new WP_Query( $args );
?>
can you please tell me the coding to include recently viewed products in WooCommerce
Try adding below snippet where you want to display
<div>
<?php
echo do_shortcode("[woocommerce_recently_viewed_products per_page='5']");
?>
</div>
Add below code to functions.php of your active theme
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {
// Get shortcode parameters
extract(shortcode_atts(array(
"per_page" => '5'
), $atts));
// Get WooCommerce Global
global $woocommerce;
// Get recently viewed product cookies data
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
// If no data, quit
if ( empty( $viewed_products ) )
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );
// Create the object
ob_start();
// Get products per page
if( !isset( $per_page ) ? $number = 5 : $number = $per_page )
// Create query arguments array
$query_args = array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'post__in' => $viewed_products,
'orderby' => 'rand'
);
// Add meta_query to query args
$query_args['meta_query'] = array();
// Check products stock status
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
// Create a new query
$r = new WP_Query($query_args);
// If query return results
if ( $r->have_posts() ) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ( $r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// Get clean object
$content .= ob_get_clean();
// Return whole content
return $content;
}
// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products", "rc_woocommerce_recently_viewed_products");
I have two different post types where I want to have a relationship. Authors can list one business(post type - "listings") and many special offers (post type - "special_offers"). In each single special offer page, I need to display which business offers that specific offer. Currently I'm using this code, and it outputs the Business name correctly. I need the business logo to be there as well (featured image)
function get_author_business() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'listings', 'post__not_in' => array( $post->ID ), 'posts_per_page' => -1 ) );
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}
This might help (i'm assuming you are trying to get the image tag inside your output html)
function get_author_business() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'listings', 'post__not_in' => array( $post->ID ), 'posts_per_page' => -1 ) );
foreach ( $authors_posts as $authors_post ) {
$business_img = wp_get_attachment_image_src( get_post_thumbnail_id( $authors_post->ID ), 'thumbnail' );
$output .= '<img src="'.$business_img[0].'">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}
You can use the following code :
<?php
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$attachment_url=wp_get_attachment_image_src($post_thumbnail_id,'full');
/* full -- is image size */
?>
<img src="<?php echo $attachment_url; ?>">
hope that will work for you.
i make two list theme
1.the posts have featured image and title, but empty content. i want get this list.
2.and other posts with content list.
I don't have any idea, help me please
Try to use below code:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
$content = get_the_content();
if($content == '') {
array_push( $blank_posts, $post);
}
endwhile;
endif;
/* print all blank content posts */
//echo "<pre>"; print_r($blank_posts);
/* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "ID= ". $pst->ID . ', '. "Title= ". $pst->post_title .'<hr />';
}
}