• Resolved abreusky

    (@abreusky)


    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 “https://yourdomain.com//index.php?random=1&#8221;

    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 14 replies - 1 through 14 (of 14 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    If this is a CSS issue you’ll need to show us the webpage in question.

    Thread Starter abreusky

    (@abreusky)

    Hi Andrew,

    thank you for the quick reply!

    I don’t know if it a CSS issue… O_o

    Do you think that if I add some line to the empty plugin that I created, then I will be able to place the post title in the correct place?

    What do you think?

    This is my theme: Blogum

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Just to make sure I’m looking at the right place, can you let us know the title of the post that contains the issue?

    Thread Starter abreusky

    (@abreusky)

    Ah, ok

    This is my blog: Pretty Good Style

    When you click “Random” on the top menu, you will see that the post title is located together with the content.

    Have a look at how this is different from the normal posts.

    What do you think? Is it a CSS issue?

    edit: the images on the blog are not mine, this is just mockup content

    • This reply was modified 8 years, 2 months ago by abreusky.
    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I think I see the issue (making the assumption I’m looking at the right place). There is some CSS in your theme’s style.css file that is styling <h6> elements inside the post content in a way that is causing the issue:

    
    .post_content h6 {
        width: 145px;
        position: absolute;
        left: 0;
        font-size: 11px;
        font-weight: normal;
        color: #9d9d9d;
    }
    

    Specifically it’s this bit that is the problem:

    
        position: absolute;
    

    This looks like it could have occurred on any post whether random or normal.
    You could ignore this by not using a <h6> – I don’t see a reason why you’ve used that in your post. Heading levels should increment sequentially so there shouldn’t be gaps between a level 2 and a level 6 heading.
    Otherwise you can write a bit of CSS to override your theme’s stylesheet:

    
    html .post_content h6 {
        width: auto;
        position: static;
    }
    
    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Oh nevermind I was looking at the wrong thing, I was looking at the page you linked: https://blogum.wpshower.com/ – On this page there’s an overlap issue. If that’s causing an issue for you then look at my previous reply, otherwise ignore it.

    Thread Starter abreusky

    (@abreusky)

    And what do you think about this one https://prettygood.style ? Is this the one that has an overlap issue?

    Can I solve it with the .php file?

    Because apparently the title is on a H1 tag

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Yea ignore my advice, that was for another website.

    You seem to be using a different template for your random post. I found this by looking at the classes in the <body> tag.
    The page normally has these classes:

    
    <body class="home blog">
    

    Whereas on pressing “Random” it has these classes:

    
    <body class="post-template-default single single-post postid-31 single-format-image">
    
    Thread Starter abreusky

    (@abreusky)

    Hmmmm, yes, true, I saw it!

    And these templates are in which files on the wp-content/themes/blogum folder?

    Maybe it is something wrong I did with the initial plugin I created for the random post.

    PS: sorry guy, really sorry. My knowledge is just a bit of HTML, and I go searching for things online but I have no deep knowledge of PHP at all. ??

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Let’s step back for a moment and walk through what you were trying to achieve.
    When you press on the “Random” link, what do you expect to happen?

    This is what’s happening at the moment:
    You press on the “Random” link and then you’re taken to a random post itself.

    Is it doing what you set out to do? If yes then there isn’t a bug.

    To highlight what I mean by there not being a bug, try opening a post in a normal way. You’ll see that when you open the post, the title isn’t on the left like it was on the Homepage. This is the way your theme has been built. You can of course change it, but it isn’t a bug as I understand you’re raising it.

    Thread Starter abreusky

    (@abreusky)

    Hmmm… it’s not a bug, actually. It’s something that is not working like the normal posts.

    It’s weird because on this:

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

    I see ‘template_redirect’,’random_template’, but there’s not an actual “random_template” at none of my WordPress folders. I even looked at wp-includes folder (that is not a folder that my theme contains)…

    I changed several words to replace this ‘template_redirect’,’random_template’, but none of them worked out. I even replaced ‘template_redirect’ for ‘template_include’, didn’t work either.

    Well, I think this is way beyond my understanding :-/

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What do you mean by not working like the normal posts?
    This is a post that I’ve clicked from the home page: https://prettygood.style/2016/12/on-the-street-camo-mixing-milan/ – As you can see the title is not on the left.

    This is post that I’ve clicked from the “random” link on the home page: https://prettygood.style/2016/12/on-the-street-cricket-player-mumbai/ – The title isn’t on the left as well.

    Thread Starter abreusky

    (@abreusky)

    AHHHHHHH!!!

    I didn’t notice that: there’s a difference from when the post is “opened” to when the post is displayed on the home page!

    Oh my, so much confusion for a small, but important detail ??

    Andrew, I’m sorry for taking your time to help me find out how things were working.

    :O

    I’m sorry!

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Don’t be sorry ??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘random post’s title being loaded in a “wrong” place’ is closed to new replies.