Display Post title PHP function doesn't work - php

Sup ,I have added 2 posts in BO , and trying to display title of posts with php , but it doesn't work , should i add any any additional function to functions php or not?
Instead of post title it display - "Home" - page title . where i wrong?
Here is some code
<div class="container">
<div class="row"id="blog">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-5"id="blog-post">
<div class="post-title"><?php echo get_the_title($post_id); ?></div>
<div class="post-txt"><?php echo get_excerpt(); ?></div>
</div>
<?php endwhile;?>
<?php endif; ?>
</div>
</div>

you don't need $post_id in your get_the_title() function because you are in a loop. In which file is this code ? Maybe you are not in a query.
Try this before your if statement and show me what you get :
<?php
$queried_object = get_queried_object();
var_dump($queried_object);
?>

I suggest this code.
if ( have_posts() ) {
// Load posts loop.
while ( have_posts() ) {
the_post();
?>
<div class="post-title"><?php echo get_the_title(); ?></div>
<?php
}
}

Related

Show excerpt at top of post

I have a js quiz template that is used for multiple quizzes, and I'd like each one to show that post's excerpt. I tried adding <?php the_excerpt(); ?> at the top of the template, but that shows just the excerpt with a "click to read more" button. Is there a way to call the excerpt for a post without "read more" and with content after it?
http://www.lawlessfrench.com/expressions/quiz/
<?php
/*
Single Post Template: Quiz
*/
get_header();
?>
<div class="row">
<div class="col-md-8 content-area" role="main">
<h1><?php the_title();?></h1>
<h2>French Quiz</h2>
<?php the_excerpt(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php endwhile; ?>
<script type = "text/javascript" src = "<?php echo get_template_directory_uri(); ?>/js/<?php echo get_the_content(); ?>"></script>
<div id="quiz">
<form id="quiz-form">
</form>
</div>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
Try using get_the_excerpt() instead:
$my_excerpt = get_the_excerpt();
echo $my_excerpt;
There is more on this at http://codex.wordpress.org/Function_Reference/get_the_excerpt
EDIT:
This, like all post functions, need to be called in the loop:
<?php
global $more; $more = -1; // suppress the more tag
if ( have_posts() ) {
while ( have_posts() ) {
$my_excerpt = get_the_excerpt();
echo $my_excerpt;
} // end while
} // end if
?>

WordPress category.php not sorting by category when using query_posts

I'm using the SuperSimple Theme for a wordpress blog and am trying to customize the category.php page. I want to have a big image for the latest post in each category on top of a smaller grid of all the older posts.
So far I have it working the way I want, except the top image (div id="post1") is just the most recent post overall instead of the latest post for the category. Here's one of the category pages: http://meanmargie.com/category/hospitality/
And here's the code I have:
<header class="header">
<h1 class="entry-title"><?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<div id="post1">
<?php query_posts('showposts=1'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>
<br>
<div id="post2">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; ?>
</div>
First of all, return the category ID of the category page you are on with get_query_var('cat')
$category = get_query_var('cat');
$cat_id = $category->cat_ID;
Now, return that id back into WP_Query, using 'cat'=> $cat_id,. Please note, showposts is depreciated, use posts_per_page instead
DO NOT USE query_posts. You should never ever use query_posts. It breaks and alters the main query and fails outright in most cases with pagination.
Here's the solution I ended up using:
<div id="post1">
<?php //new query to limit number of posts
$wp_query = new WP_Query();
$wp_query->query($query_string."&posts_per_page=1&paged=".$paged);
?>
<?php WP_Query; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div class="caption"><?php the_title(); ?></div><div class="excerpt"> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>

Combining wordpress admin page content with custom template?

Is there a way to combine content from a wordpress page's main textarea with the content in its custom template?
In this case I have a custom template that displays all posts from a single category, but I would also like to have a section that displays what is written in the wordpress admin Page area.
This is how I have the custom template set up to display relevant posts:
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Above this I would like to have the wordpress pages admin area content display, what a user would normally write into the textarea to display on the page, is this possible?
This is what I've come up with:
<?php get_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content() ;?>
<?php endwhile; ?>
<?php query_posts('category_name=baby-coupons'); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?> <span class="post-date">- <?php the_time('F j, Y'); ?></span></h2>
<div class="row">
<div class="one-third">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
<div class="two-third last">
<?php the_excerpt() ;?>
</div>
</div><!--/row-->
<hr>
<?php endwhile; ?>
Is this acceptable? It's working but I'm not sure how classy it is!

Wordpress: Check posts have comments allowed

I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks

Wordpress loop can't complete the query of posts

I have a post loop (index.php) which shows three post links as headers and above them there's a slideshow based on the slug of the post.
<div class="nuotraukos">
<?php if (have_posts()) : ?>
<?php $nuotraukos = new WP_Query('category_name=nuotraukos&showposts=3');
while ($nuotraukos->have_posts()) : $nuotraukos->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="post" id="post-<?php the_ID(); ?>">
<p class="postmetadata"><?php edit_post_link(__('Edit')); ?></p>
<div class="entry">
<?php
if ( function_exists( 'meteor_slideshow' ) ) {
$slug = basename(get_permalink());
meteor_slideshow('' . $slug. '');
}
the_content('<h2>' . get_the_title() . '</h2>');
?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
Meteor slideshow has a loop itself and I assume that it cancels the post loop after the first query. That's why it prints out only one title (but displays it three times) you can see it in http://studioglamour.co.uk.
The thing is i need three different links displayed below slideshows, but don't know how to fix this.
Try
$slug = basename(get_permalink($nuotraukos->post->ID));
...
the_content('<h2>' . get_the_title($nuotraukos->post->ID) . '</h2>');

Categories