Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Displaying one latest post with custom read more?

         

Sjtr

9:25 am on Nov 10, 2014 (gmt 0)

10+ Year Member



Hello there,

I'm wondering how to display the latest post (just the latest one) with the title, then excerpt and if possible with the featured image (being able to customize width and height). Also, the read more so I can customize the read more text. I have currently found pieces of code that only do partially of what I want. Not all this together. That's so frustrating for me because I don't know how to 'wire' these codes together and make it work.

Does anyone know the code for this?

Thanks in advance, regards

[edited by: lorax at 3:20 pm (utc) on Nov 10, 2014]

Planet13

5:13 pm on Nov 11, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this going to go straight into your templates (php pages), or into a widget area or into a content area?

Did you check out this plugin:

[wordpress.org...]

Rated 4.8 out of 5 stars. Might be difficult to style with CSS though.

~~~~

On the other hand, you can post the code you have collected here and we can all take a look at it and see if there is just something you are missing.

TechBuddy

7:45 am on Nov 13, 2014 (gmt 0)



use this code in your index.php or single.php where you want to display the post pages and change the div tag according your div.


<?php query_posts( array ('posts_per_page' => 1) ); ?>
<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"></a></h2>
<div class="blog-box">
<div class="row">
<div class="col-md-3 col-sm-3">
<a href="<?php the_permalink() ?>"><img class="img-responsive" <?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?></a>
</div>
<div class="col-md-9 col-sm-9">
<h2><?php the_title(); ?></h2>
<span class="c-red"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></span> <span class="c-light"><?php the_time('F jS , Y') ?> <?php the_author(); ?></span>
<?php the_excerpt(); ?>


<a class="btn blog-btn" href="<?php the_permalink() ?>">Read this full blog</a>

regards

[edited by: lorax at 11:02 am (utc) on Nov 13, 2014]
[edit reason] no URLs in sig please [/edit]

Sjtr

10:29 am on Nov 13, 2014 (gmt 0)

10+ Year Member



Thanks for your replies.

@Techbuddy, I tried your code but it gives me an error: C:\Program Files (x86)\Ampps\www\wordpress\wp-content\themes\custom\main-page.php on line 71 where this is on line 71: <?php get_footer(); ?>

@Planet13, here's what I got so far:

<?php $the_query = new WP_Query( 'showposts=1' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<br>
<br>
<?php echo substr(strip_tags($post->post_content), 0, 300);?>
<?php endwhile;?>


and what I used in another theme:

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div class="meta">
Posted on:
<?php the_time('F jS, Y') ?>
by
<?php the_author() ?>
&nbsp;|&nbsp;<span class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in
<?php the_category(', ') ?>
&nbsp;|&nbsp;
<?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
</span></div>
<div class="entry">
<?php
if ( has_post_thumbnail() ) {
// the current post has a thumbnail
} else {
// the current post lacks a thumbnail
}
?>
<?php
/* Display the thumbnail only when available. */
if ( has_post_thumbnail() ) :
?>
<a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php
/* Remember that we had a conditional check open; close it. */
endif;
?>
<?php the_excerpt('Read more...'); ?>

</div>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not found</h2>
<?php endif; ?>


With this in the functions.php:

<?php
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 600, 200 );
function custom_excerpt_length( $length ) {
return 43;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read more..</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Sidebar Widgets',
'id' => 'sidebar-widgets',
'description' => 'Widgets for the sidebar.',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3>',
'after_title' => '</h3>'
));
}
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
function wpbeginner_comment_text_after($arg) {
$arg['comment_notes_after'] = "We are glad you are leaving a comment. Please keep in mind that comments are moderated!";
return $arg;
}

add_filter('comment_form_defaults', 'wpbeginner_comment_text_after');
?>


Sorry for the long codes, but I'm not sure which parts I could strip from it. 'Posted on' and 'posted in' and 'No comments' code can be left out. I don't really understand how I did all that in the first place, it's code from a year ago.

Oh, also have used this; but it just pulls content from a specific page rather than last post.


<?php
$query = new WP_Query( 'post_type=page&p=38' );
while ( $query->have_posts() ) : $query->the_post();
global $more; $more = 0;
the_content( 'Lees meer..' );
endwhile;
?>


Thanks in advance and regards