Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 504799

random post’s title being loaded in a “wrong” place

$
0
0

Replies: 1

Hi guys!

After a lot of searching, and lots of mistakes, I was able to create a link to a random post on my blog. The only thing is that the post title is being loaded on top of the post content, whereas on a normal post the post title is beside the content, together with the post metadata.

This is what I did to create a random post…

1. Created a empty plugin with the following code:

<?php

add_action('init','random_add_rewrite');
function random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}

add_action('template_redirect','random_template');
function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
               foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
}

2. Added a custom link in the menu, which is “http://yourdomain.com//index.php?random=1”

As I said, when the random post is loaded, its title goes on top of the content.

On the content.php file, this is what happens (the post title is beside the post content):

<article id="post-<?php the_ID(); ?>" <?php post_class( 'clear' ); ?>>
	<header class="post-meta">
		<h1><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>

		<?php blogum_post_data(); ?>

		<?php blogum_comments_popup_link(); ?>

		<?php edit_post_link( __( 'Edit', 'blogum' ), '<div class="post-edit">', '</div>' ); ?>
	</header><!-- .post-meta -->

	<?php if ( is_search() ) : // Only display Excerpts for Search ?>
		<div class="post-content">
			<?php the_excerpt(); ?>
		</div><!-- .post-content -->
	<?php else : ?>
		<div class="post-content">
			<?php the_content( 'Read More', 'blogum' ); ?>
			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'blogum' ) . '</span>', 'after' => '</div>' ) ); ?>
		</div><!-- .post-content -->
	<?php endif; ?>
</article><!-- #post-<?php the_ID(); ?> -->

.
.

Does anybody know how can I solve this, putting the post title, on the random post, on the place where it normally goes?

I hope I provided you with the information needed to get some help.

Thank you guys!!


Viewing all articles
Browse latest Browse all 504799

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>