Show excerpt at top of post - php

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
?>

Related

Display Post title PHP function doesn't work

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
}
}

Trouble with query_posts inside of if statement

Having some trouble with a wordpress blog. I am trying to show posts with specific categories based on the section of the site the user came from. Everything is working except the 'query_posts' inside of the the if/else statement. I have the following php:
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer();
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
$text = 'test';
query_posts('cat=experienced-professionals');
// wp_reset_query();
} else {
$text = 'heyo';
query_posts('cat=college-students');
// wp_reset_query();
}
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div style="background:red;width:100%;height:200px;"></div>
<div id="container">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<p><?php echo $text; ?></p>
<?php print_r($came_from); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
I know my referer variable and if/else statement are working because the $text variable changes as it should based on the section of the site I get to the blog from. However no matter how I get to the blog, the page is showing all posts and ignoring the query_posts category inside the if/else statment. Can someone please help?
Nevermind figured this out...hope it helps someone else! Fixed it with this:
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer();
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
$text = 'test';
$queryCategory = 'experienced-professionals';
// wp_reset_query();
} else {
$text = 'heyo';
// query_posts('cat=college-students');
$queryCategory = 'college-students';
// wp_reset_query();
}
?>
<?php query_posts( array ( 'category_name' => $queryCategory ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div style="background:red;width:100%;height:200px;"></div>
<div id="container">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<p><?php echo $text; ?></p>
<p><?php echo $queryCategory; ?></p>
<?php print_r($came_from); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->
<?php wp_reset_query(); ?>
<?php get_footer(); ?>

Targeting specific Wordpress posts in a loop

I am trying to target individual posts so I can change the css (title tags, padding, etc) of specific posts. My Wordpress site currently generates the posts in a loop.
index.php code (brings in content.php which has 'post' code)
<div>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
<div class="clearfix"></div>
<div class="col-md-12">
</div>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</div>
content.php code (gets post-title, category, and sets post-thumbnail to background-image)
<?php
if (has_post_thumbnail()) {
$thumbnail_data = wp_get_attachment_image_src(get_post_thumbnail_id( get_the_ID()), 'my-fun-size' );
$thumbnail_url = $thumbnail_data[0];
}
?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<? php echo $thumbnail_url ?>')" <?php post_class('container-fluid'); ?> >
<div class="row">
<div class="col-md-12">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
functions.php (setting image size)
add_theme_support( 'post-thumbnails' );
add_image_size('my-fun-size', 'thumbnail');
The output is 'rows' 100% width with the title, category and background-image (feature-image). Stacked on top of each other. I want to be able to target the text and bg-image of different posts to make them each look different.
i think the best way to to this is by adding a custom field inside your posts, then, in your templates, you call that custom field this way:
get_post_meta($post->ID, 'name_of_your_custom_field', true);
this must be inside the loop.

How to don't show post having a specified tag in my homepage?

I am pretty new in WordPress development and I am trying to implement this custom theme that handle the so called featured posts: http://lnx.asper-eritrea.com/
As you can see in the posts area of the homepage I have the Articoli in evidenza sub area that contains my featured posts and under it the Ultimi Articoli subarea that contains the latest posts.
To implment this I use the posts tag and in the futured posts area I show the posts having the tag=featured condition.
So this is my code:
<section id="blog-posts">
<header class="header-sezione">
<h2>Articoli in evidenza</h2>
</header>
<?php query_posts('tag=featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="featured-posts">
<h1><?php the_title(); ?></h1>
<div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> // <?php the_category(', ') ?> // <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?>
</div>
<div class="featured-details"><?php the_excerpt()?>
<?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
<img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<header class="header-sezione">
<h2>Ultimi Articoli</h2>
</header>
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part('content', get_post_format());
endwhile;
else :
// If no content, include the "No posts found" template.
get_template_part('content', 'none');
endif;
?>
</section>
As you can see first I show the posts having a tag featured by the use of query-posts() function:
<?php query_posts('tag=featured');?>
Now my problem is that if a post have the featured tag I don't want that it is shown in the latest post area (at this time it is shown). So I tried to use this code:
<header class="header-sezione">
<h2>Ultimi Articoli NOT FEATURED</h2>
</header>
<?php query_posts('tag != featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="featured-posts">
<h1><?php the_title(); ?></h1>
<div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> // <?php the_category(', ') ?> // <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?>
</div>
<div class="featured-details"><?php the_excerpt()?>
<?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
<img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
But don't work and the featured post still shown in the homepage. As you can se I tried so specify that, to be shown, a post can't have the featured tag:
<?php query_posts('tag != featured');?>
Why don't work? What am I missing? Can you help me to fix this issue?
Tnx
To return posts that do not contain a particular tag, you should use pass the ID of the term into the tag__not_in argument.
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id ) );
is_front_page()
You should try <?php is_front_page(); ?> or vice versa.
http://codex.wordpress.org/Function_Reference/is_front_page
<?php if (is_front_page()) { ?>
<!-- Do something -->
<?php } else { ?>
<!-- Add else only if you need it! -->
<?php } ?>

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!

Categories