I've been stuck on this since last night and cant' figure out why the "Work" section of my wordpress doesn't show the the_title. Also, my "work" page is set to the "Work Page" template. Also, how would I go about having multiple posts on this page? Thanks!
http://www.listentotheway.com
<?php
/*
Template Name: Work Page
*/
get_header(); ?>
<p> This is the work.php file </p>
<?php
$args = array( 'post_type'=>'work' );
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><a href="<?php the_permalink() ;?>"<?php the_title(); ?></h3>
<?php the_field( 'description' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
<?php get_footer(); ?>
To summarise my suggestion so far:
<?php if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<h3><a href="<?php the_permalink() ;?>"<?php the_title(); ?></h3>
<?php // the_field( 'description' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
I have coded out the words the_field, there is no such native Wordpress function. Try my code an see what happens!
Related
I have this Page Template, developed by someone else a long long time ago, which shows all my Posts in a grid based on the category they're in.
Now, I want to have such a page for my Custom Post Type but I can't get it to work. I tried replacing the
<?php if (have_posts()) : while (have_posts()) : the_post();?>
with
<?php $args = array('post_type'=>array('shopitems')); query_posts($args); if (
have_posts() ) : while ( have_posts() ) : the_post();?>`
which seems to be the go-to answer everywhere I look. but for me, it turns out empty.
Below the original Page Template, I want to change this so it loads the 'shopitems' Custom Post Type.
Any help or nod in the right direction is greatly appreciated!
<?php get_header(); ?>
<div class="content-sidebar-wrap">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php $category_list= get_the_content(); ?>
<?php endwhile; endif; ?>
<div id="content" class="post-category page">
<?php $category_name=explode(',', $category_list);
$catIDs="";
foreach ($category_name as $cat_name) {
$catIDs.= get_cat_ID(trim($cat_name)).",";
}
$catIDs=trim($catIDs,",");
$i=0;
?>
<?php $thePosts= query_posts('cat='.$catIDs.'&post_status=publish&posts_per_page=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ) { ?>
<?php // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?>
<?php if($i%4==0) { ?>
<div class="clear"></div>
<?php } $i++; ?>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img alt="" src="<?php echo $image[0]; ?>" class="thumb-img">
<span class="thumb-title">
<?php
$second_name = get_field('second_name');
if( $second_name && !empty($second_name))
echo $second_name;
else
the_title();
?>
</span>
</a>
</div>
<?php } endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Just you need to modify your query like below.
<?php
$args = array(
'post_type'=>'shopitems'
);
$query = new WP_Query($args);
if ($query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
//do your code here
endwhile;
else:
// No posts found
endif;
?>`
I am using the following loop in my index.php to show the posts with a thumbnail:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
How can I add a permalink to the thumbnail? When the user clicks on it he should be directed to the post. At the moment, nothing is happening.
Thanks for your answers.
EDIT: I added the whole loop because the answer of Deepti produces an error. Maybe someone could help me with that.
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
So I'm trying to pull only the latest sticky post from WordPress and only 1. I am using 'showpost=1' but for some reason if I have two posts tagged as sticky both show up?
<h2>Breaking News</h2>
<?php
query_posts('posts_per_page=1');
if (have_posts()) {
while (have_posts()) : the_post();
if ( is_sticky() ) : ?>
<div class="small-12 medium-6 large-4 columns">
<div class="img-holder">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('sticky-hp-thumbnails');
}
?>
<?php if( get_field('sticky_sub_heading') ): ?>
<div class="tag">
<p>
<?php the_field('sticky_sub_heading'); ?>
</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<h3><a href"<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h3>
<?php if( get_field('sticky_date') ): ?>
<p class="sticky-date">
<?php the_field('sticky_date'); ?>
</p>
<?php endif; ?>
<p>
<?php the_field('sticky_summary'); ?>
</p>
Read More </div>
<?php endif;
endwhile;
}
wp_reset_query();
?>
Where am I going wrong in the above code?
Use posts_per_page instead of "showposts"
i.e.
query_posts( 'posts_per_page=1' );
Source:
https://codex.wordpress.org/Function_Reference/query_posts
Updated: Adding WP_QUERY code to fetch latest sticky post:
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post">
<h3><?php echo get_the_title(); ?></h3></a>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
The easiest solution to solve (workaround) your problem is to remove the while loop. This way you'll print only one.
Of course, this is sub-optimal since other pages may be fetched and unused; you could try using posts_per_page=1 or post_limits=1 to see if solve the issue.
I am building a custom theme from scratch and come across a slight problem that I need some help with.
So I have on my front-page a list of the 3 latest blog posts showing the 'title', 'Excerpt' & a 'more...' link which both the title and more link take you to single.php.
I am generating the post content in a file named 'content-post.php' which is the following:
<div class="clearfix">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
<header class="title">
<h3><?php the_title(); ?></h3>
</header>
<ul class="info">
<li><?php the_category(', '); ?> | <?php the_time('F Y'); ?></li>
<!--<li>Written By: <a href="<?php bloginfo('siteurl') ?>/about/"><?php the_author(); ?></li>-->
</ul>
<div class="excerpt">
<p><?php if(is_single()): ?>
<?php the_content(); ?>
<?php comments_template(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<a class="post-link" href="<?php the_permalink(); ?>">More...</a></p>
<?php endif; ?>
</div>
</div>
This builds the posts out to front-page.php just fine. I am having the problem when you go into the blog page which is using the same post content and the layout is the same. Is there a way I can specify how it shows on the front-page and how it appears on the blog page?
The post is being displayed on front-page.php like this:
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; endif; ?>
And on the blog page like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; else: ?>
<p>There are no posts to display or pages here</p>
<?php endif; ?>
If I understand your question, try this:
<?php if (is_page(page id)){your query}
Good luck! ;)
PS.. or on your way:
<?php if (is_page(page id)): ?>
your query
<?php endif; ?>
I am trying to alter the number of posts that will display on an events archive page using the Events Organiser plugin. Right now the following code displays only 8 events because that is what the Wordpress query is set to.
If I create a new query and set it to post type="event" it will grab all events and not just from the event category.
How can I alter the code below to be able to make sure it pulls all posts in that event category?
This is a template file so all event categories use this template. I am trying to find an easy solution to alter the number of posts since everything else is populating correctly.
<!-- Start Loop -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-md-4 clearfix">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
?>
</a>
<div class="event__details">
<a href="<?php the_permalink(); ?>" class="event__title">
<?php the_title(); ?>
</a>
<?php
if( eo_is_all_day() ){
$format = 'd F Y';
$microformat = 'Y-m-d';
}else{
$format = 'd F Y '.get_option('time_format');
$microformat = 'c';
}?>
<time itemprop="startDate" datetime="<?php eo_the_start($microformat); ?>"> <?php eo_the_start($format); ?></time>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<!-- End Loop -->
create a custom WP_Query check this
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => 8
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>