Wordpress the author function waterfall page - php

I am working on a custom blog roll for my website but cannot for the life of me figure out how to print out the author. Here is what I have so far:
<div id="blog_roll">
<?
$args = array('tag__not_in' => '5');
$posts = get_posts($args);
foreach($posts as $post) {
?>
<div class="waterfall">
<div id="waterfall_thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<div id="waterfall_title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
<div id="waterfall_author">
by:<?php the_author();?>on <?php the_time( get_option( 'date_format' ) ); ?>
</div>
<div id="waterfall_exc">
<?php echo apply_filters( 'the_content', $post->post_excerpt ); ?></div>
</div>
<? } ?>
Any help would be greatly appreciated!

The the_author() function works only inside The Loop™. To get an author's details outside the Loop, you can take the author's ID from the $post object and use it with the the_author_meta() function:
… by: <?php the_author_meta('display_name', $post->post_author); ?> …
See the function reference in the WordPress Codex for other available fields.
By the way, the_permalink also works only inside the Loop. You need to use
<?php echo get_permalink($post->ID); ?>

I use this on my wordpress blog
$user_url = get_the_author_meta('user_url', $post->post_author);
$user_name = get_the_author_meta('user_nicename', $post->post_author);
echo '' . ' by ' . $user_name . '';
As the other user mentions, the_author is only available inside the loop, so you need to use the_author_metadata (http://codex.wordpress.org/Function_Reference/the_author_meta)

Related

WordPress Custom Post query else data

I recently developed a Wordpress theme, this theme has little bit complex custom post type.
Below is my custom post query code. I would like kind of a else content whenever the request doesn't return any post. Its currently just blank.
I am not a PHP developer though, would someone give me a hand adding such a alternative content ?
Thank you.
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
//second query - posts
// Define the query
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
// output the term name in a heading tag
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<!-- row -->
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php } // end of check for query having posts
// use reset postdata to restore orginal query
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
<!--==== Top catogory section ====-->
<!-- front deal closed -->
</div>
elseif(!$query->have_posts()){//HTML here}
or
else{ //HTML here}
right after the end of check for your query , if you want to follow an OOP rule to make your code cleaner , move everything into a function inside function.php file and call the function there instead
<div class="front_page_deal_holder">
<?php
// get the currently queried taxonomy term, for use later in the template file
$term = get_queried_object();
$args = array(
'post_type' => 'products',
'product_cat' => $term->slug
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
echo'<h1>All Products about '. $term->name . '</h1>';
echo'<div class="row">';
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-3">
<a style="display:block;text-decoration:none" href="<?php the_permalink(); ?>">
<div class="single_front_sec">
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'post-image'); ?>
<img class="post_thumb_cus" src="<?php echo $featured_img_url ?>" alt="Product Image">
<h2 class="featured_product_heading">
<?php the_title(); ?>
</h2>
<span class="offer_note"><p><?php echo get_post_meta($post->ID, 'Offer Note', true); ?></p></span>
<div class="vew_d_button_holder">
<a class="view_d_button" href="<?php the_permalink(); ?>">View Details</a>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="my_Custom_pagination">
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
</div>
<?php }else{
// Add your code here for else part
}
wp_reset_postdata();
?>
<?php include_once('top-categories.php'); ?>
</div>

How to display posts categories as classes

Seems that I am just too stupid to achieve this. Seems so simple, yet…
I created a template and try to display a custom portfolio (registered by a custom post plugIn) This works fine with the following code:
<div id="container">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'boxes_scientists');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
/*
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
<?php the_category(', '); ?>
*/
<div class="some_base_class [categories of the post need to go here]">
<a class="element" href="<?php the_permalink(); ?>"></a>
<div class="portfolio-box">
<div class="portfolio-naming">
<h2 class="portfolio-title"><?php the_title(); ?></h2>
<h3 class="portfolio-attributes"><?php the_content(); ?></h3>
</div>
</div>
<?php the_post_thumbnail(); ?>
</div>
<?php endwhile;?>
but I can’t get to work the commented out code part and display the categories inside the class tag of my container element (for each post displayed in the loop).
I actually found also this concept:
https://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts
which seemed to be exactly what I need but sadly this didn’t work at all for me. (placed the function inside functions.php on my child theme and theme both without any result)
What I am misunderstanding here? Can someone show me the correct code I have to use? Would be so awesome. Thanks in advance!
EDIT
So this finally brings me on the right way:
<?php
[…]
$category = get_the_category();
$firstCategory = $category[0]->cat_name;?>
<div class="some_base_class <?php echo $firstCategory ?>">
I missed the “echo” thing inside my div. Now I will have to find the way to display all categories of my post instead of only the first.
EDIT 2:
<div id="boxes_section" class="main-content master-section-content nano_boxes no-detect no-padding">
<div class="container">
<div class="row">
<div class="col-md-12 normal-column start-animated-content az-fade-in" data-delay="300">
<div class="blank-divider" style="height: 30px;"></div>
<div id="portfolio-item-section" class="portfolio-output masonry-ly-portfolio classic-module no-pagination" data-cols="3">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'boxes_scientists');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="single-portfolio-item az-col-full-width-4 [NEED THE CLASSES HERE]">
<a class="classic-portfolio-box normal-type-prt" href="<?php the_permalink(); ?>">
<p class="site_leave"><i class="font-icon-forward"></i>You are going to leave this website</p>
</a>
<div class="portfolio-box">
<div class="portfolio-naming">
<h2 class="portfolio-title"><?php the_title(); ?></h2>
<h3 class="portfolio-attributes"><?php the_content(); ?></h3>
</div>
</div>
<?php the_post_thumbnail(); ?>
</div>
<?php endwhile;?>
</div>
</div>
</div>
</div>
</div>
I just write code category at functions.php like this:
function sps_category(){
$categories = get_the_category();
foreach ( $categories as $category ) {
echo '<a href="'.esc_url( get_category_link( $category->term_id ) ).'">
'.esc_html( $category->cat_name ).'
</a>';
}
}
and i call my functions at my page
<?php sps_category() ?>
but in other ways,
you can write your code in class like this:
$categories = get_the_category();
foreach ( $categories as $category ) {
echo '<div class=".esc_attr($category->cat_name)."><a href="'.esc_url( get_category_link( $category->term_id ) ).'">
'.esc_html( $category->cat_name ).'
</a><div>';
}
It can editable and dynamic if you want to show just 1 category.
And dont forget to use escape function if you put some php variabel/functions if you write in attribut html
for example use esc_attr(somecode) if it class/title/name/id attribute.

