Get the_category outside of loop - wordpress

I'm working on a site that has custom author pages and on the author pages it has a recent author posts widget which displays other posts by that author. This site has multiple authors so it is different for every post and I haven't found a plugin that does this. I'm trying to display the post thumbnail, title, and category name.
I'm using a function that is displaying the title and the thumbnail, but it doesn't have the category . I tried to add the category in with: the_category(' ','multiple',$authors_post->ID) unfortunately it displays all of the categories in the first li. Instead of for each post.
Here is what I'm working with:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,
'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' )
. '<p>' . the_category(' ','multiple',$authors_post->ID)
. '</p> <a href="' . get_permalink( $authors_post->ID )
. '">' . apply_filters( 'the_title', $authors_post->post_title,
$authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';
return $output;
}
Any help would be greatly appreciated,
Thanks!

Please try this code,
<?php
$cat=1;
$yourcat = get_category($cat);
if ($yourcat)
{
echo '<h2>' . $yourcat->name . '</h2>';
}
?>
To get category name,

try some thing like
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$category = get_the_category($authors_post->ID);
foreach($category as $c){
$cat=$c->cat_name.' '.$cat;
}
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' ) . '<p>' . $cat . '</p> ' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul>';
return $output;
}
hope this work for you..

Related

WP_Query returns only one post

My loop returns only one post instead all. I going through it 4th time and i do not see any reason why it act like that. Thank you for all your help.
localStorage has been cleaned, cache also - and yeah i have more then one post published in this category :)
function rtf_custom_grid( ) {
$args = array(
'post_type' => 'apartamenty',
'post_per_page' => -1,
'nopaging' => true,
'order' => 'date',
'orderby' => 'DESC'
);
$rtf_query = new WP_Query ( $args );
while($rtf_query->have_posts() ) : $rtf_query->the_post();
$cena_1 = get_field('cena_1');
$cena_2 = get_field('cena_2');
$short_dec = get_field('short_desc');
$output = '<div class="single-apartament">';
if ( has_post_thumbnail() ) :
$output .= '<div class="rtf-apartament-thumbnail">';
$output .= '' . get_the_post_thumbnail(get_the_id(), 'large') . '</div>';
endif;
$output .= '<div class="rtf-apartament-content">';
$output .= '' . get_the_title() . '';
$output .= '<div class="rtf-apartament-excerpt">' . $short_dec . '</div>';
$output .= '<div class="rtf-apartament-prize">';
$output .= '<span>' . $cena_1 . ' / ' . $cena_2 . '</span></div>';
$output .= 'Zobacz</div></div></div>';
endwhile;
wp_reset_query();
return $output;
}
add_shortcode('apartamenty', 'rtf_custom_grid');
$output = ' <div class="single-apartament"> ';
the above line is overwriting the previous content in the variable. So its showing only the last post.
Solution :
$output.= '<div class="single-apartament">';
This will append the html code into the variable instead of overwriting it.
Hope it works for you

WordPress - How to create a shortcode that outputs a CPT

There are a handful of things I'd like to do with this shortcode I'm working on. My knowledge about this isn't the best, but I'm trying to learn.
/**
* Recent Project Shortcode
*/
function project_query() {
$args = array(
'posts_per_page' => 1,
'post_type' => 'projects',
'order' => 'ASC',
);
$projects_query = new WP_Query( $args );
if ( $projects_query->have_posts() ) :
// var_dump(the_post_thumbnail_url("full")); exit;
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
while ( $projects_query->have_posts() ) :
$projects_query->the_post();
// Do stuff with each post here
$title = get_the_title();
$link = get_the_permalink();
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>';
endwhile;
$html_out .= '</article>';
else : // No results
echo "Nothing to show";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'show_project', 'project_query' );
There are a few issues here. What does work is that on the front-end it pulls the project name, which is sweet, and the button links to the appropriate page.
Here's how I'd like the shortcode to look when using it: [show_projects posts_per_page="3" order="ASC"] I want to make it "easy" for the user to modify the $args. The second thing that isn't working is the background url I'm trying to do. Right now in the front end everything is outputting except that background url.
Hey Darren the problem is that you create the $featured_img variable after you use it. It should be in while loop.
Please try this code
/**
* Recent Project Shortcode
**/
function project_query($atts) {
$atts = shortcode_atts(
array(
'example_attribute' => 'example_value',
),
$atts, 'example'
);
//if you want to use the attribute you should use $atts['example_attribute'] for example
$args = array(
'posts_per_page' => 1,
'post_type' => 'projects',
'order' => 'ASC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) :
$post = array_shift( $posts );
$title = get_the_title($post->ID);
$link = get_the_permalink($post->ID);
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
// Do stuff with each post here
$html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>';
$html_out .= '</article>';
else : // No results
echo "Nothing to show";
endif;
return $html_out;
}
add_shortcode( 'show_project', 'project_query' );

How to display thumbnails in Wordpress for related posts by author?

I have a related posts by author function using this code:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
$output = '<div class="block">';
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output; }
To output I use the following code inside the loop of my single.php file:
<?php echo get_related_author_posts(); ?>
Currently the function it displays only post titles as links.
How should look this wp code in order to display thumbnails for this related posts by author function?
Try:
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID);
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
check get_the_post_thumbnail for more information like image size and attributes like extra classes.
Let me know if you need more help.

