Buddypress Show a members most recent post in members loop - wordpress

In my list of members, I show the standard fields of avatar, username, profile data, etc - this all works fine.
I'd like to also show the 'most recent post' from that user too.
Is there a snippet I can use to grab the most recent post from that author? Somehow identifying which author it is and their most recent post?
Thanks,
Ian

Somebody provided me with a great fix for this:
<?php
$user_id = bp_get_member_user_id();
$query = new WP_Query( 'author=' . $user_id );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
//display title
the_title();
//display content
the_content(); ?>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('featuredimage'); ?>" alt="" />
</a>
<?php }
}
//reset for next member
wp_reset_postdata();
?>

Related

Blog posts won't open on my custom WordPress theme

This could be a dumb question, but I just can't figure it out. I'm working on a custom WordPress theme I created by converting a html website. But when I use WP_Query, it doesn't load my blog posts. Yes, it displays a list of blog posts as it should with the loop, but when I click on each title or the 'read more' link of a post, it only changes the url in the address bar to the link of the post and then simply refreshes the hope page. It doesn't open I'm really tired. Here's the query loop in my index.php
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
//define args for query
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '30',
);
//The main query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt(); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div><!-- blog-main-content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Please what do I do
Based on your comment above, you are missing a template file for your posts page. In your current configuration, the posts loop will be displayed on all pages of your website since you only have an index.php file. You should create a single.php template to display your single blog posts.
Here is the WordPress template hierarchy for reference: https://developer.wordpress.org/themes/basics/template-hierarchy/

Switch Wordpress database outside Wordpress

I have two Wordpress blogs on the same domain/server - one regular posts the other videos - plus other non-Wordpress pages which have a box with the latest blog posts using this code:
<ul>
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');
$args = array(
'cat' => 5, // Only source posts from a specific category
'posts_per_page' => 1 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<li>
<span class="post_title"><?php the_title(); ?></span>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ) { ?>
<span class="post_thumbnail"><?php the_post_thumbnail(); ?></span>
<?php } ?>
</a>
<span class="post_excerpt"><?php the_excerpt(); ?></span>
</li>
<? }
}
wp_reset_postdata();
?>
</ul>
All works as I want it. However we now want a second box under the first on the same pages with posts from the other blog (video). As wp-load.php has already been included for the first blog box it gets posts from the wrong blog.
How can I swap the database connection to the settings for the other (video) blog?
I have searched for solutions and found
$wpdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
I am lost as to how to use this and make it work.
I have considered merging them in to one blog but that seems a bit excessive for what would seem a simple problem so would rather not do that.
Thanks

Displaying category images on homepage in WordPress

I am attemtping - and failing - to display the category image (thumbnail) on my WordPress homepage. Here is my code:-
<?php $paged = get_query_var('paged') ? get_query_var('paged') : 1;
$cat_id = get_cat_ID( single_cat_title(null, false) );
query_posts( "cat=$cat_id&paged=$paged&posts_per_page=7" );
$i=1;
if(have_posts()): while(have_posts()): the_post();
?>
<div class="latest_cat_post col-md-3">
<div class="news_thumb">
<a href="<?php the_permalink();?>">
<?php if ( has_category_thumbnail() ) {
the_category_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-featured.png" alt="<?php the_title(); ?>" />
<?php } ?>
</a>
<div class="cat_name"><?php the_category('•'); ?></div>
</div>
However, this fails, citing "Fatal error: Call to undefined function has_category_thumbnail()", so I assume this function does not exist, but strangely, has_post_thumbnail() does, so I assumed this would be the case for categories too - perhaps I am wrong.
Can anyone give me 2 minutes of their day to fix this problem? I don't want to use a plugin, when it should be relatively straightforward to do. Thanks!
In WordPress you don't have category thumbnails, but you can try use a plugin. Try this one.
Assuming you will have the category ID, you have to do something like this:
$images = get_option('taxonomy_image_plugin');
$cat_id = $category->term_taxonomy_id;
if( array_key_exists( $cat_id, $images ) ) {
echo wp_get_attachment_image( $images[$cat_id] );
}
For more information you can visit this related question/answer.

WordPress - Display Custom Field on Post - Pull Post Title and URL

Okay, I have the code pulling the custom fields URL and title of the url. Now I can't seem to get it to show the second featured blog. Here is the working code.
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
Here is a screenshot showing my Custom Fields if it helps at all, and the results they are showing on the site. screenshot
Example 1: You are using the actual word 'TITLE' for the link use <?php the_title() ?> instead
Example 2: You are not building link at all. Your href attribute is empty. Cut echo $featured_blog1 and paste it to href attribute to end up like so:
Example 3: Same as 2
Also you can delete dose instructions or put them inside <?php ?> code so they are not visible to viewers.
Hope this helps.
If you need more info just ask. ;)

wordpress featured posts

i'm trying to create a portfolio website using wordpress,
each post has view costum fields, one of which is called type - with the value of "featured" or "not-featured"
now when user clicks on the post title - they go the the single.php to see the entire post, here i would love to display all featured thumbnails
i tried this
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'type', true) == "featured") {; ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img src="<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a></h2>
<?php }; ?>
<div class="entry-content">
</div><!– .entry-content –>
<?php endwhile; ?>
(THIS CODE IS SIMILAR TO THE CODE I USE AT INDEX.PHP AND THERE IT DOES WORK, HERE AT SINGLE.PHP IT DOES NOT WORK)
but this does not display all the thumbnails (only the current posts' thumbnail (is it's a feature post))
this is my first attempt of trying to create a theme from blank so i'm not sure what the error could be
thanks for your help
The code in your question only loops through the posts returned by the query made for the current view, in the case of a single post view that is one post. You want to perform a new query to retrieve all the posts that have the required meta value:
<?php
query_posts(array("meta_key" => "type", "meta_value" => "featured"));
if (have_posts()) : while (have_posts()) : the_post();
?>
<!-- Display thumbnails -->
<?php endwhile; endif; ?>

Resources