Show logged in users comments with post title of post - wordpress

This thread How to display logged in users comments (only) on Wordpress
has gotten me most of the way to where I want to be... However, I am trying to change it slightly so that instead of the author's name it displays the post title and links back to the post they left the comment on.
This is the code I've come up with but it is not working.
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'order' => 'DESC', 'user_id' => $user_id );
$comments = get_comments($args); foreach($comments as $comment) : echo '<p>';
$post_id = $comment->comment_post_ID;
$member_name = get_post( $comment->comment_post_ID );
echo( '' . $member_name . '<br />' . $comment->comment_content);
echo '</p>';
endforeach;
}

Not where I can test this, but you’re grabbing the post into the array $member_name. So to display the post name, replace the output $member_name with $member_name->post_title.
Like:
echo( '' . $member_name->post_title . '<br />' . $comment->comment_content);
Probably could rename the array to something clearer like $post also.

Related

How do I display the taxonomy term alongside the post type post title?

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.

How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2

I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// 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>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.

Using WP_Query in functions.php only returning one result

I've written a function in my functions.php file that runs a new WP_Query class to fetch some child pages on my custom page template according to their meta key/value. It's kind of working but it's only returning one result - I know there are more as I had the query running properly on the specific page before I turned it into a function. It returned all the correct results, but as I may need this functionality in several pages I decided to turn it into a function.
Here is my function code…
function contact_profiles($args) {
global $post;
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output = '<div class="staff-member">'
.'' . get_the_post_thumbnail() . ''
.'<h2 class="name">' . get_the_title() . '</h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}
and here is how I am calling it in my custom page template…
$myarray = array('meta_key' => 'job_area', 'meta_value' => 'Online', 'post_type' => 'page',);
echo contact_profiles($myarray);
Am I doing something obvious that I shouldn't be? Is the global $post bit causing the issue as I'm not sure I should call it from the functions file.
Most probably you have your posts per page setting to 1 in the back end reading section. If no custom value is passed to a custom query, the default option get_option( 'posts_per_page' ) is used as value to the posts_per_page parameter.
You solution would be to explicitely set posts_per_page to a desired amount or to -1 to get all posts
EDIT
I have missed this before, your concatenation is wrong.
$output = '<div class="staff-member">'
should be
$output .= '<div class="staff-member">'
Here is an updated version of your code
function contact_profiles($args)
{
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output .= '<div class="staff-member">'
.'' . get_the_post_thumbnail() . ''
.'<h2 class="name">' . get_the_title() . '</h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}

Getting the post ID outside outside of the Wordpress loop

So I have a snippet of code that grabs the categories and their coinciding posts and lists them outside of the loop (Below). I've been trying to get the post to link to #post-[ID] instead of the permalink - but I keep failing. Can anyone help?
<ul id="sidebar">
<?php
foreach( get_categories('orderby=ID&order=desc') as $cat ) :
if( !$cat->parent ) {
echo '<li class="title"><h2>' . $cat->name . '</h2>';
echo '<ul>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
$id = $post->ID;
global $post;
if( $cat_posts ) :
foreach( $cat_posts as $menuPost ) :
echo '<li';
if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; }
echo '>';
echo '' . $menuPost->post_title . '';
echo '</li>';
endforeach;
endif;
echo '</ul></li>';
}
?>
The above code is outputting UL/LI tags like this:
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
Admittedly, I don't exactly understand what you mean by "linking to #post-[ID]", but going with the question title:
If you use get_permalink() when echoing the link, you will get the permalink - that's just what that function does.
Use get_the_ID() instead, if you want the post-ID returned, or the_ID() if you want it displayed (the_ID() is the same as echo get_the_ID()).
Edited from here:
If you're otherwise happy with the above code, changing
echo '' . $menuPost->post_title . '';
to
echo '' . $menuPost->post_title . '';
ought to do it.
However, I'd go about it like so:
echo '<ul>';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<li class="title"><h2>' . $category->name . '</h2><ul>';
// query posts of that category:
$query_args = array(
'cat' => $category->cat_ID,
'posts_per_page' => -1
);
$your_query = new WP_Query( $query_args );
// loop through them:
while ( $your_query->have_posts() ) : $your_query->the_post();
echo '<li><a href="#post-';
the_ID();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo '</ul></li>';
// Reset Post Data
wp_reset_postdata();
}
echo '</ul>';

Only displaying a posts selected taxonomy terms?

When using wp_get_post_terms() I can produce a list of taxonomy terms associated with a post. However, I only want to show the taxonomy terms that have been selected for that post. Using the aforementioned function and get_terms() will successfully find the taxonomy terms, but it will show all of the terms. Not only the ones that have been selected. In the $args array for the functions I've looked for a 'selected' filter, but I found none and when I tried it, it didn't work.
Am I trying to do something that can't be done? I'm sure it's something that is starring me right in the face. I just want to ask the pro's before I make major changes to the way I'm doing things.
wp_get_post_terms only returns terms that have been selected for that post, it doesn't return all taxonomy terms.
http://codex.wordpress.org/Function_Reference/wp_get_post_terms
<?php
$the_selected = $_GET['cat'];
$args = array( 'post_type' => 'portfolio_item', 'posts_per_page' => 11, 'orderby' => 'id', 'order' => 'DESC', 'themes_categories' => "$the_selected");
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
This works well for me. I simply send the taxonomy slug to browser, and itterate through them with the code above.
I send by this:
<li>Filter By:</li>
<?php
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '?cat=' . $category->slug.'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
}
?>
You can try out this code, it worked for me. I have a taxonomy named 'stores' and i wanted to display 2 selected taxonomy from it. so i used a include feature.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
<?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>
And if you want it without the term linking you can use this
<?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>

Resources