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.
Related
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>
I'm trying to loop only custom post types using the WP loop, but only shows the ones I give by ID.
This is my "normal" loop right now:
<?php $args = array(
'post_type' => 'referenties', 'posts_per_page' => 5, 'order' => 'DESC',
); ?>
<?php
$number = 0;
query_posts($args);
if(have_posts()):
?>
<!-- /Carousel script -->
<div class="container">
<div class="carousel-loop">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<?php while(have_posts()): the_post(); ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php endwhile; ?>
</ol>
<div class="controle-buttons">
<a class="carousel-control left" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-circle-left"></i></a>
<a class="carousel-control right" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-circle-right"></i></a>
</div>
<!-- Carousel items -->
<div class="carousel-inner">
<?php while(have_posts()): the_post(); ?>
<!-- Carousel nav -->
<div class="item">
<div class="col-sm-2">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(150,150)); // Declare pixel size you need inside the array ?>
</div>
<div class="col-sm-4">
<h4><?php the_title(); ?></h4>
<?php $bedrijf = get_field('naam_bedrijf'); ?>
<?php $feest = get_field('feest'); ?>
<?php $link = get_field('mylink'); ?>
<?php echo '<p>Bedrijfsnaam: ' . $bedrijf . '</p>'; ?>
<?php $post_object = get_field('mylink');
if( $post_object ): $post = $post_object; setup_postdata( $post ); ?>
<p>Feest type: <a style="color:#ff6600" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endif; ?>
</div><?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<div class="col-sm-4 col-sm-offset-1">
<h4>Opmerking</h4>
<p><?php echo custom_field_excerpt_longer(); ?></p>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php echo '<p>' . wp_review_show_total() . '</p>'; ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
But I only want to show post ID's: 2706, 2462, 2514, 2511 and 2505.
The loop is shown in a carousel, this works fine. But I just want the ID's to show and not all posts added.
Use something like this:
<?php
$args = array(
'post_type' => 'referenties',
'post__in' => array(2706, 2462, 2514, 2511, 2505),
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//post content output goes here
}
// Restore original Post Data
wp_reset_postdata();
} else {
// no posts found
}
The post__in () argument uses array with desired post ids to retrieve.
Don't use query_posts for custom queries. Too much that can go wrong.
Hope this helps :)
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)
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.
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);
}
?>