I have blog which is in sub directory and showing last post on static index page on main site.
main site -> http://example.com
blog -> http://example.com/wp
The blog post is showed correctly on main site but I can't show links for prev and next post/article. This is what I have an what I'm trying
<?php
define('WP_USE_THEMES', false);
require('wp/wp-blog-header.php');
$posts = get_posts('numberposts=1&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?>
</h1><h2><?php the_date(); echo "<br />";?></h2>
<h3><?php the_content(); ?> </h3>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
<?php endforeach; ?>
So this isn't visible: <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
In Layman language it is a wordpress function to fetch posts/custom-posts. Internally it calls WP_Query function. Or you can use WP_Query instead of this. 'posts_nav_link' function does not work with WP_Query & get_posts function. You can try this to get next previous links.
$posts = get_posts('numberposts=1&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?>
</h1><h2><?php the_date(); echo "<br />";?></h2>
<h3><?php the_content(); ?> </h3>
<?php endforeach; ?>
<div class="navigation">
<p><?php previous_post_link(); ?><?php next_post_link(); ?></p>
</div>
I cant comment you because I haven't enough reputations,
First follow https://codex.wordpress.org/Template_Tags/get_posts link
then check the sample code.
and also check the post display setting in the wp-admin panel /wp-admin/options-reading.php settings->reading section.
Related
I am developing a separate website and for showing the blogs i am using the worpress.I have used following code to display blogs.It shows the textual contents properly but for video it just shows the player bar and not click able.
I have also checked themes index.php there is no the_excerpt.
When i check preview of the post using wordpress admin it shows the video properly.
can anyone help me resolve this?
here is my code..
<?php
global $more;
$posts = get_posts('category=3&numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php $more = 1; ?>
<?php the_date(); echo "<br />"; ?>
<span style="color:#1C1644;font-size:1.3em !important;font-weight: bold;">
<?php the_title(); ?>
</span>
<div id="lol"><?php the_content(); ?>
</div>
<hr>
<?php
endforeach;
?>
Please try this
<?php
global $more;
$posts = get_posts('category=3&numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) :
setup_postdata( $post ); ?>
<?php $more = 1; ?>
<?php the_date(); echo "<br />"; ?>
<span style="color:#1C1644;font-size:1.3em !important;font-weight: bold;">
<?php echo the_title(); ?>
</span>
<div id="lol">
<?php echo the_content(); ?>
</div>
<hr>
<?php
endforeach;
?>
All you need to do to embed something into a post or page is to post the URL to it into your content area. Make sure that the URL is on its own line and not hyper-linked (clickable when viewing the post).
For example:
http://www.youtube.com/watch?v=dQw4w9WgXcQ
WordPress will automatically turn that into a YouTube embed when the post is viewed.
You can also optionally wrap the URL in the [embed] shortcode. It will accomplish the same effect, but does not require the URL to be on its own line.
It also allows you to set a maximum (but not fixed) width and height, like so:
[embed width="123" height="456"]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]
If WordPress fails to embed your URL you will get a hyper-link to the URL.
Use wp custom fields. Add video_embed custom field your post and add code.
<?php echo get_post_meta($post->ID, 'video_embed', true); ?>
Edit:
if(get_post_meta($post->ID, 'video_embed', true)){
echo get_post_meta($post->ID, 'video_embed', true);
}
else
{
the_content();
}
I've created a custom post type to do hand-crafted excerpts from my blog on my portfolio site. I've got a content window, a link, and a featured image for the post type, which I have called blog.
The issue is that, no matter what I try, the posts are displayed oldest to newest, whereas I would like to display the newest first. Here's the query_posts() call:
<?php query_posts( 'post_type=blog&order=ASC'); ?>
But I've also tried more elaborate queries such as:
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?>
My complete template file looks like:
`
">
<div class="sliderContent">
<!--first loop-->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('Read More »', THEMENAME)); ?>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
<!--second loop, displays custom post type-->
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC') ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
</div>
</div>
<!-- content end -->
<?php } ?>
`
So I'm displaying the content from the page that this template is applied, then I'm displaying my custom post type.
Thanks for the help, I'm stumped!
Your query is in Ascending order. Ascending order IS oldest to newest. You want DESCENDING order if you want newest to oldest. Also, you should avoid using query_posts if at all possible, as it modifies the default Wordpress loop.
Your second query isn't that much more elaborate than the first. The only difference is you're using an array rather than a string to define the query parameters (which an array is arguably the correct way to go about it), and you're setting the orderby parameter.
Lastly, the default order is by date in descending order (newest to oldest) so you theoretically don't even NEED to define order and orderby parameters.
Try this:
<!--second loop, displays custom post type-->
<?php
$args = array('post_type' => 'blog', 'orderby'=>'date','order'=>'DESC');
/*Consider changing to: $args = array('post_type' => 'blog');*/
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php
endif;
wp_reset_postdata();
?>
</div>
Well, as majorano84 alluded to, after further reading query_posts() is not the right function to use at all (because I guess it makes more work for the server?)
Instead, I used get_posts(), and that displays the posts in the preferred order without any further effort on my part:
<?php
$args = array( 'post_type' => 'blog' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endforeach; ?>
`
So I'm going to call that the answer, because it solves the problem I was having. Thanks!
Now I've done this a couple other times with no problem, I have a main page using home.php and the blog as another page using "blog-home.php" blog template with all the right code to grab the posts but it's not displaying. The only difference is I've added a custom portfolio post field to the functions, would this be effecting it or could it be something else? I can access a post from the home page under latest post putting the code below but that's it.
<?php query_posts("post_per_page=1"); the_post(); ?>
<p><?php the_excerpt(); ?></p>
<?php wp_reset_query(); ?></div>
*UPDATE: I've tried another code but now it is only displaying the blog page as a post. *
<?php
/*
Template Name: Blog Home
*/
?>
<?php get_header(); ?>
<div id="contentwrapper">
<?php query_posts( array ( 'category_name' => 'Blog', 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="blogentry">
<h4><?php the_title(); ?> </h4>
<?php the_content(); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Maybe if you use
$posts = get_posts(array('numberposts' => 1));
global $post;
$post = $posts[0];
the_excerpt();
instead
query_posts();
is never a good idea change the global query if get_posts dosn't work for you try with WP_Query()
I've installed a plugin for Wordpress Titled "CMS Press" onto my Site.
I have created two custom fields within a custom post I've called "The Team"
The code for the page is as follows :
<div class="span16"> <!-- This is column 1 -->
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="pagehead"><h2 class="title_page"><?php echo get_post_meta($post->ID, 'title_page', true);?></h2></div>
<div class="span6 subcontent">
<?php
$args = array ('post_type' => 'team');
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="teamsubhead">
<h2 class="team_page"><?php the_title(); ?></h2>
<h3 class="job_title"><?php get_post_meta($post->ID, 'jobtitle', true); ?></h3>
</div>
<?php the_content(); ?>
<?php endforeach; ?>
</div>
<?php endwhile; ?>
</div>
The custom field I created in my custom post is called "jobtitle" I am trying to retrieve that from this line of code
<h3 class="job_title"><?php get_post_meta($post->ID, 'jobtitle', true); ?></h3>
However nothing is getting output at all.
Can someone help me with this. I suspect it may be something to do with the loop and my new query being in a new loop?
Thanks in advance.
Try it with echo at the beginning:
<?php echo get_post_meta($post->ID, 'jobtitle', true); ?>
I have a list of Recent Posts in the sidebar of a Wordpress blog. The title and author show up properly, but the excerpt that gets shown is the excerpt of the current page/post not the relevant recent post.
The code:
<?php $myposts = get_posts('numberposts=10&offset=0');
foreach($myposts as $post) :?>
<li><?php the_title();?> <span>by <?php the_author(); ?></span> <br /> <?php the_excerpt(); ?></li>
<?php endforeach; ?>
Any idea why it would be pulling the correct Title/Author, but incorrect excerpt?
<?php $myposts = get_posts('numberposts=10&offset=0');
foreach($myposts as $post) :
setup_postdata($post); ?>
<li><?php the_title();?> <span>by <?php the_author(); ?></span> <br /> <?php the_excerpt(); ?></li>
<?php endforeach;
wp_reset_query();
?>
Postdata isn't set up. Those functions pull the global values besides $post (e.g. $ID). setup_postdata() sets all the right values. Also, I'd suggest reseting the query after this.