Print category of post Wordpress

I have try all the functions related to print categories that the codex provide, but i havent found any way that works for me.
Im trying to print the categories slug for put it inside a class.
I want to print the category of the actual post in div with the class proyect, so then i can use it to filter with isotope.
<!-- feature posts -->
<div id="container">
<?php $the_query = new WP_Query('showposts=5&orderby=post_date&order=DESC'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();
$id = get_the_ID(); ?>
<div class="proyect <?php wp_get_post_categories($id); ?>">
<div class="view view-tenth">
<a style="display:block;" href="<?php the_permalink(); ?>">
<article> <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail('', array("class" => "")); } ?></article>
</a>
<div class="mask">
<h2><?php echo substr(strip_tags(get_the_title()),0,35); ?></h2></a>
<p class="fecha-post"><?php the_time('F j, Y'); ?></p>
<?php echo substr(strip_tags(get_the_content()),0,100); ?>
<a class="info" href="<?php the_permalink(); ?>">Ver más...</a>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<!-- #feature post -->
From http://wordpress.org/support/topic/getting-category-slug-from-posts-in-the-loop:
<li class="<?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
You should be able to use:
$cats = wp_get_post_categories($post->ID);
This will be an array of the categories associated with this post. Then you can loop through them and do whatever you need.

Recent posts on homepage showing full post not excerpt

I have managed to get recent posts showing on my homepage which is working well. They only problem i have is that the full post and not the excerpt is showing.
<div id="home-news-container">
<?php query_posts('cat=#&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-post-container">
<div class="home-post-thumb">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail('large_wide'); ?>
</a>
</div>
<div class="home-post-copy">
<h4>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>
<h5>
<?php the_date(); ?>
</h5>
<?php echo the_excerpt(); ?>
<div class="home-news-readmore">
read more
</div>
</div>
</div> <!-- end home-post-container -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="home-news-readmore news-extra">
more news
</div>
</div> <!--- end home-post-container -->
I don't understand what the problem is to be honest. I created a new full width template for the homepage which i thought might be causing it but its not. Bit stumped to be honest. Any help would be greatly appreciated
Use wp_get_recent_posts to get recent post
Use substr to display some limeted character of post character
<?php
$args = array (
'numberposts' => 3,
'post_type' => 'your post type name'
);
// the query
$recent_posts = new wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
echo substr($recent["post_content"],0,100);
}
?>

How to include excerpt in ul from different pages with the same parent - Wordpress

I wish to get the title, the excerpt and some meta from three different pages that all have the same parent.
I do not wish to specify wich three pages to show but rather show the three that was last edited/published.
I aim to display the content as three blocks on a horizontal line.
As I was unable to get the code to work:
<ul id="">
<?php query_posts("posts_per_page=1&post_type=page&post_parent=4"); the_post(); ?>
<li>
<img src="<?php echo get_post_meta($post->ID, "image", true); ?>" alt="<?php the_title(); ?>" />
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</li>
</ul>
Thank you Dan, this still does not work though. The excrept of the first page does not show (the title shows and the meta also). Tried the code below with the same result except that the second time the content is displayed, the excerpt of the first page shows.
<?php get_header(); the_post(); ?>
<div id="main-content">
<?php
$categoriesCF = get_post_meta($post->ID, "categories", true);
$allCategories = explode(",", $categoriesCF);
foreach ($allCategories as $category) {
$pieces = explode("|", $category);
$link = get_permalink($pieces[1]);
echo "<div class='product-group group'>";
echo "<h3><a href='$link'>" . $pieces[0] . "</a></h3>";
query_posts("posts_per_page=-1&post_type=page&post_parent=$pieces[1]");
while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>" class="product-jump" title="<?php the_title(); ?>" data-large="<?php get_post_meta($post->ID, "image", true); ?>">
<?php echo "<img src='" . get_post_meta($post->ID, "image", true) . "' />"; ?>
<span class="product-title"><?php the_title(); ?></span>
<span class="product-title"><?php the_excerpt(); ?></span></a>
<?php endwhile; wp_reset_query();
echo "</div>";
};
?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>`
Change posts_per_page to 3 and use orderby=date&order=DESC to get the 3 most recent.
If you are displaying this on a page that already shows some other post/page/posts/pages, use get_posts instead of query_posts.
The WordPress codex has full documentation and examples for all of these things:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Function_Reference/get_posts

Categories