I've stuck in dev WP theme, please help me.
The problem is when I wrote code for displaying post in front-page.php, it display me the_title() and the_content() from page, not from post.
There is code:
<?php if(have_posts()): ?>
<?php while(have_posts()) : the_post(); ?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php __('No post found') ?></p>
<?php endif; ?>
It displays me post only if I go to post location e.g. localhost/2017/10/16/first-post/.
There is picture how it correctly shows but on another location.
http://prntscr.com/gy922y
Since you are displaying in a custom page, You'll need to declare and specify post_type as post
Here is a working example:
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php
}
}
?>
Related
I'm currently working on a WordPress site and I've created a custom post type called 'events' using the CPT UI plugin.
I want to display the events on my home page, so I've tried to create a loop in my homepage template in the theme files. I've been using this as a guide https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
but for the life of me, I can't get the PHP that is used in that link to work for me.
<?PHP
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$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>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
When I try to save that I get this error
syntax error, unexpected 'else' (T_ELSE)
I've been searching for an answer for this for a while and I can't find anything.
I'm pretty new to PHP, so sorry if I'm being incredibly stupid. Any help would be appreciated :)
You have not end while loop , place this code also <?php endwhile; ?>
<?php
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$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>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11.
However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.
Here is the loop on the custom page template
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else:
// no posts.
endif;
?>
And here is what I have on single.php
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
You should call the_post() in single.php before doing anything else.
Try this:
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Leave your other code alone. It looks like it should be working as expected.
Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.
In single.php use the following code to get the content and title.
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
use this in your custom page, i used wp_reset_postdata();
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else:
// no posts.
endif;
?>
And on single.php use this
<?php
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
?>
I have an if/else to call the template for a post type, and for some reason I cannot seem to figure out why the first portion is stuck in an infinite loop.
Here's the main page loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php if ( has_term('bands', 'work_family')):
get_template_part('content', 'single-bands');
else:
get_template_part('content', 'single-work');
endif; ?>
<?php endwhile; endif; ?>
I'm pretty sure the problem is in the single-bands template that is getting loaded, because if I comment out that line and stick some text in there it works just as it should. Here's that template:
<header class="fl-post-header">
<h1 class="fl-post-title" itemprop="headline">
<?php
// strip "Songs by" from titles for archive page
$title = get_the_title();
$prefix = 'Songs by ';
$stripper = $title;
if (substr($stripper, 0, strlen($prefix)) == $prefix) {
$stripper = substr($stripper, strlen($prefix));
}
?>
<?php echo $stripper; ?>
<?php edit_post_link( _x( 'Edit', 'Edit post link text.', 'fl-automator' ) ); ?>
TEMPLATE LOADED CORRECTLY IF YOU SEE THIS
</h1>
</header><!-- .fl-post-header -->
<div class="fl-content <?php FLTheme::content_class(); ?>">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article <?php post_class( 'work-post' ); ?> id="fl-post-<?php the_ID(); ?>" itemscope="itemscope">
<?php the_content(); ?>
<?php get_template_part('content', 'band-performances') ?>
<?php endwhile; endif; ?>
</div>
Nothing else loop related on the page and all the html works fine when I load the template directly instead of in that outer loop from the other page. I'm relatively certain the problem is in the header, because the rest of the template does not get looped, just that.
I have created two php files (recent_blog_post.php and single_blog_post.php) one for displaying post from wordpress site within same domain and another for displaying single post. I don’t know whether it is possible or not. The first one works fine but the problem is if i put the_permalink() in the title or read more link, the page redirects to main wordpress site. I don’t want to go back to main site, want linking to the custom php file to show each single post. Here is my code for recent_blog_post.php
<ul>
<?php
require($_SERVER['DOCUMENT_ROOT'] . '../wordpress/wp-load.php');
$args = array(
'posts_per_page' => 5
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<li>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>
</li>
</ul>
And single_blog_post.php
<?php require($_SERVER['DOCUMENT_ROOT'] . '../wordpress/wp-load.php');
query_posts('showposts=1');
if(have_posts()) : while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php edit_post_link('Edit', '<p>', '</p>'); ?>
<?php endwhile; ?>
<?php endif; ?>
you have to use the filter "the_permalink"
function subs_url ( $url ) {
return str_replace ( "www.mywebsite.com/", "www.mywebsite.com/single_blog_post.php/", $url );
}
add_filter('the_permalink', 'subs_url');
That filter will change your permalink from www.mywebsite.com/category/slug to www.mywebsite.com/single_blog_post.php/category/slug
Source: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_permalink
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; ?>