How to modify a theme-bundled widget in child theme?

I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here
theme folder/example theme/lib/activity/plugin.php.
Here is the exact code:
private function get_latest_posts( $post_count ) {
// Get the latest posts
$latest_posts = get_posts(
array(
'numberposts' => $post_count,
'order' => 'desc',
'orderby' => 'date'
)
);
// Create the markup for the listing
$html = '<div class="tab-pane" id="recent">';
$html .= '<ul class="latest-posts">';
if( count( $latest_posts ) > 0 ) {
foreach( $latest_posts as $post ) {
$html .= '<li class="clearfix">';
// Add the small featured image
if( has_post_thumbnail( $post->ID ) ) {
$html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
$html .= '</a>';
} // end if
$html .='<div class="latest-meta">';
// Add the title
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_title( $post->ID );
$html .= '</a>';
// Add date posted
// If there's no title, then we need to turn the date into the link
if( strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
} // end if
$html .= '<span class="latest-date">';
$html .= get_the_time( get_option( 'date_format' ), $post->ID );
$html .= '</span>';
// Close the anchor
if(strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '</a>';
} // end if
$html .='</div>';
$html .= '</li>';
} // end foreach
} else {
$html .= '<li>';
$html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
$html .= '</li>';
} // end if/else
$html .= '</ul>';
$html .= '</div>';
return $html;
} // end get_latest_posts
Now please tell me how to remove the nofollow tag from this code using the child theme?
Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:
Filtering the output of WordPress widgets
The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:
function theme_dynamic_sidebar($index = 1) {
// capture output from the widgets
ob_start();
$result = dynamic_sidebar($index);
$out = ob_get_clean();
// do regex search/replace on $out
echo $out;
return $result;
}
Seems that you are out of luck.
That's a private function and no filter hooks are offered by the theme author.
You may try to override the include('path/to/plugin.php'); and include your own modified version.

wp_list_categories - with custom taxonomy

I'm back struggling with some more coding :(
I have written a custom PHP page to loop through a custom post type. Now what I want is to list the custom posts categories, and have each category link to the same page but pass the category to the query_posts() function.
My code is currently
<div id="left" style="float:left;width:200px">
<b>Categories</b><br><br>
<?php wp_list_categories( $args ); ?>
</div> <!-- end of left container -->
<div id="middle" style="float:left;width:700px">
<?php
$args = array(
'post_type'=> 'e-books',
'showposts' => '-1',
'eCats' => $cat,
'order' => 'ASC'
);
query_posts( $args );
where
$cat = $_GET['cat']
What I want is each link from the wp_list_categories() to be something like.
http://example.com/products.php?cat=fiction
Is this possible? I can't get my head around the Walker class :( I've got this far
class MyWalker extends Walker_Category {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$link = '<a href="products.php?cat="';
$link .= $cat_name;
$link .= '>';
$link .= $cat_name . '</a>';
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
$class = 'cat-item cat-item-' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
}
}
but the links aren't completing - they are showing as
?cat=
For listing category with the custom taxonomy :
You can you following code:
$catnames = get_terms( 'custom taxonomy name');
Then use foreach loop to disply the category.

Resources