Anyone knows why the_content() doesn't show on my single.php page, while it shows up on index.php?
<div class="block">
<h1><?php the_title(); ?></h1>
<p class="date">
<?php the_date('d F Y'); ?>
</p>
<p>
<?php the_content(); ?>
</p>
</div>
in your single.php page nad index page the_*() functions are only designed to be used within the loop.
while(have_posts())
{
///the_content(); the_title(); the_post(); etc
}
//If I Used here then it would not work!
Related
I'm trying to make it so posts can be easily paginated in a custom theme, but I'm doing something wrong. I've included wp_link_pages(); in the while like which I believe is required, but maybe I have it in the wrong place or I'm missing something else?
<?php get_header(); ?>
<?php
while(have_posts()) {
the_post();
wp_link_pages();
?>
<div class="mainConent">
<div class="leftSidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('left_sidebar') ?>
</div>
</div>
<div class="recentBlogsWrapper">
<div class="blogWrapper">
<h2><?php the_title(); ?></h2>
<p><?php the_time('F j, Y') ?></p>
<?php if(has_post_thumbnail()) { ?>
<div class="card-image">
<img class="page-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card Image">
</div>
<?php } ?>
<div class="card-description">
<?php the_content(); ?>
<?php comments_template(); ?>
<?php } ?>
<div class="backarrowwrap">
<a class="backhomelink" href="<?php echo site_url(); ?>" <?php if(is_front_page()) echo 'class="active"' ?>>
<img class="backarrow" src="<?php echo get_template_directory_uri(); ?>/img/backarrow.png" alt="back arrow" />
Go Back Home
</a>
</div>
</div>
</div>
</div>
<div class="rightSidebar" id="sidebar">
<div class="sidebarTitleWrapper">
<?php dynamic_sidebar('right_sidebar') ?>
</div>
</div>
<?php get_footer(); ?>
Another odd thing that's happening is that the code block is showing the code on the page instead of hiding it like it should.
The wordpress editor
My blog post page which shows the code
Gutenburg was preventing me from writing the comment as code, even in a code box. Once I switched to the classic view the problem went away since I could write the comment in the html directly.
I have made a custom page in my wordpress theme. The page is working fine. But I am facing trouble using the_content(); function. I am using a few custom fields to display some content on the page and I want the content of the page, that is entered in the Editor, to be displayed on a specific place on the page. But Whatever I enter in the Editor, gets placed at the top of the page above all other content.
EDIT:
Here is the full code:
<?php get_header(); ?>
<div id="main-content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<div class="mobile-top">
<a href="<?php the_permalink(); ?>">
<p><?php the_title(); ?> price</p>
</a>
</div>
<?php comments_template(); ?>
</div>
</div> <!--main-content Ends-->
<div class="row clearfix">
<div class="content-wrap ninecol clearfix">
<div class="content">
<h1 class="title">Om oss</h1>
<hr>
<div class="entry-content">
<h4>Vilka är Unified Sweden?</h4>
<p>Unified Sweden är en webbyrå som ständigt strävar </p>
</div>
<div>
</div>
if I would want to make this as a template so that I can use it for other pages is the code below right?
<div class="content-wrap ninecol clearfix">
<div class="row clearfix">
<div class="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<div>
</div>
You should have a look at the Wordpress Template hierarchy and Page Templates.
You should create a file called page-<something-you-want>.php. Open the file and add a template name to it and add your content.
For example:
<?php
/*
* Template Name: A Static Page
*/
get_header(); ?>
<whatever content you want to add>
<?php get_footer(); ?>
Then go to Add new Page, and in the Page Attribute box, you'll see a dropdown called Template. You select the template called A Static Page (or otherwise defined above) and publish the page. That's all.
To get the content
To get the content in Wordpress, you need to The Loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content(); // displays whatever you wrote in the wordpress editor
endwhile; endif; //ends the loop
?>
Getting back to your case:
<div class="sevencol">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
</div>
I am using a nice jquery slideshow plugin I found and trying to get it to work into my Wordpress template. I have tried the code below in various formats but I can't seem to get it the way I want.
The first part is where the title and content of the post reads into the slider, using a specific category. I have 3 of these sections:
<div class="details_wrapper">
<div class="details">
<div class="detail">
<?php query_posts('cat_ID=7&showposts=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<h2 class="Lexia-Bold"><a href="<?php the_permalink() ?>">
<?php the_title() ?></a><?php the_excerpt(); ?></h2>
<?php endwhile; endif;
?>
</div><!-- /detail -->
<div class="detail">
<?php query_posts('cat_ID=8&showposts=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<h2 class="Lexia-Bold"><a href="<?php the_permalink() ?>">
<?php the_title() ?></a><?php the_excerpt(); ?></h2>
<?php endwhile; endif;
?>
</div><!-- /detail -->
<div class="detail">
<?php query_posts('cat_ID=9&showposts=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<h2 class="Lexia-Bold"><a href="<?php the_permalink() ?>">
<?php the_title() ?></a><?php the_excerpt(); ?></h2>
<?php endwhile; endif;
?>
</div><!-- /detail -->
</div><!-- /details -->
</div>
Now this actually works, but I just need it to post the title and excerpt from one of the posts from the category noted. I was reading that I may need to add the wp_reset_query(); line somewhere to destroy the previous loop's query, but I'm not sure.
Here's the second part of the code where the post's featured image is retrieved:
<div class="item item_1">
<?php query_posts('cat_ID=7&posts_per_page=1'); ?>
<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>
</div><!-- /item -->
<div class="item item_2">
<?php query_posts('cat_ID=8&posts_per_page=1'); ?>
<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>
</div><!-- /item -->
<div class="item item_3">
<?php query_posts('cat_ID=9&posts_per_page=1'); ?>
<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>
</div>
Any help would be greatly appreciated :) Here's an example.
Have you tried using...
query_posts('cat_ID=9&posts_per_page=1');
Or I have used get_post before as while to get certain amount of post like so...
<?php
global $post;
$myposts = get_posts('posts_per_page=1&numberposts=-1&category=1');
foreach($myposts as $post) :
?>
<h6><?php the_title(); ?></h6>
<?php setup_postdata($post);?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
</div>
If its just one post you want you wouldn't really need the foreach or while loop.
I used the method Tianbo84 suggested above both to query the posts AND the featured images from that post to finish the job :) Thanks Tianbo84! To my understanding, the get_posts and <?php endforeach; ?> lines were key... like opening a query and then closing it once the data was retrieved.
I have a very custom template, and it works great if there are 1 or 2 posts on the blog page. But as soon as a 3rd post is added, it alters the structure of the template... Literally moves a div inside of another and I cannot understand why. The code for the blog template is here, and a screenshot of the structure as it should be and another showing the misplaced div when a third post is there. Does this make any sense, any ideas?
<div class="post" id="post-<?php the_ID(); ?>"><!--start post-->
<h2><?php the_title(); ?></h2>
<div id="main_full" class=" clearfix"><!--start main-->
<div id="top_bar"><h3 class="gallery-title">news</h3></div>
<div id="blog_page"><!--start blog page-->
<div class="entry"><!--start entry-->
<?php the_content(); ?>
</div><!--end entry-->
</div><!--end blog page-->
</div><!--end main-->
<?php endwhile; endif; ?>
</div><!--end post-->
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
<?php comments_template(); ?>
Without seeing the beginning of your loop, I can't be 100% sure, but it looks like you need to have:
<?php endwhile; endif; ?>
</div><!--end post-->
be
</div><!--end post-->
<?php endwhile; endif; ?>
<?php endwhile; endif; ?>
</div><!--end post-->
Flip their places. If it doesn't help, please show us what while and what if are getting closed.